eureka迁移到nacos,springboot项目适配

一、修改POM依赖

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

改为

<!--nacos-->
<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
   <version>2.2.5.RELEASE</version>
</dependency>
<dependency>
   <groupId>com.alibaba.cloud</groupId>
   <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
   <version>2.2.5.RELEASE</version>
</dependency>
<resources>
   <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
      <includes>
         <include>application-${spring.profiles.active}.properties</include>
         <include>application.properties</include>
         <include>logback-spring.xml</include>
      </includes>
   </resource>
</resources>

改为

<resources>
   <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
      <includes>
         <include>bootstrap.properties</include>
         <include>logback-spring.xml</include>
      </includes>
   </resource>
</resources>

使用bootstrap.properties文件启动,去除对应环境的配置文件,使用nacos namespace区分环境
二、修改配置文件
删除application.properties文件,添加bootstrap.properties配置文件

eureka.instance.prefer-ip-address=true
eureka.instance.ip-address=${spring.cloud.client.ip-address}
eureka.instance.appname=${spring.application.name}
eureka.instance.instanceId=${spring.cloud.client.ip-address}:${server.port}
eureka.client.registry-fetch-interval-seconds=20
eureka.instance.lease-renewal-interval-in-seconds=10
eureka.instance.lease-expiration-duration-in-seconds=22
eureka.client.healthcheck.enabled=true

改为:

spring.cloud.nacos.config.server-addr=opennacos.iflydocs.com:80
spring.cloud.nacos.config.namespace=LOCAL
spring.cloud.nacos.config.username=admin
spring.cloud.nacos.config.password=opendocs2021_

三、启动类修改

@EnableEurekaClient

改为

@EnableDiscoveryClient

四、nacos控制台添加配置文件
创建namespace 命名为LOCAL
在LOCAL下新建配置文件:iflydocs-sdk-oss.properties
默认命名规则:${prefix}-${spring.profile.active}.${file-extension}
prefix:spring.application.name 项目名
spring.profile.active 生效环境,这里没配置
file-extension:spring.cloud.nacos.config 默认properties

填写iflydocs-sdk-oss.properties内容,即application-local.properties内容去除eureka配置,添加nacos discovery配置

server.port=9085
spring.cloud.nacos.discovery.server-addr=opennacos.iflydocs.com:80
spring.cloud.nacos.discovery.username=xx
spring.cloud.nacos.discovery.password=xx
spring.cloud.nacos.discovery.namespace=LOCAL
nacos.instance.instanceId=${spring.cloud.client.ip-address}:${server.port}
#file system
spring.data.mongodb.fs.uri=mongodb://xx
swagger.is.enable=true

五、启动验证
idea下可能需要重新导入项目,才能正常启动。
启动日志:

09-23 10:25:07.924 [main] INFO o.s.c.b.config.PropertySourceBootstrapConfiguration - Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource@1718906711 {name='iflydocs-sdk-oss.properties,DEFAULT_GROUP', properties={server.port=9085, spring.cloud.nacos.discovery.server-addr=opennacos.iflydocs.com:80, spring.cloud.nacos.discovery.username=xx, spring.cloud.nacos.discovery.password=xx, spring.cloud.nacos.discovery.namespace=LOCAL, nacos.instance.instanceId=${spring.cloud.client.ip-address}:${server.port}, spring.data.mongodb.fs.uri=xx(相关配置信息)
09-23 10:25:07.945 [main] INFO com.iflytek.iflydocs.oss.IflydocsOssApplication - No active profile set, falling back to default profiles: default

查看注册服务信息:

d2e9c3fd-cd15-4973-b9ce-7863ace09075.png
六、使用脚本启动与环境配置
java -Xmx1024m -Xms256m -jar $dir$jarfile --spring.cloud.nacos.config.server-addr=ip1:port1,ip2:port2 --spring.cloud.nacos.config.namespace=LOCAL
更改环境需要更改配置:
启动脚本中的
spring.cloud.nacos.config.server-addr
spring.cloud.nacos.config.namespace,
以及对应配置文件中的
spring.cloud.nacos.discovery.server-addr
spring.cloud.nacos.discovery.namespace

发表新评论