SpringCloud集成nacos注册中心
1、Nacos服务端搭建
下载地址:https://github.com/alibaba/Nacos/releases
1)linux环境启停:
①:把我们的Nacos包解压 tar -zxvf nacos-server-1.1.4.tar.gz
②:cd 到我们的解压目录nacos cd nacos
③:进入到bin目录下 执行命令(启动单机) sh startup.sh -m standalone
④:检查nacos启动的端口 lsof -i:8848
⑤:访问nocas的服务端 http://192.168.159.8:8848/nacos/index.html
默认的用户名密码是 nocas/nocas
2)windwos启动
需要在解压bin目录,通过startup.cmd -m standalone启动,直接启动会报错
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverMemberManager' defined in URL [jar:file:/D:/softPackage/nacos-server-2.4.3/nacos/target/nacos-server.jar!/BOOT-INF/lib/nacos-core-2.4.3.jar!/com/alibaba/nacos/core/cluster/ServerMemberManager.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.nacos.core.cluster.ServerMemberManager]: Constructor threw exception; nested exception is ErrCode:500, ErrMsg:jmenv.tbsite.net
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:306)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1372)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1222)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:336)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:334)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:209)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1391)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:904)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:781)
... 99 common frames omitted
3)访问页面
注意:未配置用户名和密码不会出现登录页面直接进入主页,但是会提示一行蓝色的字,没进行权限认证,可能会出现查询不到服务
4)使用设置的用户名和密码登录,此处我设置的都是nacos
5)进入主页面,由于工程中对应配置文件已经配置了nacos,未进行任何配置服务列表为空
2、工程中配置,服务发现
(1)引入依赖,注意此处需与jdk版本,springBoot版本对应。
我这里的jdk版本为:17
springBoot版本为:3.3.4
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2023.0.1.2</version>
</dependency>
(2)修改配置文件.
在Spring:下级,cloud中配置nacos
cloud:
nacos:
discovery:
server-addr: http://192.168.1.226:8848
username: nacos
password: nacos
与Spring同级加入权限认证
nacos:
core:
auth:
enabled:true
Server-addr:nacos服务端的地址
username和password配置定义的用户名和密码,若不配置用户名,启动后会报错找不到user
nacos服务端的配置文件也需要修改:
打开config/application.properties,修改以下内容:
nacos.core.auth.enabled=true
nacos.core.auth.server.identity.key=${NACOS_AUTH_IDENTITY_KEY:example}
nacos.core.auth.server.identity.value=${NACOS_AUTH_IDENTITY_VALUE:example}
nacos.core.auth.plugin.nacos.token.secret.key=${NACOS_AUTH_TOKEN:SecretKey01234567890123456789012345345678999987654901234567890}
(3)增加@EnableDiscoveryClient注解(可不加)
(4)查看效果。此处的服务名对应配置文件application:name的名字,同一服务集群配置一样的服务名。分组可在配置文件配置group
当服务宕机或者新增,服务端自动会刷新。
3、配置管理
nacos支持动态配置功能
(1)引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
(2)新建bootStrap.yaml,内容如下:
spring:
cloud:
nacos:
config:
server-addr: http://192.168.1.226:8848
name: orderService
file-extension: yaml
group: DEFAULT_GROUP
username: nacos
password: nacos
(3)修改application.yml配置文件,新增config:
cloud:
nacos:
discovery:
server-addr: http://192.168.1.226:8848
username: nacos
password: nacos
#新增
config:
import-check:
enabled: false
file-extension: yml
(4)在nacos服务定义
DataId:
1.如果配置文件中配置了
profiles:
active: dev
此处的DataId为[application.name]-[active] .yaml
未配置active则为服务名
Group:未指定默认为DEFAULT_GROUP,指定按照配置文件的分组
配置内容:需要动态配置的属性,注意这些属性application.yml中必须存在,否则会报错
Bootstap.yaml会读取nacos此处定义的yaml,再交给nacos修改
(5)效果
1.配置文件
配置文件中配置的端口号为8081
2.nacos新增配置管理
3、验证代码,在Controller中加@RefreshScope,用于nacos服务端修改配置刷新
通过@Value获取端口号
@RestController
@RequestMapping("/order")
@RefreshScope
public class OrderController {
@Value("${server.port}")
private String port;
@GetMapping("/getPort")
public String getPort(){
System.out.println("当前服务端口"+port);
return port;
}
}