欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-21 14:02:46
官方文档:https://github.com/alibaba/spring-cloud-alibaba/wiki/Nacos-config创建一个名为config9003的服务pom.xml

官方文档: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配置中心

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

配置内容:注意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

本文地址:https://blog.csdn.net/weixin_42665587/article/details/107365748

相关标签: java