alibabaCloud整合Nacos注册中心和配置中心

一、微服务版本依赖

Spring Cloud VersionSpring Cloud Alibaba VersionSpring Boot Version
Hoxton.SR82.2.3.RELEASE2.3.2.RELEASE
Greenwich.SR62.1.3.RELEASE2.1.13.RELEASE
Hoxton.SR82.2.2.RELEASE2.3.2.RELEASE
Hoxton.SR32.2.1.RELEASE2.2.5.RELEASE
Hoxton.RELEASE2.2.0.RELEASE2.2.X.RELEASE
Greenwich2.1.2.RELEASE2.1.X.RELEASE
Finchley2.0.3.RELEASE2.0.X.RELEASE
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    <version>2.2.1.RELEASE</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>

二、注册中心

  1. 添加依赖
// 服务发现
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
  1. @EnableDiscoveryClient 开启服务注册发现功能
@EnableDiscoveryClient
@SpringBootApplication
public class SystemApplication {
    public static void main(String[] args) {
        SpringApplication.run(SystemApplication.class, args);
    }
}
  1. bootstrap.yml 中配置 nacos
spring:
  application:
    name: sun-system
  profiles:
    # 环境配置
    active: dev
  cloud:
    nacos:
      discovery:
        # 服务发现地址
        server-addr: 127.0.0.1:8848
        # 命名空间,一般用来区分环境DEV,不同环境之间不可相互调用
        namespace: 1d26e04f-aaba-4534-a16b-e25082642072
        # 分组,默认DEFAULT_GROUP,不同分组之间不可调用
        group: LOCAL_GROUP

在这里插入图片描述

三、配置中心

  1. 添加依赖

// 配置中心
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
  1. bootstrap.yml 中配置 nacos

spring:
  application:
    name: sun-system
  profiles:
    # 环境配置
    active: dev
  cloud:
    nacos:
      config:
        # 配置文件后缀,常用yml
        file-extension: yml
        # 命名空间,一般用来区分环境DEV
        namespace: 1d26e04f-aaba-4534-a16b-e25082642072
        # 配置中心地址
        server-addr: 127.0.0.1:8848
        # 分组,默认DEFAULT_GROUP
        group: DEFAULT_GROUP

之所以需要配置 spring.application.name ,是因为它是构成 Nacos 配置管理 dataId字段的一部分。

在 Nacos Spring Cloud 中,dataId 的完整格式如下:

${prefix}-${spring.profiles.active}.${file-extension}
  • prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
  • spring.profiles.active 即为当前环境对应的 profile。
    • 注意:当 spring.profiles.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}
  • file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 propertiesyaml 类型。

在这里插入图片描述

  1. 配置自动更新@RefreshScope

    在这里插入图片描述

    @Value("${nacos.value}")
    private String nacosValue;
    
    @GetMapping("/value")
    public ResponseResult getValue() {
        return ResponseResult.ok(nacosValue);
    }
    

    这样是可以正常获取到zhangsan数据的,但是这时候从nacos配置中心修改成lisi,服务里是不会更新的,获取数据还是zhangsan

    @RestController
    @RefreshScope
    public class HealthController {
    
        @Value("${nacos.value}")
        private String nacosValue;
    
        @GetMapping("/value")
        public ResponseResult getValue() {
            return ResponseResult.ok(nacosValue);
        }
    }
    

    在需要自动更新的类加上@RefreshScope注解,就可以自动更新配置了

  2. 共享配置和扩展配置
    1. 共享配置(shared-configs)
    spring:
      application:
        name: sun-system
      profiles:
        # 环境配置
        active: dev
      cloud:
        nacos:
          config:
            # 配置文件后缀,常用yml,也有properties
            file-extension: yml
            # 命名空间,一般用来区分环境DEV
            namespace: 1d26e04f-aaba-4534-a16b-e25082642072
            # 配置中心地址
            server-addr: 101.43.51.245:8848
            # 分组,默认DEFAULT_GROUP
            group: DEFAULT_GROUP
            shared-configs[0]:
              data-id: common-dev.yml
              group: DEFAULT_GROUP
              refresh: true
    
    • shared-configs是一个数组,后面的数据越大说明优先级越高,shared-configs[1] > shared-configs[0]
    • data-id指定公共配置,需要带上后缀
    • group默认分组为DEFAULT_GROUP
    • refresh是否刷新默认为false,这里指定为true
    要在各应⽤之间共享⼀个配置,请使⽤上⾯的 shared-configs,因此按该理念,shared-configs指定的配置,本来应该是不指定group的,也就是应当归⼊DEFAULT_GROUP这个公共分组
    1. 扩展配置(extension-configs)
    • 拓展配置的用法同shared-configs,优先级高于shared-configs,也就是相同的属性会对其覆盖

    • 如果要在特定范围内(⽐如某个应⽤上)覆盖某个共享dataId上的特定属性,请使⽤ extension-config

    • ⽐如其他应⽤的数据库url,都是⼀个固定的url,使⽤shared-configs.dataId = mysql的共享配置,但其中有⼀个应⽤ddd-demo是特例,需要为该应⽤配置扩展属性来覆盖

    extension-configs[0]:
        data-id: common-dev.yml
        group: DEFAULT_GROUP
    	refresh: true
    
  3. 配置优先级

    优先级

    user-service-dev.yml(nacos上的当前环境配置) > user-service.yml(nacos上的)> application.yml(本地的)

在这里插入图片描述

实现本地配置优先
spring:
  cloud:
    config:
	  # 如果本地配置优先级高,那么 override-none 设置为 true,包括系统环境变量、本地配置文件等配置
      override-none: true
      # 如果想要远程配置优先级高,那么 allow-override 设置为 false,如果想要本地配置优先级高那么 allow-override 设置为 true
      allow-override: true
      # 只有系统环境变量或者系统属性才能覆盖远程配置文件的配置,本地配置文件中配置优先级低于远程配置;注意本地配置文件不是系统属性
      override-system-properties: false

注意:一定要配置到远程配置(nacos)上,否则不生效

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Cloud 是一个基于 Spring Boot 的框架,用于快速构建分布式系统的通用工具集。Nacos 是阿里巴巴开源的注册中心配置中心,而 Feign 是一个声明式的 HTTP 客户端,使得编写 Web 服务客户端变得更加简单。 下面是 Spring Cloud 项目搭建,并整合 Nacos 和 Feign 组件的步骤: 1. 创建父工程,添加以下依赖: ``` <dependencyManagement> <dependencies> <!-- Spring Cloud --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2020.0.1</version> <type>pom</type> <scope>import</scope> </dependency> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.4.4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 2. 创建子工程,添加以下依赖: ``` <!-- Nacos 注册中心配置中心 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2.2.3.RELEASE</version> </dependency> <!-- Feign 声明式 HTTP 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>3.0.2</version> </dependency> ``` 3. 在子工程的启动类上添加注解 `@EnableDiscoveryClient` 和 `@EnableFeignClients`,分别启用服务注册与发现和 Feign 组件。 ``` @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 4. 配置 Nacos 注册中心配置中心的地址,可以在 `application.properties` 文件中添加以下配置: ``` # Nacos 注册中心配置中心地址 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 spring.cloud.nacos.config.server-addr=127.0.0.1:8848 ``` 5. 创建 Feign 接口,使用 `@FeignClient` 注解指定要调用的服务名称和请求路径。 ``` @FeignClient(name = "service-provider") public interface HelloFeignClient { @GetMapping("/hello") String hello(); } ``` 6. 在需要使用 Feign 的地方注入 `HelloFeignClient` 对象,即可调用服务提供者的 `/hello` 接口。 ``` @RestController public class HelloController { @Autowired private HelloFeignClient helloFeignClient; @GetMapping("/hello") public String hello() { return helloFeignClient.hello(); } } ``` 至此,Spring Cloud 项目已经搭建完成,并整合Nacos 和 Feign 组件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值