Spring Cloud Config為服務端和用戶端提供了分布式系統的外部化配置支援,配置服務中心采用Git的方式存儲配置檔案,
是以我們很容易部署修改,有助于對環境配置進行版本管理。
一、配置中心
在根目錄spring_cloud中建立Maven Moudle子產品: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">
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.sam</groupId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
建立消費者服務啟動類:ConfigApplication
package com.sam.config.server;
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;
/**
* @ClassName: ConfigApplication
* @Description: 配置中心服務
* @author sam
* @date 2018年8月10日 下午3:49:01
*/
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
建立配置檔案:application.yml
server:
port: 8030
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/ #eureka服務注冊位址
# git管理配置
spring:
cloud:
config:
server:
git:
uri: https://github.com/vtopqx/config/ #git倉庫位址
searchPaths: demo* #搜尋路徑
# username: username
# password: password
application:
name: config-server
management:
security:
enabled: false #是否開啟actuator安全認證
在配置的git倉庫下建立一個demo1的檔案夾,在裡面建立一個叫client-a-dev.properties的配置檔案
檔案中随便加上兩個配置
啟動子產品,然後打開 http://localhost:8030/client-a/dev 可以看到相應的配置資訊:
{"name":"client-a","profiles":["dev"],"label":"master","version":"2029351dd3871b1284b0b8533755ccec90531be7","state":null,"propertySources":[{"name":"https://github.com/vtopqx/config/demo1/client-a-dev.properties","source":{"port":"8899","ip":"192.168.1.300"}}]}
二、配置用戶端讀取
這裡建立一個新的消費者服務來測試,
在根目錄spring_cloud中建立Maven Moudle子產品:service-consumer-config
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">
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.sam</groupId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-consumer-ribbon</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<!-- 使用配置中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
建立配置檔案:application.yml
server:
port: 8913
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8010/eureka/
spring:
application:
name: client-a
cloud:
config:
discovery:
enabled: true #開啟通過服務來通路Config Server的功能
service-id: config-server
profile: dev
label: master
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
在TestController添加測試方法
package com.sam.service.consumer.config;
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.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @ClassName: TestController
* @Description: [測試]配置中心
* @author mqx
* @date 2018年8月10日 下午2:56:02
*/
@RestController
@RefreshScope
public class TestController {
@Autowired
RestTemplate restTemplate;
@RequestMapping("/hi")
public String hi(@RequestParam String id) {
return restTemplate.getForObject("http://service-producer/hi?id=" + id, String.class);
}
@Value("${ip}")
private String ip;
@Value("${port}")
private String port;
@RequestMapping("/getProperties")
public String getProperties() {
return ip + " : " + port;
}
}
配置完成後,啟動子產品後打開 http://localhost:8913/getProperties
表示配置檔案讀取成功,通過以上配置,即可實作程式配置檔案分離!
PS:通過以上配置是可以讀取配置檔案,不過如果修改配置檔案的話,還需要重新開機服務才行,下一篇會實作動态配置,解決配置修改重新開機問題!