springcloud alibaba 之 nacos

想从 springcloud中切换到 springcloud alibaba本文可以起到指引作用

头号大事是服务注册发现,在选型上我优先考虑了 consul弄完之后发现没有传说中的那么香,于是乎换成了 nacos

项目源码

下面是集成 nacos的过程

在开始之前首先需要看一下 nacos入门

一、搭建 nacos服务

  • 运行以下命令使用 docker来虚拟化服务
git clone https://github.com/nacos-group/nacos-docker.git

docker-compose -f nacos-docker/example/standalone-derby.yaml up -d

以上运行完成后会在本地 8848启动一个 nacos服务

  • 控制面板访问 http://localhost:8848/nacos默认账户和密码是 nacos
  • nacos默认 registry的类型是 file用默认的就行

二、创建一个 springboot项目

<!--提供 springboot 监控指标暴露-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--提供 nacos 服务注册发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--提供 nacos 动态配置-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

三、实现动态配置

  • 登录 nacos
    nacos中增加一个配置列表
Data IdGroup配置格式配置内容
devLC_GROUPYAMLspring.profiles.active: dev
  • 在创建的项目中配置
    因为 springboot的设定是 bootstrap.yml最高优先级,所以创建一个 bootstrap.yml,写入如下内容
spring:
  application:
    name: nacos-consumer
  cloud:
    nacos:
      config:
        server-addr: localhost:8848  #Nacos服务器侦听器的IP和端口 #80不能省略
        file-extension: yaml
        group: LC_GROUP  # 与Nacos配置的组名相同 默认是 DEFAULT_GROUP
        timeout: 3000   # 从nacos获取配置超时
        name: dev  #对应配置的data名
  • 在启动入口上加上 @EnableDiscoveryClient注解
@EnableDiscoveryClient
@SpringBootApplication
public class NacosConsumerApplication{

	public static void main(String[] args) {
		SpringApplication.run(NacosConsumerApplication.class, args);
	}

        @LoadBalanced
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
}

四、测试动态配置

  • 代码
@RestController
@RequestMapping("/config")
@RefreshScope //Nacos动态刷新配置 需要热加载的bean需要加上@RefreshScope注解,当配置发生变更的时候可以在不重启应用的前提下完成bean中相关属性的刷新
public class ConfigController {

    @Value("${spring.profiles.active}")
    String active;
    @Autowired
    RestTemplate restTemplate;

    /**
     * http://localhost:8080/config/get
     */
    @GetMapping("get")
    public String get(@RequestParam(required = false) String str) {
        return active + " | " + str;
    }

    /**
     * http://localhost:8080/config/rest
     */
    @SentinelResource("ss")
    @GetMapping("rest")
    public String rest() {
        return restTemplate.getForObject("http://nacos-provider/config/get/" + "?str=consumerSend", String.class);
    }
}
  • 启动项目后 访问 http://localhost:8080/config/get
  • 更改原先在 nacos增加的 Data Iddev的配置表,将配置内容改为 spring.profiles.active: test无需重启服务,直接刷新上一步的访问地址

到此 nacos的动态配置已经完成

欢迎关注我的个人公众号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值