Nacos注册中心
Nacos是阿里巴巴的产品,现在是SpringCloud中的一个组件。相比Eureka功能更加丰富,在国内受欢迎程度较高。
一、服务注册到Nacos
(1)在cloud-demo父工程中添加spring-cloud-alilbaba的管理依赖:
<!--nacos的管理依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
(2)注释掉order-service和user-service中原有的eureka依赖。
(3)添加nacos的客户端依赖【修改order-service和user-service】:
<!--引入eureka客户端依赖--> <!-- <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-discovery</artifactId> </dependency>
(4)修改user-service&order-service中的application.yml文件,注释eureka地址,添加nacos地址
server: port: 8081 spring: datasource: url: jdbc:mysql://localhost:3306/cloud_user?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true username: root password: 130740 driver-class-name: com.mysql.cj.jdbc.Driver application: # user服务的服务名称 name: userservice cloud: nacos: server-addr: localhost:8848 # nacos服务地址 mybatis: type-aliases-package: cn.itcast.user.pojo configuration: map-underscore-to-camel-case: true logging: level: cn.itcast: debug pattern: dateformat: MM-dd HH:mm:ss:SSS #eureka: # client: # service-url: # eureka的地址信息 # defaultZone: http://localhost:10086/eureka
启动Nacos:startup.cmd -m standalone