spring cloud config 配置中心(一)

微服務的基礎服務之一,能夠使用Apollo 代替,後續補充,也能夠本身搭建 https://github.com/ctripcorp/apollogit

spring 基礎功能不作簡介,只說關鍵點:github

第一步:依賴包spring

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

第二步:啓動類,加註釋,熟悉的套路很少說bootstrap

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

第三步:配置文件(方便測試採用的本地git庫,遠程的話須要配置帳戶密碼)安全

### 配置服務器配置項
spring.application.name = config-server
### 定義HTTP服務端口
server.port = 9090
### 本地倉庫的GIT URI 配置,正式環境本身切換成遠程git倉庫
spring.cloud.config.server.git.uri = file:///E:/config

第四步:上傳配置文件服務器

第五步:客戶端引入依賴,啓動類配置註解同server端app

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

第六步:客戶端配置spring-boot

bootstrap.properties微服務

### bootstrap 上下文配置
# 配置服務器 URI
spring.cloud.config.uri = http://localhost:9090/
# 配置客戶端應用名稱:{application}
spring.cloud.config.name = application
# profile 是激活配置
spring.cloud.config.profile = dev
# label 在Git中指的分支名稱
spring.cloud.config.label = master

說明:name+profile+label 來定位git中配置文件的位置,別搞錯測試

完工。是否是很簡單

開始測試

/**
 * @author hht
 * @ClassName TestConfigController
 * @Description TODO
 * @Date 2019/4/25 18:40
 * @VERSION 1.0
 * RefreshScope 用來刷新環境變量  @Value
 */
@RefreshScope  
@RestController
public class TestConfigController {

    @Value("${my.name}")
    private String name;

    @GetMapping("/getMyname")
    public String getMyname(){
        return name;
    }
}

成功截圖

補充:客戶端如何刷新環境變量,參考上一步代碼經過  @RefreshScope   和   ContextRefresher 的refresh()方法,這裏用的定時器拉取作的測試,固然生產環境能夠結合spring cloud bus實現事件驅動更新

@SpringBootApplication
@EnableScheduling
public class SpringCloudConfigClientApplication {

    private ContextRefresher refresher;

    public  SpringCloudConfigClientApplication(ContextRefresher refresher){
        this.refresher = refresher;

    }

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

    /**
     * @Author chengpunan
     * @Description 初始化後延時 3秒運行 ,而後 每隔 5秒 執行一次
     * @Date 16:24 2019/4/29
     * @Param []
     * @return void
     **/
    @Scheduled(fixedRate = 5 * 1000,initialDelay = 3 * 1000)
    public void autoRefresh(){
        System.out.println(new Date());
        refresher.refresh();
    }

}

注意:爲了方便測試不要引入spring-boot-starter-actuator

若是引入  server端添加

### 全局關閉 Actuator 安全
# management.security.enabled = false
### 細粒度的開放 Actuator Endpoints
### sensitive 關注是敏感性,安全
endpoints.env.sensitive = false
endpoints.health.sensitive = false

client端添加

### 全局關閉 Actuator 安全
management.security.enabled = false
### 細粒度的開放 Actuator Endpoints
### sensitive 關注是敏感性,安全
endpoints.env.sensitive = false
endpoints.refresh.sensitive = false
endpoints.beans.sensitive = false
endpoints.health.sensitive = false
endpoints.actuator.sensitive = false
相關文章
相關標籤/搜索