在微服务架构中,服务注册与发现、以及配置中心都是非常重要的组件,而Nacos正是一个可以同时实现服务注册发现和配置中心的解决方案。

在这里插入图片描述

本篇博客将会介绍如何使用Spring Cloud和Nacos来实现服务注册和配置中心的整合。

1. 概述

首先,我们需要了解一下Nacos的一些基本概念:

服务提供者(Provider):提供服务的应用,例如一个Web应用。
服务消费者(Consumer):调用服务的应用,例如另一个Web应用。
注册中心(Registry):服务提供者将自己的服务注册到注册中心,服务消费者从注册中心获取服务信息。
配置中心(Config Center):可以用来动态配置服务提供者和消费者的参数。
Nacos提供了以下两种方式来实现服务注册与发现和配置中心:

服务注册与发现:Nacos Server可以作为服务注册中心,实现服务注册与发现的功能。
配置中心:Nacos Config可以作为配置中心,实现动态配置服务提供者和消费者的参数。
同时,Spring Cloud也提供了相应的组件来集成服务注册与发现和配置中心:

Spring Cloud Netflix Eureka:可以实现服务注册与发现功能。
Spring Cloud Config:可以实现配置中心功能。
但是,随着Spring Cloud Alibaba的推出,我们可以直接使用Spring Cloud Alibaba提供的组件来集成Nacos。在本篇博客中,我们将使用以下组件:

Spring Cloud Alibaba Nacos Discovery:用来实现服务注册与发现的功能。
Spring Cloud Alibaba Nacos Config:用来实现配置中心的功能。

2. 集成Nacos Discovery

2.1 添加依赖

首先,在pom.xml文件中添加以下依赖:

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

2.2 配置Nacos Server地址

application.yml文件中添加以下配置:

spring:
  application:
    name: ddz
  cloud:
    nacos:
      discovery:
        # 用户名和密码 2.0版本之后需要显式配置
        username: nacos
        password: nacos
        server-addr: localhost:8848
        service: ${spring.application.name}
        # 如果需要配置命名空间那必须填命名空间的ID
        namespace: 9ebef975-dcc0-4430-9c63-1c62d8a86d82
        # 是否注册 默认是 ture
        register-enabled: true

其中,server-addr表示Nacos Server的地址和端口号。

2.3 开启注册服务功能

使用@EnableDiscoveryClient注解开启服务注册功能。

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

此时,应用启动后,可以在Nacos Server的控制台中看到该服务的注册信息。
在这里插入图片描述

3. 集成Nacos Config

3.1 添加依赖

在pom.xml文件中添加以下依赖:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

3.2 配置Nacos和引入公共配置

注意:需要再resources目录下创建一个bootstrap.yml用来配置nacos:config。
原因:将Nacos配置信息放在bootstrap.yml中的另一个原因是在应用启动过程中,Spring Boot会先加载bootstrap.yml文件,然后再加载application.yml文件。这样做可以确保在应用启动时先加载配置中心的配置信息,然后再加载应用本身的配置信息,从而确保配置中心的配置可以覆盖应用本身的默认配置,实现动态的配置管理。
bootstrap.yml文件中添加以下配置:

spring:
  cloud:
    nacos:
      config:
      	# 用户名和密码 2.0版本之后需要显式配置
        username: nacos
        password: nacos
        server-addr: localhost:8848
        # 如果需要配置命名空间那必须填命名空间的ID
        namespace: 9ebef975-dcc0-4430-9c63-1c62d8a86d82
        # 分组默认是	DEFAULT_GROUP
        group: ddz
        # data-id前缀(会自动获取spring.profiles.active追加在后面)
        prefix: ddz-user-dev
        # 配置文件后缀
        file-extension: yaml
        # 共享配置
        shared-configs:
          # 共享配置的文件ID的全称
          - data-id: common-mysql-redis.yml
            # 如果上面配置了group 这里也必须配置
            group: ddz
            # 是否开启动态刷新,默认不支持;公共配置不建议动态刷新,我这里测试就开启的。
            refresh: true
        # 是否开启动态刷新
        refresh-enabled: true

其中,server-addr表示Nacos Server的地址和端口号,namespace、group、prefix分别表示Nacos Config中的命名空间、组、前缀。这些配置信息可以根据实际情况进行修改。

3.3 在Nacos控制台创建配置文件

Nacos Config中创建一个配置文件,例如example.properties,并在其中添加以下内容:
在这里插入图片描述
在这里插入图片描述

example:
	name: Spring Cloud Alibaba
example:
	version: 1.0.0

3.4 读取配置信息

注意:像我们上面配置了动态刷新,还需要在类上添加@RefreshScope注解才能实现功能;这样我们每次在Nacos上修改配置信息就不要重启我们的服务器去加载了。
使用@Value注解读取配置信息:

@RefreshScope
@RestController
public class ExampleController {

    @Value("${example.name}")
    private String name;

    @Value("${example.version}")
    private String version;

    @GetMapping("/info")
    public String info() {
        return "Name: " + name + ", Version: " + version;
    }
}

此时,访问/info接口,即可获取配置信息。

3.5 如需配置数据源

本地项目中加了 MySQL的驱动和Mybatis-plus的依赖,然后在实测中我在nacos上面直接配置spring.datasouce.url,他会报一个URL没有配置的错误:
在这里插入图片描述
然后我再添加dynamic多数据源的依赖即可使用。

4. 总结

本篇博客介绍了如何使用Spring Cloud和Nacos来实现服务注册和配置中心的整合。具体步骤包括添加依赖、配置Nacos Server地址和配置信息、创建配置文件、读取配置信息等。

通过这些步骤,我们可以很方便地实现微服务架构中的服务注册发现和配置中心,使应用更加灵活和可扩展。

GitHub 加速计划 / na / nacos
29.82 K
12.75 K
下载
Nacos是由阿里巴巴开源的服务治理中间件,集成了动态服务发现、配置管理和服务元数据管理功能,广泛应用于微服务架构中,简化服务治理过程。
最近提交(Master分支:2 个月前 )
2b178bec 1 天前
a0fdc42c * Add some check in DiskUtil and add unit test. * simply DiskUtils in control plugin. 1 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐