SpringCloudAlibaba 2021.0.5.0 集成Nacos2.2.0 集群配置中心使用记录

Nacos2.2.0集群配置中心使用记录,踩过太多坑
Nacos2.2.0集群搭建参考

1. Nacos配置中心使用

官方文档: https://github.com/alibaba/spring­cloud­alibaba/wiki/Nacos­config

1.1 准备配置


新建配置

powershell 复制代码
config:
    name: coisini
server:
  port: 9420

DataId:每个项目下有若干个微服务,每个配置集(DataId)是一个微服务的主配置文件

Group:代表某项目

Namespace:代表不同环境,如开发、测试、生产环境。

1.2 搭建nacos-config服务

1)引入依赖:

SpringCloud对应版本 https://github.com/alibaba/spring-cloud-alibaba/wiki/版本说明

powershell 复制代码
<!--Springboot版本管理-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.6.13</version>
                <type>pom</type>
            </dependency>
            <!--spring-cloud版本管理-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2021.0.5</version>
            </dependency>
            <!--spring-cloud-alibaba版本管理-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.0.5.0</version>
            </dependency>

     <!--引入nacos服务-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--引入nacos-config服务-->
        <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>
        </dependency>

2)项目添加application.yml bootstrap.yml文件


application.yml

powershell 复制代码
server:
  port: ${server.port}

bootstrap.yml

powershell 复制代码
spring:
    # 配置中心地址
  application:
    # 服务名称
    name: admin
  # 环境配置 例如 admin-dev.yaml
  #profiles:
  #  active: dev
  cloud:
    nacos:
      discovery:
        # 开启nacos作为服务注册中心,默认值:true
        enabled: true
        # nacos集群服务注册地址
        server-addr: 192.168.20.128:8999
        # nacos用户名
        username: nacos
        # nacos密码
        password: nacos
        # 命名空间,默认 public,可设置dev,pro等,相同特征的服务分类,先去nacos命名空间创建
        # namespace: public
        # 分组,默认 DEFAULT_GROUP 相同特征的服务划分的更细
        group: DEFAULT_GROUP
        # 临时实例,默认true,false永久实例,即使宕机也不会从nacos服务中删除,可应对雪崩保护,避免服务被删除
        ephemeral: true
        # 权重 1-100 数值越大权重越大分配的流量就越大,通常结合权重负载均衡策略
        weight: 100
      config:
        server-addr: ${spring.cloud.nacos.discovery.server-addr}
        username: ${spring.cloud.nacos.discovery.username}
        password: ${spring.cloud.nacos.discovery.password}
        # dataid为yaml的文件扩展名配置方式 ${spring.application.name}.${file‐extension:properties}
        file-extension: yaml
        # namespace:
        group: DEFAULT_GROUP
        context-path: /nacos
        # 共享配置
        #shared-configs:
        #  - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

3) 测试微服务是否使用配置中心的配置
启动类:

powershell 复制代码
@SpringBootApplication
// 服务注册发现
@EnableDiscoveryClient
 //开启 OpenFeign 功能
@EnableFeignClients
public class AdminApplication {
    public static void main(String[] args) throws InterruptedException {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(AdminApplication.class, args);
        while (true) {
            String name = applicationContext.getEnvironment().getProperty("config.name");
            String post = applicationContext.getEnvironment().getProperty("server.port");
            System.out.println("name :"+name);
            System.out.println("post :"+post);
            // 每一秒加载一次,查询注册中心配置是否变更
            TimeUnit.SECONDS.sleep(2);
        }
    }
}

新建controller 测试能否获取config.name值

powershell 复制代码
@RestController
// 当前类下的配置支持动态更新
@RefreshScope
@RequestMapping("/api/admin")
public class CsController {
    @Value("${config.name}")
    String name;

    @GetMapping("/cs")
    public String cs() {
        return name;
    }
}

启动测试:



搞定,感谢阅览~

END


相关推荐
老友@7 小时前
集中式架构、分布式架构与微服务架构全面解析
分布式·微服务·架构·系统架构
Jabes.yang16 小时前
Java求职面试实战:从Spring Boot到微服务架构的技术探讨
java·数据库·spring boot·微服务·面试·消息队列·互联网大厂
smilecold19 小时前
SpringCloud 入门 - Gateway 网关与 OpenFeign 服务调用
spring cloud·gateway
老王熬夜敲代码1 天前
Etcd使用
c++·微服务·etcd
编啊编程啊程1 天前
【004】生菜阅读平台
java·spring boot·spring cloud·dubbo·nio
岁岁岁平安1 天前
Java+SpringBoot+Dubbo+Nacos快速入门
java·spring boot·nacos·rpc·dubbo
金色天际线-1 天前
nginx + spring cloud + redis + mysql + ELFK 部署
redis·nginx·spring cloud
洛小豆1 天前
Swagger3学习与实践指南
spring boot·后端·spring cloud
奋斗的小monkey2 天前
Spring Boot 3.x核心特性与性能优化实战
java·spring boot·微服务·性能优化·响应式编程
Yeats_Liao2 天前
遗留系统微服务改造(一):遗留系统改造策略与实战场景分析
微服务·云原生·架构