SpringCloud分佈式微服務搭建(三)

本例子是一個springcloud的configserver,client例子git

利用git存儲各個服務的配置文件github

server獲取配置文件的倉庫位置,並把server註冊到eureka中,同時爲了實現HA,多開幾個serverweb

client經過server得到git的地址,運行時利用得到的git配置文件來配置服務自身。spring

這樣子服務的配置修改更加方便json

git上的配置文件倉庫地址:https://github.com/linjiaqin/springcloud-config-repobootstrap

 

本例子代碼:https://github.com/linjiaqin/scdemoapp

一.ConfigServer端的配置 spring-boot

1.引導類spa

package com.ljq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigserverApplication {

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

}

 2.配置文件code

server.port=9000
spring.application.name=config-server-9000
#表示配置中心所在倉庫的位置
spring.cloud.config.server.git.uri=https://github.com/linjiaqin/springcloud-config-repo.git
#倉庫路徑下的的相對搜索位置,能夠配置多個
spring.cloud.config.server.git.search-paths=scdemo
#git的用戶名
spring.cloud.config.server.git.username=×××××××
#git的密碼
spring.cloud.config.server.git.password=×××××××

 3.能夠直接看server獲取git上配置文件的json效果

4. 爲了實現高可用,開啓兩個configserver

 已經寫入start-all.sh的一鍵腳本中

#開啓兩個config server
cd /home/linjiaqin/log_stream_platform/source/scdemo/configserver
nohup mvn spring-boot:run -Dserver.port=40001 > /dev/null 2>&1 &
nohup mvn spring-boot:run -Dserver.port=40002 > /dev/null 2>&1 &

5. 必須先開啓configserver,才能開啓configclient,configclient獲取到server的地址以後就無所謂了

 

二.客戶端

任何一個服務均可以是configclient

經過eureka獲取到configserver的serverID,而後找到一個server地址去獲取到git的地址,label指定了master分支,profile指定了dev配置文件

而後運行這個configclient的時候就會按照獲取到的git上配置文件來運行

1.下面這個本地配置文件必須名爲bootstrap

#配置服務名
spring.application.name=config-client
#服務id
server.port=50000
#配置對應文件規則中的{profile}部分
spring.cloud.config.profile=dev
#配置對應文件規則中的{label}
spring.cloud.config.label=master
#配置中心的地址,有了eureka以後再也不手動配置
#spring.cloud.config.uri=http://localhost:9000/
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server

eureka.client.serviceUrl.defaultZone=http://mu01:8761/eureka,http://cu01:8762/eureka,http://cu02:8763/eureka

 2.Controller

package com.ljq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class HelloController {
    //value註解的做用是得到配置文件上的對應的key
    @Value("${Parameter}")
    private String Parameter;

    @Value("${server.port}")
    private String port;
    @Autowired
    private Environment environment;
    @GetMapping("/get_name")
    public String name(){
        return "Parameter:"+Parameter;
    }
    @GetMapping("/get_port")
    public String port(){
        return "Port:"+port;
    }
    @GetMapping("/get_name_env")
    public String name_env(){
        return environment.getProperty("Parameter","undefine");
    }
}

 

 

 

 

3. git上的dev配置文件

Parameter:dev
spring.application.name=config-client-by-git-dev-properties
#服務id
server.port=50001

所以啓動configclient時是以這個配置文件啓動的,優先級比本地的要高


 

相關文章
相關標籤/搜索