Spring Cloud实战 | 第二篇:Spring Cloud整合Nacos实现注册中心

有来技术团队2022-01-13 09:50:29博主文章分类:Spring Cloud©著作权

文章标签Spring Cloudspring微服务动态更新文章分类其它其它阅读数149

SpringCloud注册中心由eureka切换到nacos

前言

随着eureka的停止更新,如果同时实现注册中心和配置中心需要SpringCloud Eureka和SpringCloud Config两个组件;配置修改刷新时需要SpringCloud Bus消息总线发出消息通知(Kafka、RabbitMQ等)到各个服务完成配置动态更新,否者只有重启各个微服务实例,但是nacos可以同时实现注册和配置中心,以及配置的动态更新。

版本声明

Nacos Server: 1.3.2

SpringBoot: 2.3.0.RELEASE

SpringCloud: Hoxton.SR5

SpringCloud Alibaba: 2.2.1.RELEASE

项目实战

1.项目背景

本篇项目实战是基于开源 有来商城youlai-mall​ 使用的微服务基础脚手架 ​ ​youlai​​ 这个项目,原先使用的是eureka,现在在此基础上创建个nacos分支,并在这个分支上完成eureka到nacos的升级,话不多说,一张图说明。

下文就认证中心youlai-auth模块升级为nacos举例说明

2.工程整合nacos

父工程添加spring-cloud-alibaba依赖

 

<properties>
<spring-cloud-alibaba.version>2.2.0.RELEASE</spring-cloud-alibaba.version>
</properties>

<dependencyManagement>
<dependencies>
...
<!--Spring Cloud Alibaba 相关依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
...
</dependencies>
</dependencyManagement>

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

子模块youlai-auth添加nacos依赖

 

<!-- nacos 依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

应用启动类入口类添加注解开启服务的注册和发现

 

@EnableDiscoveryClient
@SpringBootApplication
public class AuthApplication {
public static void main(String[] args) {
SpringApplication.run(AuthApplication.class);
}
}

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

服务注册中心配置

 

spring:
application:
name: youlai-auth
cloud:
nacos:
discovery:
server-addr: http://localhost:8848

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

打开nacos控制台进入服务列表查看

结语

至此youlai-auth模块整合nacos注册中心完成了,完整代码地址在 ​ ​https://github.com/users/hxrui​​ ,下篇就youlai-auth如何整合nacos完成配置中心进行说明。未完待续...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值