This article is not suitable for beginners. For beginners, please move to the basic knowledge of Baidu.
Nacos can be used as both a registration center and a configuration center, which can replace the traditional eureka+cloud config method, and nacos has its own graphical management interface and supports hot loading. Nacos can seamlessly switch with eureka+cloud config, so It also supports remote calls such as RestTemplate and Feign
One: Environment
The relevant version of this article, the version must correspond well, otherwise various strange errors will be reported
1:spring-cloud-alibaba 2.1.2.RELEASE
2:spring-cloud Greenwich.RELEASE:
3:spring-boot 2.1.4.RELEASE:
4 : nacos 0.9.0.RELEAS
5: nacos server version 1.0.1
The corresponding version correspondence is given below
Spring Cloud Version Spring Cloud Alibaba Version Spring Boot Version
Spring Cloud Edgware 1.5.1.RELEASE 1.5.X.RELEASE
Spring Cloud Finchley 2.0.2.RELEASE 2.0.X.RELEASE
Spring Cloud Greenwich 2.1.2.RELEASE 2.1.X.RELEASE
Spring Cloud Hoxton.RELEASE 2.2.0.RELEASE 2.2.X.RELEASE
Spring Cloud Hoxton.SR3 2.2.1.RELEASE 2.2.5.RELEASE
Two: download nacos
Address: https://github.com/alibaba/nacos/releases , just unzip after downloading
Windows system click startup.cmd to start. Command startup under linux: nohup sh startup.sh -m standalone & or bash startup.sh -m standalone & , the default port is 8848
Start successfully, visit http://localhost:8848/nacos on the browser, it will jump to the login interface, the default login user name is nacos, and the password is also nacos.
Configuration management and service management can be performed inside
Three: replace eureka with nacos
There are mainly 4 points
1: Add SpringCloud Alibaba and nacos dependencies in pom
<!-- alibaba cloud -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--nacos-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-config</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
2: yml excludes euroke dependency
autoconfigure:
exclude: org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration #排除eureka注册中心
3: Comment out the config configuration in yml
The complete yml code bootstrap.yml, note that it must be bootstrap.yml, bootstrap is loaded before application
#eureka注册中心地址
#eureka:
# instance:
# prefer-ip-address: true
# client:
# service-url:
# defaultZone: http://xxx.xxx.xxx.xxx:8060/eureka/
# register-with-eureka: true
# fetch-registry: true
#客户端配置中心配置
#spring:
# application:
# name: api-system
# cloud:
# config:
# fail-fast: true #是否启动快速失败功能,功能开启则优先判断config server是否正常
# name: api-druid-hdys #配置中心Git仓库config文件夹里的文件名字
# label: master #默认分支master
# profile: dev #不加此属性直接获取api-druid-hdys.yml,加了后符合config的名字规则api-druid-hdys-dev.yml
# discovery:
# enabled: true
# service-id: config-server #spring cloud 配置中心服务名
#nacos注册中心配置
spring:
application:
name: api-system
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
namespace: 6e65de9c-9069-4d05-8c20-2f94602163aa
config:
server-addr: 127.0.0.1:8848
namespace: 6e65de9c-9069-4d05-8c20-2f94602163aa
file-extension: yaml #nacos config扩展名 目前仅支持ymal和properties
prefix: api-hdys-durid-sys
profiles:
active: dev #和Nacos中的dataId 格式对应 ${prefix}-${spring.profile.active}.${file-extension}
autoconfigure:
exclude: org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration #排除eureka注册中心
Nacos related fields are explained, mainly the following configuration. The configuration here dataId
corresponds to the format in Nacos . My one here is api-hdys-durid-sys-dev.yaml. The complete format of nacos is as follows:
${prefix}-${spring.profile.active}.${file-extension}
spring:
application:
name: api-system
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 #nacos服务的地址:端口号
namespace: 6e65de9c-9069-4d05-8c20-2f94602163aa #nacos命名空间的id
config:
server-addr: 127.0.0.1:8848
namespace: 6e65de9c-9069-4d05-8c20-2f94602163aa
file-extension: yaml #nacos配置中心需要的文件后缀,目前仅支持ymal和properties
prefix: api-hdys-durid-sys #nacos配置中心文件名
profiles:
active: dev #nacos配置中心文件名开发环境
心
4: Startup class
由原来的 @EnableEurekaClient 改为 @EnableDiscoveryClient
Four: configuration file content
Here you can configure the basic information of the database, etc. It is not recommended to configure the port number here, because multiple services may call the same configuration file
api-hdys-durid-sys-dev.yaml
After the above steps, the conversion from eureka to nacos can basically be completed. It is recommended to put the routing information of gatway in the nacos configuration center for hot loading. Of course, if you want to change it back, just change the steps above.
Since nacos has been outputting heartbeat logs on the console, you can change the log level to error in the configuration center
logging:
level:
com:
alibaba:
nacos:
client:
naming: error