【Java】Spring Boot 2 集成 nacos
官方文档:https://nacos.io/zh-cn/docs/quick-start-spring-boot.html
pom
本次Springboot版本 2.2.6.RELEASE
,nacos-config 版本 0.2.7
,nacos-discovery版本 0.2.7
parent
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.6.RELEASE</version><relativePath/> <!-- lookup parent from repository -->
</parent>
dependencies
<!-- nacos-config-->
<dependency><groupId>com.alibaba.boot</groupId><artifactId>nacos-config-spring-boot-starter</artifactId><version>0.2.7</version>
</dependency>
<!-- nacos-discovery-->
<dependency><groupId>com.alibaba.boot</groupId><artifactId>nacos-discovery-spring-boot-starter</artifactId><version>0.2.7</version>
</dependency>
Nacos
Nacos版本 nacos-server-2.1.0
,启动方式 ./startup.cmd -m standalone
单机方式.
实现配置的动态变更
注解方式
示例使用默认的空间
my.http_url=aaaaa
server.port=8092
.................
package cn.com.codingce.demo;import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@NacosPropertySource(dataId = "test", autoRefreshed = true)
@SpringBootApplication
public class CodingceDemoApplication {public static void main(String[] args) {SpringApplication.run(CodingceDemoApplication.class, args);}}
启动项目
2023-02-26 10:39:28.055 INFO 9544 --- [ main] c.a.b.n.c.u.NacosConfigPropertiesUtils : nacosConfigProperties : NacosConfigProperties{serverAddr='127.0.0.1:8848', contextPath='null', encode='null', endpoint='null', namespace='null', accessKey='null', secretKey='null', ramRoleName='null', autoRefresh=false, dataId='null', dataIds='null', group='DEFAULT_GROUP', type=null, maxRetry='null', configLongPollTimeout='null', configRetryTime='null', enableRemoteSyncConfig=false, extConfig=[], bootstrap=Bootstrap{enable=false, logEnable=false}}
测试
package cn.com.codingce.demo.conrtoller;import cn.com.codingce.demo.utils.ResT;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("test")
public class Test {/*** 通过NacosValue读取配置,* autoRefreshed 表示是否自动更新*/@NacosValue(value = "${my.http_url}", autoRefreshed = true)private String httpUrl;/*** 信息*/@RequestMapping(value = "/a", method = RequestMethod.GET)public ResT info() {return ResT.ok().put("nacos", httpUrl);}}
http://localhost:8092/test/a
测试可以实现动态的配置变更.
配置文件方式
采用自定义的命名空间进行测试,新建命名空间
新建test配置
这里我们已经创建完了,就截一张编辑图片.
配置文件 application.properties
# nacos
# nacos.config.bootstrap.enable 这个配置必须设置, 否则不会读取 properties 配置文件
nacos.config.bootstrap.enable=true
# bootstrap.log-enable
nacos.config.bootstrap.log-enable=true
# server-addr 本机启动, 远端写远端的ip:端口
nacos.config.server-addr=127.0.0.1:8848
# nacos.config.type=properties 这个配置必须设置, 未设置会报空指针, 支持的类型 properties xml json text html yaml
nacos.config.type=properties
# Data Id
nacos.config.dataId=test
# 命名空间ID
nacos.config.namespace=7746f477-222d-4c88-8244-3fae3ae5bdfa
# Group
nacos.config.group=dev
# 自动刷新
nacos.config.auto-refresh=true
nacos.config.bootstrap.enable=true
nacos.config.type=properties
这两个配置有点坑了,也怪自己不仔细,大家用的时候注意是否缺少这两个配置~
2023-02-26 10:57:13.579 INFO 7856 --- [ main] c.a.b.n.c.u.NacosConfigPropertiesUtils : nacosConfigProperties : NacosConfigProperties{serverAddr='127.0.0.1:8848', contextPath='null', encode='null', endpoint='null', namespace='7746f477-222d-4c88-8244-3fae3ae5bdfa', accessKey='null', secretKey='null', ramRoleName='null', autoRefresh=true, dataId='test', dataIds='null', group='dev', type=PROPERTIES, maxRetry='null', configLongPollTimeout='null', configRetryTime='null', enableRemoteSyncConfig=false, extConfig=[], bootstrap=Bootstrap{enable=true, logEnable=true}}
测试可以实现动态的配置变更.
实现服务的注册与发现
// todo