9.Spring Cloud Config統一管理微服務配置

            Spring Cloud Config統一管理微服務配置

9.1. 爲何要統一管理微服務配置

9.2. Spring Cloud Config簡介

Spring Cloud Config爲分佈式系統中的外部配置提供服務器和客戶端支持。

使用Config Server,您能夠在全部環境中管理應用程序的外部屬性。客戶端和服務器上

的概念映射與Spring EnvironmentPropertySource抽象相同,所以它們與Spring應用

程序很是契合,但能夠與任何以任何語言運行的應用程序一塊兒使用。隨着應用程序經過

從開發人員到測試和生產的部署流程,您能夠管理這些環境之間的配置,並肯定應用程

序具備遷移時須要運行的一切。服務器存儲後端的默認實現使用git,所以它輕鬆支持標

籤版本的配置環境,以及能夠訪問用於管理內容的各類工具。很容易添加替代實現,並

使用Spring配置將其插入。

9.3. 編寫Config Server

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
        <groupId>com.itmuch.clod</groupId>
        <artifactId>spring-cloud-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  <artifactId>spring-cloud-config-servers</artifactId>
  <packaging>jar</packaging>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies> 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--  
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        -->
  </dependencies>
</project>

啓動類

package com.itmuch.cloud;

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

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

配置文件nginx

server:
  port: 8001
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cyj930307/spring-cloud-starter/     # 配置git倉庫的地址
#          search-paths: config-repo                              # git倉庫地址下的相對地址,能夠配置多個,用,分割。
#          username:                                              # git倉庫的帳號
#          password:                                              # git倉庫的密碼        
#security:
#  basic:
#    enabled: true
#  user:
#    name: user
#    password: 123456
#encrypt:
#  key: foo
   

git倉庫----》文件

profile: profile-default

訪問路徑

http://localhost:8001/abc-default.propertiesgit

git倉庫----》文件

profile: cyjfoorbar

訪問路徑

 

 

訪問路徑規則

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

其中「應用程序」做爲SpringApplication中的spring.config.name注入(即常規的Spring Boot應用程序中一般是「應用程序」),

「配置文件」是活動配置文件(或逗號分隔列表的屬性),「label」是可選的git標籤(默認爲「master」)。

9.4. 編寫Config Client

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
        <groupId>com.itmuch.clod</groupId>
        <artifactId>spring-cloud-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  <artifactId>spring-cloud-config-clients</artifactId>
  <packaging>jar</packaging>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies> 
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
  </dependencies>
 
  
</project>

配置文件

application.ymlweb

server:
  port: 8002

bootstrap.ymlspring

spring:
  cloud:
    config:
      uri: http://localhost:8001
      profile: dev
      label: master  #當configserver的後端存儲是git時,默認就是master
  application:
    name: foobak
#profile: abc

啓動類

package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 
 
@SpringBootApplication
 
public class ConfigClientApplication 
{
    public static void main( String[] args )
    {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

Controller類

package com.itmuch.cloud;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

訪問路徑:

 

9.5. Config Server的Git倉庫配置詳解

 

 

Git URI中的佔位符

Spring Cloud Config服務器支持一個Git倉庫URL,其中包含{application}{profile}(以及{label})的佔位符,

若是須要,請記住標籤應用爲git標籤)。所以,您可使用(例如)輕鬆支持「每一個應用程序的一個repo」策略:

配置文件的修改

server:
  port: 8001
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cyj930307/{application}
 

訪問路徑:

模式匹配和多個存儲庫

 

 還能夠經過應用程序和配置文件名稱的模式匹配來支持更復雜的需求。

模式格式是帶有通配符的{application}/{profile}名稱的逗號分隔列表(可能須要引用以通配符開頭的模式)。
apache

配置文件

server:
  port: 8001
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cyj930307/spring-cloud-starter
          repos:
            simple: https://gitee.com/cyj930307/simple
            special:
              pattern: special*/dev*,special*/test*
              uri: https://gitee.com/cyj930307/special
             

訪問路徑:

 

問題:

 

 

pattern: special*/dev*,special*/test* 匹配不到,就到上述的路徑 

若是{application}/{profile}不匹配任何模式,它將使用在「spring.cloud.config.server.git.uri」下定義的默認uri。

在上面的例子中,對於「簡單」存儲庫,模式是simple/*(即全部配置文件中只匹配一個名爲「簡單」的應用程序)。

「本地」存儲庫與全部配置文件中以「local」開頭的全部應用程序名稱匹配(將/*後綴自動添加到任何沒有配置文件匹配器的模式)。

搜索路徑

 

 

server:
  port: 8001
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cyj930307/spring-cloud-starter
          search-paths: 
              - foo  #路徑
              - bar  #路徑
             

  

 

 

 

 

9.6. Config Server的健康情況指示器

 

9.7. 配置內容的加解密

    9.7.1. 安裝JCE

                          要使用加密和解密功能,您須要在JVM中安裝全面的JCE(默認狀況下不存在)。bootstrap

                 您能夠從Oracle下載「Java加密擴展(JCE)無限強度管理策略文件」,並按照安裝說明後端

              (實際上將JRE lib / security目錄中的2個策略文件替換爲您下載的文件)。安全

    9.7.2. Config Server的加解密端點

 

 

             9.7.3. 對稱加密

                

server:
  port: 8003
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cyj930307/spring-cloud-starter
          username: 
          password: 

encrypt:
  key: foo
             

 

    9.7.4. 存儲加密後的內容

spring:
  datasource:
    username: dbuser
    password: '{cipher}FKSAJDFGYOS8F7GLHAKERGFHLSAJ'

.properties文件中的加密值不能用引號括起來,不然不會解密該值:服務器

spring.datasource.username: dbuser
spring.datasource.password: {cipher}FKSAJDFGYOS8F7GLHAKERGFHLSAJ

 

    9.7.5. 非對稱加密

命令

keytool -genkeypair -alias mytestkey -keyalg RSA \
  -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" \
  -keypass changeme -keystore server.jks -storepass letmein

 

在win下運行

緣由:

win下  \  不支持

keytool -genkeypair -alias mytestkey -keyalg RSA -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" -keypass changeme -keystore server.jks -storepass letmein

 在文件中會出現一個server.jks文件

server.jks文件放在類路徑(例如)中,而後在您的application.yml中配置服務器:

encrypt:
  keyStore:
    location: classpath:/server.jks
    password: letmein
    alias: mytestkey
    secret: changeme

 

                       9.7.6. 安全

服務端:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
        <groupId>com.itmuch.clod</groupId>
        <artifactId>spring-cloud-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  <artifactId>spring-cloud-config-servers-authc</artifactId>
  <packaging>jar</packaging>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies> 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
  </dependencies>
</project>

啓動類

package com.itmuch.cloud;

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

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

配置文件

security:
  basic:
    enabled: true
  user:
    name: user
    password: password123

server:
  port: 8001
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cyj930307/spring-cloud-starter
          username: 
          password: 
encrypt:
  key: foo

訪問路徑:

 客服端:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
        <groupId>com.itmuch.clod</groupId>
        <artifactId>spring-cloud-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  <artifactId>spring-cloud-config-clients-authc</artifactId>
  <packaging>jar</packaging>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies> 
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
  </dependencies>
 
  
</project>

配置文件

application.yml架構

server:
  port: 8002
  

bootstrap.yml

spring:
  cloud:
    config:
      uri: http://user:password123@localhost:8001
      profile: dev
      label: master  #當configserver的後端存儲是git時,默認就是master
  application:
    name: foobak
#profile: abc

啓動類

package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 
 
@SpringBootApplication
 
public class ConfigClientApplication 
{
    public static void main( String[] args )
    {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

Controller類

package com.itmuch.cloud;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

訪問路徑:

問題:

 

 配置的優先級高於路徑

9.8. 使用/refresh端點手動刷新配置

服務端:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
        <groupId>com.itmuch.clod</groupId>
        <artifactId>spring-cloud-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  <artifactId>spring-cloud-config-servers</artifactId>
  <packaging>jar</packaging>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies> 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--  
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        -->
  </dependencies>
</project>

配置文件

server:
  port: 8001
spring:
  application:
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cyj930307/spring-cloud-starter
          username: 
          password: 
 

啓動類

package com.itmuch.cloud;

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

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

客服端:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
        <groupId>com.itmuch.clod</groupId>
        <artifactId>spring-cloud-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  <artifactId>spring-cloud-config-clients-refresh</artifactId>
  <packaging>jar</packaging>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies> 
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-actuator</artifactId>
        </dependency>
  </dependencies>
 
  
</project>

配置文件  application.yml  ,bootstrap.yml

server:
  port: 8002


spring:
  cloud:
    config:
      uri: http://localhost:8001
      profile: dev
      label: master  #當configserver的後端存儲是git時,默認就是master
  application:
    name: foobak
#profile: abc
management:
  security:
    enabled: false

啓動類

package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 
 
@SpringBootApplication
 
public class ConfigClientApplication 
{
    public static void main( String[] args )
    {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

 

接口類

package com.itmuch.cloud;

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;

@RestController
@RefreshScope
class HelloController {

    @Value("${profile}")
    private String profile;
    
    @GetMapping("/profile")
    public String getProfile(){
        return this.profile;
    }
}

訪問路徑:

 

9.9. 使用Spring Cloud Bus實現自動刷新配置

    9.9.1. Spring Cloud Bus簡介

                         Spring Cloud Bus將分佈式系統的節點與輕量級消息代理連接。

               這能夠用於廣播狀態更改(例如配置更改)或其餘管理指令。一個關鍵

              的想法是,總線就像一個分佈式執行器,用於擴展的Spring Boot應用

             程序,但也能夠用做應用程序之間的通訊通道。目前惟一的實現是使用AMQP

             代理做爲傳輸,可是相同的基本功能集(還有一些取決於傳輸)在其餘傳輸的

             路線圖上。

    環境的配置:

   下載:RabbitMQ 和erlang

   

 

 

 

 

    9.9.2. 實現自動刷新

 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
        <groupId>com.itmuch.clod</groupId>
        <artifactId>spring-cloud-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  <artifactId>spring-cloud-config-clients-refresh-bus</artifactId>
  <packaging>jar</packaging>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies> 
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-actuator</artifactId>
        </dependency>
        <dependency>
             <groupId>org.springframework.cloud</groupId>
             <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
   
  </dependencies>
 
  
</project>

   配置文件

server:
  port: 8002



spring:
  cloud:
    config:
      uri: http://localhost:8001
      profile: dev
      label: master  #當configserver的後端存儲是git時,默認就是master
  application:
    name: foobak
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
management:
  security:
    enabled: false
 
 
 





  

啓動類

package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 
 
@SpringBootApplication
 
public class ConfigClientApplication 
{
    public static void main( String[] args )
    {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

Controller類

package com.itmuch.cloud;

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;

@RestController
@RefreshScope
class HelloController {

    @Value("${profile}")
    private String profile;
    
    @GetMapping("/profile")
    public String getProfile(){
        return this.profile;
    }
}

 

 

 

 9.9.3. 局部刷新

                      某些場景下(例如灰度發佈),咱們可能只想刷新部分微服務的配置,此時可經過/bus/refresh端點的destination參數來定位要刷新的應用程序。

                      例如:/bus/refresh?destination=customers:8000,這樣消息總線上的微服務實例就會根據destination參數的值來判斷是否須要要刷新。

                      其中,customers:8000指的是各個微服務的ApplicationContext ID。

                      destination參數也能夠用來定位特定的微服務。例如:/bus/refresh?destination=customers:**,這樣就能夠觸發customers微服務全部實例的配置刷新。

    9.9.4. 架構改進

    9.9.5. 跟蹤總線事件

                      一些場景下,咱們可能但願知道Spring Cloud Bus事件傳播的細節。此時,咱們能夠跟蹤總線事件(RemoteApplicationEvent的子類都是總線事件)。

                      跟蹤總線事件很是簡單,只需設置spring.cloud.bus.trace.enabled=true,這樣在/bus/refresh端點被請求後,訪問/trace端點就可得到相似以下的結果:

 

 

9.10. Spring Cloud Config與Eureka配合使用

   spring-cloud-eureka

1.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
        <groupId>com.itmuch.clod</groupId>
        <artifactId>spring-cloud-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  <artifactId>spring-cloud-eureka</artifactId>
  <packaging>jar</packaging>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies> 
        <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
  </dependencies>
 
  
</project>

配置文件

server:
 port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
        defaultZone: http://localhost:8761/eureka
   

啓動類

package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
 
 
@SpringBootApplication
@EnableEurekaServer

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

     spring-cloud-config-serve-eureka

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
        <groupId>com.itmuch.clod</groupId>
        <artifactId>spring-cloud-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  <artifactId>spring-cloud-config-servers-eureka</artifactId>
  <packaging>jar</packaging>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies> 
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
  </dependencies>
 
  
</project>

配置文件

server:
  port: 8050
spring:
  application:
    name: spring-cloud-config-server-eureka
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cyj930307/spring-cloud-starter/   
#         search-paths: config-repo 
          username:                                      
          password:                                       
eureka:
  client:
    service-url:
       defaultZone: http://localhost:8761/eureka
   

啓動類

package com.itmuch.cloud;

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

@EnableConfigServer
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigServerApplication {

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

  spring-cloud-config-clients-eureka

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
        <groupId>com.itmuch.clod</groupId>
        <artifactId>spring-cloud-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
  <artifactId>spring-cloud-config-clients-eureka</artifactId>
  <packaging>jar</packaging>
  
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  <dependencies> 
          <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  </dependencies>
 
  
</project>

配置文件

application.yml

server:
  port: 8051

bootstrap.yml

spring:
  application:
    name: foobak
  cloud:
    config:
      profile: dev
      label: master
      discovery:
        enabled: true                                 # 默認false,設爲true表示使用註冊中心中的configserver配置而不本身配置configserver的uri
        service-id: spring-cloud-config-server-eureka  # 指定config server在服務發現中的serviceId,默認爲:configserver
     
  
  
eureka:
  client:
    service-url:
       defaultZone: http://user:123456@localhost:8761/eureka
 
   

啓動類

package com.itmuch.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication 
{  
    public static void main( String[] args )
    {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

Controller文件

package com.itmuch.cloud;
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,配置也不會刷新
 * @author eacdy
 */
@RestController
@RefreshScope
class ConfigClientController {
    
    @Value("${profile}")
    private String profile;
    
    @GetMapping("/profile")
    public String getprofile(){
        return this.profile;
    }
}

訪問路徑

 

 

 

9.11. Spring Cloud Config的用戶認證

項目名稱:spring-cloud-config-servers-authc

配置文件:

server:
  port: 8001
spring:
  application:
    name: spring-cloud-config-server-basic
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/cyj930307/spring-cloud-starter
          username: 
          password: 
 
eureka:
  client:
    service-url:
       defaultZone: http://localhost:8761/eureka
       
security:
  basic:
    enabled: true               # 開啓基於HTTP basic的認證
  user:
    name: user                  # 配置登陸的帳號是user
    password: password123       # 配置登陸的密碼是password123
 

 啓動該服務,並輸入http://localhost:8001/microservice-foo/dev

9.12. Config Server的高可用

    9.12.1. Git倉庫的高可用

    9.12.2. RabbitMQ的高可用

    9.12.3. Config Server自身的高可用

                   Config Server的高可用也分爲2種:

                        1.Config Server未註冊到Eureka Server上 

                           這種狀況在Spring Cloud中稱之爲「Config First Bootstrap」。可使用一個負載均衡軟件(如nginx)來作高可用,Cloud Config Client URI指向Nginx。

                        2.Config Server註冊到了Eureka Server上

                           這種狀況,在Spring Cloud中稱之爲「Discovery First Bootstrap」。實現Config Server的高可用很簡單,只須要將多個Config Server註冊到Eureka server便可。

                           將Config Server做爲一個普通的微服務應用,歸入Eureka的服務治理體系中。

                            這樣咱們的微服務應用就能夠經過配置中心的服務名來獲取配置信息,這種方式比起傳統的實現模式來講更加有利於維護,

                           由於對於服務端的負載均衡配置和客戶端的配置中心指定都經過服務治理機制一併解決了,既實現了高可用,也實現了自維護。 

相關文章
相關標籤/搜索