Nacos部署和使用(服务注册与发现、配置中心)

文章正文
发布时间:2025-05-13 09:02

1. docker部署nacos

参考:

2.注册中心原理

在微服务远程调用的过程中,包括两个角色:

服务提供者:提供接口供其它微服务访问,比如 A-service

服务消费者:调用其它微服务提供的接口,比如 B-service

在大型微服务项目中,服务提供者的数量会非常多,为了管理这些服务就引入了注册中心的概念。注册中心、服务提供者、服务消费者三者间关系如下:

流程如下:

服务启动时就会注册自己的服务信息(服务名、IP、端口)到注册中心

调用者可以从注册中心订阅想要的服务,获取服务对应的实例列表(1个服务可能多实例部署)

调用者自己对实例列表负载均衡(随机、轮询、IP的hash、最近最少访问...),挑选一个实例

调用者向该实例发起远程调用

当服务提供者的实例宕机或者启动新实例时,调用者如何得知呢?

服务提供者会定期向注册中心发送请求,报告自己的健康状态(心跳请求)

当注册中心长时间收不到提供者的心跳时,会认为该实例宕机,将其从服务的实例列表中剔除

当服务有新实例启动时,会发送注册服务请求,其信息会被记录在注册中心的服务实例列表

当注册中心服务列表变更时,会主动通知微服务,更新本地服务列表

3.服务注册与发现

接下来,我们创建一个服务study_nacos_A注册到Nacos,步骤如下:

引入依赖

配置Nacos地址

重启

3.3.1.添加依赖

在study_nacos_A的pom.xml中添加依赖:

<!--nacos 服务注册发现--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>

注意在父工程中药进行spring-cloud的依赖管理

<properties> <spring-cloud.version>Hoxton.SR10</spring-cloud.version> </properties> <dependencyManagement> <dependencies> <!-- springCloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> 3.3.2.配置Nacos

在item-service的application.yml中添加nacos地址配置:

server: port: 8081 spring: application: name: A-service cloud: nacos: server-addr: 192.168.152.130:8848 3.3.3 启动

启动时候可能会因为springBoot和springCloud版本问题报错:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans]: Factory method 'configurationPropertiesBeans' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.10.jar:5.3.10] at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.10.jar:5.3.10] ... 34 common frames omitted Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata at org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration.configurationPropertiesBeans(ConfigurationPropertiesRebinderAutoConfiguration.java:56) ~[spring-cloud-context-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$1b4f1aed.CGLIB$configurationPropertiesBeans$2(<generated>) ~[spring-cloud-context-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$1b4f1aed$$FastClassBySpringCGLIB$$92bb4cd3.invoke(<generated>) ~[spring-cloud-context-2.1.3.RELEASE.jar:2.1.3.RELEASE] at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.10.jar:5.3.10] at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.10.jar:5.3.10] at org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$1b4f1aed.configurationPropertiesBeans(<generated>) ~[spring-cloud-context-2.1.3.RELEASE.jar:2.1.3.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121] at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121] at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.10.jar:5.3.10] ... 35 common frames omitted Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_121] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_121] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_121] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_121] ... 46 common frames omitted Disconnected from the target VM, address: '127.0.0.1:53071', transport: 'socket' Process finished with exit code 1

一般版本对应

访问Spring官方地址,可获取SpringBoot和SpringCloud版本对应信息。

地址如下:

https://start.spring.io/actuator/info

JSON格式如下:

{ "git": { "branch": "aa1ad68ea9a0eac4180037118854753f3e200e85", "commit": { "id": "aa1ad68", "time": "2023-03-10T15:26:12Z" } }, "build": { "version": "0.0.1-SNAPSHOT", "artifact": "start-site", "versions": { "spring-boot": "3.0.2", "initializr": "0.20.0-SNAPSHOT" }, "name": "start.spring.io website", "time": "2023-03-10T15:27:19.222Z", "group": "io.spring.start" }, "bom-ranges": { "codecentric-spring-boot-admin": { "2.4.3": "Spring Boot >=2.3.0.M1 and <2.5.0-M1", "2.5.6": "Spring Boot >=2.5.0.M1 and <2.6.0-M1", "2.6.8": "Spring Boot >=2.6.0.M1 and <2.7.0-M1", "2.7.4": "Spring Boot >=2.7.0.M1 and <3.0.0-M1", "3.0.0-M4": "Spring Boot >=3.0.0-M1 and <3.1.0-M1" }, "solace-spring-boot": { "1.1.0": "Spring Boot >=2.3.0.M1 and <2.6.0-M1", "1.2.2": "Spring Boot >=2.6.0.M1 and <3.0.0-M1" }, "solace-spring-cloud": { "1.1.1": "Spring Boot >=2.3.0.M1 and <2.4.0-M1", "2.1.0": "Spring Boot >=2.4.0.M1 and <2.6.0-M1", "2.3.2": "Spring Boot >=2.6.0.M1 and <3.0.0-M1" }, "spring-cloud": { "Hoxton.SR12": "Spring Boot >=2.2.0.RELEASE and <2.4.0.M1", "2020.0.6": "Spring Boot >=2.4.0.M1 and <2.6.0-M1", "2021.0.0-M1": "Spring Boot >=2.6.0-M1 and <2.6.0-M3", "2021.0.0-M3": "Spring Boot >=2.6.0-M3 and <2.6.0-RC1", "2021.0.0-RC1": "Spring Boot >=2.6.0-RC1 and <2.6.1", "2021.0.6": "Spring Boot >=2.6.1 and <3.0.0-M1", "2022.0.0-M1": "Spring Boot >=3.0.0-M1 and <3.0.0-M2", "2022.0.0-M2": "Spring Boot >=3.0.0-M2 and <3.0.0-M3", "2022.0.0-M3": "Spring Boot >=3.0.0-M3 and <3.0.0-M4", "2022.0.0-M4": "Spring Boot >=3.0.0-M4 and <3.0.0-M5", "2022.0.0-M5": "Spring Boot >=3.0.0-M5 and <3.0.0-RC1", "2022.0.0-RC1": "Spring Boot >=3.0.0-RC1 and <3.0.0-RC2", "2022.0.0-RC2": "Spring Boot >=3.0.0-RC2 and <3.0.0", "2022.0.1": "Spring Boot >=3.0.0 and <3.1.0-M1" }, "spring-cloud-azure": { "4.6.0": "Spring Boot >=2.5.0.M1 and <3.0.0-M1", "5.0.0": "Spring Boot >=3.0.0-M1 and <3.1.0-M1" }, "spring-cloud-gcp": { "2.0.11": "Spring Boot >=2.4.0-M1 and <2.6.0-M1", "3.4.6": "Spring Boot >=2.6.0-M1 and <3.0.0-M1", "4.1.2": "Spring Boot >=3.0.0-M1 and <3.1.0-M1" }, "spring-cloud-services": { "2.3.0.RELEASE": "Spring Boot >=2.3.0.RELEASE and <2.4.0-M1", "2.4.1": "Spring Boot >=2.4.0-M1 and <2.5.0-M1", "3.3.0": "Spring Boot >=2.5.0-M1 and <2.6.0-M1", "3.4.0": "Spring Boot >=2.6.0-M1 and <2.7.0-M1", "3.5.0": "Spring Boot >=2.7.0-M1 and <3.0.0-M1", "4.0.0": "Spring Boot >=3.0.0 and <3.1.0-M1" }, "spring-shell": { "2.1.6": "Spring Boot >=2.7.0 and <3.0.0-M1", "3.0.0": "Spring Boot >=3.0.0 and <3.1.0-M1" }, "vaadin": { "14.9.6": "Spring Boot >=2.1.0.RELEASE and <2.6.0-M1", "23.2.15": "Spring Boot >=2.6.0-M1 and <2.7.0-M1", "23.3.5": "Spring Boot >=2.7.0-M1 and <3.0.0-M1", "24.0.0": "Spring Boot >=3.0.0-M1 and <3.1.0-M1" }, "hilla": { "1.3.5": "Spring Boot >=2.7.0-M1 and <3.0.0-M1", "2.0.0": "Spring Boot >=3.0.0-M1 and <3.1.0-M1" }, "wavefront": { "2.0.2": "Spring Boot >=2.1.0.RELEASE and <2.4.0-M1", "2.1.1": "Spring Boot >=2.4.0-M1 and <2.5.0-M1", "2.2.2": "Spring Boot >=2.5.0-M1 and <2.7.0-M1", "2.3.4": "Spring Boot >=2.7.0-M1 and <3.0.0-M1", "3.0.1": "Spring Boot >=3.0.0-M1 and <3.1.0-M1" } }, "dependency-ranges": { "okta": { "1.4.0": "Spring Boot >=2.2.0.RELEASE and <2.4.0-M1", "1.5.1": "Spring Boot >=2.4.0-M1 and <2.4.1", "2.0.1": "Spring Boot >=2.4.1 and <2.5.0-M1", "2.1.6": "Spring Boot >=2.5.0-M1 and <3.0.0-M1", "3.0.3": "Spring Boot >=3.0.0-M1 and <3.1.0-M1", "managed": "Spring Boot >=3.1.0-M1" }, "mybatis": { "2.1.4": "Spring Boot >=2.1.0.RELEASE and <2.5.0-M1", "2.2.2": "Spring Boot >=2.5.0-M1 and <2.7.0-M1", "2.3.0": "Spring Boot >=2.7.0-M1 and <3.0.0-M1", "3.0.0": "Spring Boot >=3.0.0-M1" }, "camel": { "3.5.0": "Spring Boot >=2.3.0.M1 and <2.4.0-M1", "3.10.0": "Spring Boot >=2.4.0.M1 and <2.5.0-M1", "3.13.0": "Spring Boot >=2.5.0.M1 and <2.6.0-M1", "3.17.0": "Spring Boot >=2.6.0.M1 and <2.7.0-M1", "3.20.2": "Spring Boot >=2.7.0.M1 and <3.0.0-M1", "4.0.0-M2": "Spring Boot >=3.0.0-M1 and <3.1.0-M1" }, "picocli": { "4.7.0": "Spring Boot >=2.5.0.RELEASE and <3.1.0-M1" }, "open-service-broker": { "3.2.0": "Spring Boot >=2.3.0.M1 and <2.4.0-M1", "3.3.1": "Spring Boot >=2.4.0-M1 and <2.5.0-M1", "3.4.1": "Spring Boot >=2.5.0-M1 and <2.6.0-M1", "3.5.0": "Spring Boot >=2.6.0-M1 and <2.7.0-M1" } } }

启动成功后,会在nacos中看到注册成功

4.配置中心

微服务共享的配置可以统一交给Nacos保存和管理,在Nacos控制台修改配置后,Nacos会将配置变更推送给相关的微服务,并且无需重启即可生效,实现配置热更新。

4.1.配置共享

我们可以把微服务共享的配置抽取到Nacos中统一管理,这样就不需要每个微服务都重复配置了。分为两步:

在Nacos中添加共享配置

微服务拉取配置

4.1.1.添加共享配置

简单示例

我们在nacos控制台添加这配置。

路径:配置管理->配置列表中点击+新建一个配置:

发布时候可能发布失败:

解决:

找到nacos配置中这两个表

查看是否有这个字段 encrypted_data_key 如果没有加上,并且可以是空,就解决了

4.1.2.拉取共享配置

我们要在微服务拉取共享配置。将拉取到的共享配置与本地的application.yml配置合并,完成项目上下文的初始化。

不过,需要注意的是,读取Nacos配置是SpringCloud上下文(ApplicationContext)初始化时处理的,发生在项目的引导阶段。然后才会初始化SpringBoot上下文,去读取application.yml。

也就是说引导阶段,application.yaml文件尚未读取,根本不知道nacos 地址,该如何去加载nacos中的配置文件呢?

SpringCloud在初始化上下文的时候会先读取一个名为bootstrap.yml(或者bootstrap.properties)的文件,如果我们将nacos地址配置到bootstrap.yml中,那么在项目引导阶段就可以读取nacos中的配置了。

因此,微服务整合Nacos配置管理的步骤如下:

1)引入依赖:

在cart-service模块引入依赖:

<!--nacos配置管理--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--读取bootstrap文件--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> <version>3.1.0</version> </dependency>

2)新建bootstrap.yml

在服务中的resources目录新建一个bootstrap.yml文件,可以把固定配置放到这里边:

示例内容如下:

server: port: 8081 spring: application: name: A-service cloud: nacos: server-addr: 192.168.152.130:8848 config: file-extension: yaml # 文件后缀名 group: DEFAULT_GROUP shared-configs: # 共享配置 - dataId: share-user.yaml # 共享mybatis配置 refresh: true 4.1.3.配置热更新

建立配置类

@ConfigurationProperties(prefix = "share-user") @EnableConfigurationProperties @Configuration @RefreshScope @Data public class ShareUserConfig { private String name; private Integer age; }

新配置

获取:

更新配置:

重新获取:

这样就实现了不重启就更新了配置

首页
评论
分享
Top