天天看点

springcloud alibaba 之 nacos在开始之前首先需要看一下 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

项目

  • 可以使用 https://start.aliyun.com/ 来创建
  • maven

    中加入依赖
<!--提供 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 Id Group 配置格式 配置内容
dev LC_GROUP YAML

spring.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 Id

    dev

    的配置表,将配置内容改为

    spring.profiles.active: test

    无需重启服务,直接刷新上一步的访问地址

到此

nacos

的动态配置已经完成

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

继续阅读