【springCloud基礎篇-11】Spring Cloud Config 配置中心之refresh與SVN示例(他人的博客地址)

接上篇文章。

demo代碼地址:https://download.csdn.net/download/qq_33333654/12014918

SVN示例的博客地址:http://www.ityouknow.com/springcloud/2017/05/23/springcloud-config-svn-refresh.html

refresh:

客戶端client項目添加依賴:

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

修改配置文件(application.yml)注意關閉權限:

server:
  port: 9021
eureka:
  client:
    service-url:
      defaultZone: http://peer1:8000/eureka/,http://peer2:8001/eureka/,http://peer3:8002/eureka/
spring:
  application:
    name: spring-cloud-config-client
management:
  security:
    enabled: false #springboot 1.5.X 以上默認開通了安全認證

 

控制器添加註解@RefreshScope:

package com.example.spirngcloudconfigclient.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ProjectName: spirng-cloud-config-client
 * @Package: com.example.spirngcloudconfigclient.web
 * @ClassName: HelloController
 * @Author: MC
 * @Description: ${description}
 * @Date: 2019/11/15 0015 11:45
 * @Version: 1.0
 */
@RestController
@RefreshScope // 使用該註解的類,會在接到SpringCloud配置中心配置刷新的時候,自動將新的配置更新到該類對應的字段中。
public class HelloController {

    @Value("${native.hello}")
    private String hello;

    @RequestMapping("/hello")
    public String from() {
        return this.hello;
    }
}

 

測試:

先訪問http://localhost:9021/hello

返回的是hello_i_im_mysql

 修改server項目中的mysql配置文件:

native.hello=hello_i_im_mysql_update

重啓server項目。

cmd命令執行:curl -X POST http://localhost:9021/refresh返回結果如圖:

再次訪問瀏覽器:http://localhost:9021/hello

 

OK

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章