SpringCloud七:配置中心Eureka+Config+Bus+RabbitMQ

隨着線上項目變的日益龐大,每個項目都散落着各種配置文件,如果採用分佈式的開發模式,需要的配置文件隨着服務增加而不斷增多。某一個基礎服務信息變更,都會引起一系列的更新和重啓,運維苦不堪言也容易出錯.SpringCloud Config 就可以解決該問題.
本文闡述SpringCloud配置中心和配置客戶的架構
爲了配置中心的高可用和服務化,使用Eureka作爲註冊中心,並把配置中心註冊到Eureka註冊中心,此時就可以多開幾個配置中心來高可用集羣,
爲了把配置的變更刷新到客戶端中的任務交給配置中而不是客戶端,使用SpringCloud Bus+RabbitMQ
下面先闡述配置中心的搭建

配置中心

在pom.xml中引入依賴
springboot的版本爲1.5.15
springcloud的版本爲Edgware.SR4


		<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-bus-amqp</artifactId>
	</dependency>
		<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>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

與spring-cloud相關的屬性必須配置在bootstrap.yml中,config部分內容才能被正確加載。因爲config的相關配置會先於application.yml,而bootstrap.properties的加載也是先於application.yml,主要配置了git
bootstrap.yml

server:
  port: 5001
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/lhc0512/config.git  
          search-paths:                          
          username: username                                    
          password: password

在application.yml中配置rabbitmq,
注意配置management.security.enabled=false,
允許使用window的 curl -X POST http://localhost:5001/bus/refresh 更新消息總線

eureka:
  instance: 
    prefer-ip-address: true
    instance-id: config-server:5001
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/ 
management:
  security:
     enabled: false   
spring: 
  rabbitmq:
    host: 120.73.233.104
    port: 5672
    username: guest
    password: guest
     

在啓動類中使用@EnableConfigServer使之爲註冊中心server,並註冊到eureka中

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

啓動後
我在github上創建倉庫https://github.com/lhc0512/config.git
並創建文件config-dev.properties"
想訪問config-dev.properties的詳細信息
訪問http://localhost:5001/config/dev
內容如下

{
"name": "config",
"profiles": [
"dev"
],
"label": null,
"version": "25abae735e19b2c0ac2e975cd0e112083fae1204",
"state": null,
"propertySources": [
{
"name": "https://github.com/lhc0512/config.git/config-dev.properties",
"source": {
"hello": "hello dev"
}
}
]
}

想訪問config-dev.properties的內容
訪問http://localhost:5001/config-dev.properties
內容如下

hello: hellodev8

更改github的文件,會發現服務端自動更新

client

接下來闡述客戶端的搭建
在pom.xml引入依賴

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</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>
	

由於bootstrap.yml會先加載,springcloud config 都配置在這裏,主要配置了rabbitmq 開啓springcloud bus的消息跟蹤,並註冊爲eureka服務
bootstrap.yml

spring:
  application:
    name: config-client
  rabbitmq:
    host: 120.77.245.104
    port: 5672
    username: guest
    password: guest
  cloud:
    bus:
      trace:
        enabled: true #消息跟蹤
    config: 
      name: config #github上的文件名
      profile: dev #運行環境
      label: master #分支 
      discovery:  
        enabled: true #發現服務
        service-id: config-server  #服務名
eureka: 
  instance:
    prefer-ip-address: true
    instance-id: config-client:5011
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka/
server:
  port: 5011

在啓動類中,開啓服務發現@EnableDiscoveryClient

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

在controller寫RestAPI暴露配置的信息

@RefreshScope
@RestController
public class ConfigController {

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

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

終於把客戶端的搭建講完了,下面終於可以說一下配置更新一事.
之前更改github上的文件,配置中心更新了,但會發現,客戶端並沒有更新,使用SpringCloud Bus+RabbitMQ的意義在於,把客戶端更新的任務交給配置中心,此時想讓客戶端更新很簡單
使用如下方式讓客戶端更新,在window的cmd中,輸入如下的hook

curl -X POST http://localhost:5001/bus/refresh

會發現調用http://localhost:5011/getHello的restAPI,已經更新

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