记录使用spring-cloud-starter-alibaba-nacos-config遇到的问题(nacos 2.1)【2022.06】

遇到的问题

使用nacos作为配置中心,字段没有被负值
@Value报错
GrpcClient报错
字段刷新

解决方案

1. 确定nacos的版本、端口开通{8848、9848}

localhost:8848 登陆成功后左上角显示 nacos 版本,此处以2.1为例
在这里插入图片描述
docker命令,注意docker宿主机此处端口必须是 1000 的偏移量

docker run --env MODE=standalone --name nacos -d -p 8848:8848 -p 9848:9848 -v /home/docker-data/nacos-data/logs:/home/nacos/logs -v /home/docker-data/nacos-data/conf:/home/nacos/conf nacos/nacos-server
2. 修改pom文件依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2021.1</version>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba.nacos/nacos-client -->
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

说明:

  1. spring-cloud-starter-alibaba-nacos-config 中的nacos-client 版本过低,选择使用 nacos 服务端匹配的版本
  2. spring-cloud-starter-bootstrap用于加载 NacosConfigBootstrapConfiguration 配置类,开启访问配置中心的资源文件并开启刷新字段的监听器
  3. nacos-client的 2.x版本 有grpc的优化,这里就是需要上面端口1000 的偏移量:9848
3. 增加bootstrap.yml配置

由于 加载nacos的configuration优先级很高,application.yml满足不了,必须使用bootstrap.yml来配置访问nacos服务端的配置信息

bootstrap.yml

spring:
  application:
    name: global
  cloud:
    nacos:
      config:
        server-addr: ip:8848
        group: DEFAULT_GROUP
        username: nacos
        password: nacos
        namespace: b14aef81-2b0e-472d-8b79-e98e4582c3d8
        enabled: true
        extension-configs:
          - dataId: global.properties
            group: DEFAULT_GROUP
            refresh: true
          - dataId: global_common.properties
            group: DEFAULT_GROUP
            refresh: true

ext-config 以过时,使用extension-configs。
读取配置文件的顺序:

源码:

loadSharedConfiguration(composite);
loadExtConfiguration(composite);
loadApplicationConfiguration(composite, dataIdPrefix, nacosConfigProperties, env);

loadSharedConfiguration 对应配置 shared-configs

loadExtConfiguration 对应配置 本地+ extension-configs + spring.application.name。
以当前配置为例 读取nacos服务端三次,分别是 global.properties、global_common.properties、global

loadApplicationConfiguration中又读取了一次global.properties,以设置的profile 修改dataid放到 Set<PropertySource<?>> propertySources 集合中,源码如下:

		for (String profile : environment.getActiveProfiles()) {
			String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension;
			loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup,
					fileExtension, true);
		}
4. 修改类文件注解和刷新字段逻辑
package com.example.springcloudgateway7050;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/config")
@RefreshScope
public class HelloController {


    @Value("${first_global_config:123}")
    private String first_global_config;

    @Value("${hello_global_config:321}")
    private String hello_global_config;

    private ConfigurableApplicationContext context;

    @RequestMapping("/sayHello")
    public String sayHello() {
        return first_global_config+ " | " + hello_global_config;
    }

    @RequestMapping("/sayRefreshHello")
    public String sayRefreshHello() {
        return context.getEnvironment().getProperty("first_global_config") + "  ||  " +
                context.getEnvironment().getProperty("hello_global_config");
    }


    @Autowired
    public void context(ConfigurableApplicationContext context) {
        this.context = context;
    }

}

@Value 遵从spring就可以

  1. 类上增加@RefreshScope注解 ,自动刷新@Value标识的属性
    配置文件中的 refresh: true ,自动刷新
  2. ConfigurableApplicationContext.getEnvironment().getProperty(“first_global_config”)中的属性

至于怎么刷新的大家看下源码就知道了,有时间再细讲。
(ps: 有问题请再评论留言)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值