本章讲解的是在不改变原有业务的情况下将springboot改为springcloud项目使用nacos作为注册中心
首先在官网下载好nacos注册中心,在bin目录中startup.cmd文件为启动命令默认端口号是8888
接下来修改原有项目依赖
在父依赖模块添加springcloud依赖版本,若没有父依赖直接在项目指定也一样
首先指定版本
1
2
3
4
5
|
< properties > < cloud.version >Hoxton.RELEASE</ cloud.version > < alibaba.version >2.2.0.RELEASE</ alibaba.version > <!--原有项目代码--> </ properties > |
添加依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< dependency > < groupId >org.springframework.cloud</ groupId > < artifactId >spring-cloud-dependencies</ artifactId > < version >${cloud.version}</ version > < type >pom</ type > < scope >import</ scope > </ dependency > < dependency > < groupId >com.alibaba.cloud</ groupId > < artifactId >spring-cloud-alibaba-dependencies</ artifactId > < version >${alibaba.version}</ version > < type >pom</ type > < scope >import</ scope > </ dependency > |
子项目中添加nacos依赖
1
2
3
4
5
|
<!-- 服务注册 --> < dependency > < groupId >com.alibaba.cloud</ groupId > < artifactId >spring-cloud-starter-alibaba-nacos-discovery</ artifactId > </ dependency > |
启动类加注解
1
|
@EnableDiscoveryClient |
在application配置文件添加
指定naocs地址,以及服务器名称
1
2
3
4
5
6
7
|
spring: cloud: nacos: discovery: server-addr: 127.0.0.1:8888 application: name: lzq-v587 |
此时启动项目后,在启动naocs,登陆nacos网址
http://localhost:8888/nacos;账号密码都是nacos
在服务列表就可以看到当前服务;nacos是懒加载的,需要先访问服务,naocs中才会显示
到此这篇关于SpringBoot项目改为SpringCloud项目使用nacos作为注册中心的文章就介绍到这了,更多相关SpringBoot项目改为SpringCloud内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_52210557/article/details/124017477