SpringBoot整合Nacos
首先看版本对应
https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明
然后导入依赖
Nacos集群搭建参照 Windows安装Nacos单机&集群
<!-- nacos --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>0.2.1.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.0.2.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-spring-context</artifactId> <version>0.3.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
然后配置bootstrap.yml (注意要用bootstrap.yml)
spring: application: name: test-service profiles: active: dev cloud: nacos: #配置中心 config: server-addr: 127.0.0.1:8848,127.0.0.1:8847 file-extension: yaml encode: UTF-8 shared-dataids: share-config1.yml,share-config2.yml refreshable-dataids: share-config1.yml,share-config2.yml # shared-dataids和refreshable-dataids就等价于下面的ext-config的配置,只是ext-config的配置更加灵活 # ext-config: # - data-id: share-config1.yml # group: DEFAULT_GROUP # refresh: true # - data-id: share-config2.yml # group: DEFAULT_GROUP # refresh: true #注册中心 discovery: server-addr: 127.0.0.1:8848,127.0.0.1:8847 service: ${spring.application.name} enabled: true management: endpoints: web: exposure: exclude: "*"
使用的是DEFAULT_GROUP组下的dataid为test-service-dev.yaml的配置文件
启动类上加上注解
@EnableDiscoveryClient
@RefreshScope
启动后查看nacos就会看到名为test-service的服务
劈天造陆,开辟属于自己的天地!!!与君共勉