Config的手动刷新(refresh端点)

1、Config-Server使用之前的最基本的Config-Server的配置

pom.xml

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

application.yml

server:
  port: 8080
spring:
  cloud:
    config:
      server:
        git:
          uri: git@github.com:wangchunself/ymlforconfig.git

启动类

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

}

2、手动刷新只需要在Config-Client端进行特殊的配置即可

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

bootstrap.yml

此处要暴露出/refresh端点

server:
  8081
spring:
  cloud:
    config:
      uri: http://localhost:8080
      label: master
      profile: bar
  application:
    name: foo
management:
  endpoints:
    web:
      exposure:
        include: refresh

启动类

@SpringBootApplication
public class ConfigClientRefreshApplication {

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

}

调用类

添加@RefreshScope注解,这个注解是能够保证,我们刷新不会影响到正在进行的业务

不能和@Configration在同一个类的注解上

@RestController
@RefreshScope
public class ConfigClientController {
    @Value("${profile}")
    String profile;
    @GetMapping("/getProfile")
    public String getProfile(){
        return this.profile;
    }
}

3、测试

启动ConfigServer和Client,

访问http://localhost:8081/getProfile

修改https://github.com/wangchunself/ymlforconfig仓库下,foo-bar.yml文件为

post请求http://localhost:8081/actuator/refresh(图中结果表示,profile属性更改刷新了)

再次访问http://localhost:8081/getProfile

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值