当前位置: 首页 > news >正文

springboot链接nacos测试

代码资料链接:https://download.csdn.net/download/ly1h1/90881498

场景说明:本次测试是springboot项目,可以链接上ncaos,将对应命名空间下的配置信息读取出俩,然后可以在接口进行返回显示。

0.环境配置

1.代码结构

2.POM配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.itheima</groupId><artifactId>demonacostest</artifactId><version>0.0.1-SNAPSHOT</version><name>demonacostest</name><description>demonacostest</description><properties><java.version>8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.28</version></dependency>
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-bootstrap</artifactId>-->
<!--            <version>3.0.3</version>-->
<!--        </dependency>--></dependencies><dependencyManagement><dependencies><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.5.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

3.bootstrap.yml如下,不需要application.yml

spring:application:name: nacos-testcloud:nacos:config:server-addr: 43.162.118.209:8848username: nacospassword: nacosnamespace: f90776be-d19e-41c4-b55f-4cdcae33fa57file-extension: yaml
# 本地配置示例
app:local-config: 这是本地配置

4.配置属性模型

ConfigProperties
package com.itheima.demonacostest.config;import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;import java.io.Serializable;@Data
@Component
@RefreshScope
@ConfigurationProperties(prefix = "base.config")
public class ConfigProperties implements Serializable {private String appKey;private String appKey2;
}

5.接口如下

package com.itheima.demonacostest.controller;import com.itheima.demonacostest.config.ConfigProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/api/config")
public class ConfigController {@Autowiredprivate ConfigProperties nacosConfigProperties;@RequestMapping(value = "getConfig", method = RequestMethod.GET)public String getConfig() {return nacosConfigProperties.getAppKey();}@RequestMapping(value = "getConfig2", method = RequestMethod.GET)public String getConfig2() {return nacosConfigProperties.getAppKey2();}
}

6.nacos配置如下

7.效果

http://www.xdnf.cn/news/571339.html

相关文章:

  • 分类预测 | Matlab实现PNN概率神经网络多特征分类预测
  • 数学实验(Matlab绘图基础)
  • 大量程粗糙度轮廓仪适用于哪些材质和表面?
  • 矿物绝缘加热电缆行业2025数据分析报告
  • 使用Gemini, LangChain, Gradio打造一个书籍推荐系统 (第一部分)
  • Dockerfile指令详解
  • 打卡第二十四天
  • Swagger在java的运用
  • PostgreSQL 日常维护
  • Elasticsearch常用命令
  • 宁夏建设工程专业技术职称评审条件
  • 嵌入式Linux:移植使用scp指令
  • Java多线程深度解析:从核心机制到高阶实战
  • upload-labs通关笔记-第16关 文件上传之exif_imagetype绕过(图片马)
  • springcloud集成seata报错Error creating bean with name ‘globalTransactionScanner‘
  • 解决RedisTemplate的json反序列泛型丢失问题
  • SpringAI开发SSE传输协议的MCP Server
  • 安卓开发用到的设计模式(1)创建型模式
  • OpenCv高阶(十四)——LBPH人脸识别
  • 如何用 Qwen1.5-7B-Chat 模型打造高效轻量的 Python 智能助手(详细实操指南)
  • Windows在PowerShell或CMD运行 curl 命令报错 解决办法 (zx)
  • 如何支持Enhanced RTMP H.265(HEVC)
  • 蓝耘Ubantu服务器测试最新 PP-StructureV3 教程
  • linux 查看java的安装路径
  • Java面试问题基础篇
  • Wireshark 抓包工具使用
  • Visual Studio Code插件离线安装指南:从市场获取并手动部署
  • Android Framework学习八:SystemServer及startService原理
  • 鸿蒙开发——9.wrapBuilder与@BuilderParam对比解析
  • Oracle 11g post PSU Oct18 设置ssl连接(使用jks)