天天看點

SpringCloud Config手動重新整理及自動重新整理

1、Config手動重新整理

a、使用@RefreshScope注解

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

/**
 * 這邊的@RefreshScope注解不能少,否則即使調用/refresh,配置也不會重新整理
 */
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${env}")
    private String env;

    @Value("${password}")
    private String password;

    @Value("${username}")
    private String username;

    @GetMapping("/config/profile")
    public String hello() {
        return this.env+","+this.password+","+this.username;
    }
}      

b、post請求config用戶端的/refresh端點

http://localhost:6062/refresh

再次通路http://localhost:6062/config/profile,發現配置檔案為最新配置。