使用Spring Cloud Alibaba创建springcloud项目-4.Nacos配置中心

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

创建一个名为config9003的服务

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloudstudy</artifactId>
        <groupId>com.study</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config9003</artifactId>


    <dependencies>
        <!--  SpringCloud alibaba nacos  config   -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

        <!--  SpringCloud alibaba nacos    -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--  web组件      -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.hmy.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


</project>

这里注意和其他不同的是用bootstrap.yml配置,因为bootstrap优先级比application更高。

bootstrap.yml

server:
  port: 9003
spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
        file-extension: yaml
  profiles:
    active: test

访问localhost:8848/nacos

使用Spring Cloud Alibaba创建springcloud项目-4.Nacos配置中心_第1张图片

使用Spring Cloud Alibaba创建springcloud项目-4.Nacos配置中心_第2张图片

配置内容:注意key和value之间要有个空格

创建主启动类ConfigMain9003:`

@EnableDiscoveryClient
@SpringBootApplication
public class NacosConfigClientMain3377 {
    public static void main(String[] args) {
        SpringApplication.run(NacosConfigClientMain3377.class,args);
    }
}

创建Controller

@RestController
public class ConfigController {

    @Value("${info}")
    String info;

    @GetMapping(value = "/config/getConfig")
    public String getConfig(){
        return info;
    }
}

启动程序,访问localhost:9003/config/getConfig,访问到配置中心内容

实时刷新

现在我们修改nacos上面的配置文件的配置内容,再次访问localhost:9003/config/getConfig会发现服务拿到的还是老数据,现在我们在ConfigController上加一个注解,如下

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${foo}")
    private String configInfo;

    @GetMapping("/config/version")
    public String getConfigInfo(){
        return configInfo;
    }
}

启动程序,访问localhost:9003/config/getConfig,修改配置内容,再次访问,内容实时刷新了

使用Spring Cloud Alibaba创建springcloud项目-5.Spring Cloud Stream & RabbitMQ

你可能感兴趣的:(使用Spring Cloud Alibaba创建springcloud项目-4.Nacos配置中心)