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


相关推荐
鹏北海-RemHusband27 分钟前
从零到一:基于 micro-app 的企业级微前端模板完整实现指南
前端·微服务·架构
7哥♡ۣۖᝰꫛꫀꪝۣℋ32 分钟前
Spring-cloud\Eureka
java·spring·微服务·eureka
惊讶的猫5 小时前
OpenFeign(声明式HTTP客户端)
网络·网络协议·http·微服务·openfeign
鹏北海5 小时前
micro-app 微前端项目部署指南
前端·nginx·微服务
Java后端的Ai之路5 小时前
【Spring全家桶】-一文弄懂Spring Cloud Gateway
java·后端·spring cloud·gateway
indexsunny21 小时前
互联网大厂Java面试实战:Spring Boot微服务在电商场景中的应用与挑战
java·spring boot·redis·微服务·kafka·spring security·电商
CodeCaptain1 天前
nacos-2.3.2-OEM与nacos3.1.x的差异分析
java·经验分享·nacos·springcloud
vx_Biye_Design1 天前
【关注可免费领取源码】房屋出租系统的设计与实现--毕设附源码40805
java·spring boot·spring·spring cloud·servlet·eclipse·课程设计
Volunteer Technology1 天前
sentinel基本操作
spring cloud·sentinel