在分佈式系統中,每一個功能模塊都能拆分成一個獨立的服務,一次請求的完成,可能會調用很多個服務協調來完成,爲了方便服務配置文件統一管理,更易於部署、維護,所以就需要分佈式配置中心組件了,在spring cloud中,有分佈式配置中心組件spring cloud config,它支持配置文件放在在配置服務的內存中,也支持放在遠程Git倉庫裏。引入spring cloud config後,我們的外部配置文件就可以集中放置在一個git倉庫裏,再新建一個config server,用來管理所有的配置文件,維護的時候需要更改配置時,只需要在本地更改後,推送到遠程倉庫,所有的服務實例都可以通過config server來獲取配置文件,這時每個服務實例就相當於配置服務的客戶端config client,爲了保證系統的穩定,配置服務端config server可以進行集羣部署,即使某一個實例,因爲某種原因不能提供服務,也還有其他的實例保證服務的繼續進行。
一、新建一個maven項目:configServer
完整的pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<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.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gaox.configServer</groupId>
<artifactId>configServer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>configServer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在本地倉庫創建一個lisiService-dev.properties和lisiService-prod.properties的配置文件,然後推送到遠程的git倉庫(這裏貼出我在碼雲上新建的遠程倉庫https://gitee.com/fox9916/springCloudConfig),
lisiService-dev.properties內容如下:
name=lisi
age=18
version=dev
lisiService-prod.properties內容如下:
name=lisi
age=18
version=prod
完整的application.properties:
#服務端口
server.port=8091
#服務名稱
spring.application.name=configServer
#服務註冊中心
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#服務的git倉庫地址
spring.cloud.config.server.git.uri=https://gitee.com/fox9916/springCloudConfig
#配置文件所在的目錄
spring.cloud.config.server.git.search-paths=/**
#配置文件所在的分支
spring.cloud.config.label=master
#git倉庫的用戶名
spring.cloud.config.username=fox9916
#git倉庫的密碼
spring.cloud.config.password=********
在啓動類上開啓配置中心的註解:@EnableConfigServer
package com.gaox.configServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
服務端配置完成,下面驗證一下服務端的配置有沒有什麼問題,依次啓動serviceCenter、configServer,在瀏覽器裏輸入http://localhost:8091/lisiService/dev或
http://localhost:8091/lisiService-dev.properties或http://localhost:8091/master/lisiService-dev.properties後,返回內容:
{"name":"lisiService","profiles":["dev"],"label":null,"version":"86e81c9a9c191738398d7390efa7518a90e641c3","state":null,"propertySources":[{"name":"https://gitee.com/fox9916/springCloudConfig/lisiService-dev.properties","source":{"version":"dev","age":"18","name":"lisi"}}]}
從裏可以看到lisiService-dev.properties裏的內容,則說明配置沒有問題,如果返回內容類似,沒有isiService-dev.properties裏的內容,說明配置有問題,沒有讀取到配置文件。
證明配置服務中心可以從遠程程序獲取配置信息,http請求地址和資源文件映射如下:,可參考
· /{application}/{profile}[/{label}]
· /{application}-{profile}.yml
· /{label}/{application}-{profile}.yml
· /{application}-{profile}.properties
· /{label}/{application}-{profile}.properties
其中application爲微服務名,也可以就是“application”,“application”代表公共配置。
profile代表dev,test,pro
label代表分支,如master
二、對上一篇文章中新建的lisiService作一些改動,驗證一下是否能從遠程倉庫裏讀取到配置內容
在pom.xml裏添加配置:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
在resources目錄下新建文件bootstrap.properties,內容:
#開啓配置服務發現
spring.cloud.config.discovery.enabled=true
#配置服務實例名稱
spring.cloud.config.discovery.service-id=configServer
#配置文件所在分支
spring.cloud.config.label=master
spring.cloud.config.profile=prod
#配置服務中心
spring.cloud.config.uri=http://localhost:8091/
Application.properties配置內容不變:
server.port=8765
spring.application.name=lisiService
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
feign.hystrix.enabled=true
LisiServerApplication.java:
package com.gaox.lisiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@RestController
@EnableHystrixDashboard
@EnableCircuitBreaker
public class LisiServiceApplication {
public static void main(String[] args) {
SpringApplication.run(LisiServiceApplication.class, args);
}
@FeignClient(value = "helloService" ,fallback = HelloError.class)
public interface HelloService {
@RequestMapping(value = "/hello", method = RequestMethod.GET)
String hello(@RequestParam("name") String name);
}
@Component
public class HelloError implements HelloService {
@Override
public String hello(String name){
return "hello ,"+name+"! sorry ,error !";
}
}
@Autowired
private HelloService helloService;
@RequestMapping("/hello")
public String hello(@RequestParam("name")String name){
return helloService.hello(name);
}
@Value("${name}")
private String name;
@Value("${age}")
private String age;
@Value("${version}")
private String version="開發環境";
@RequestMapping("/test")
public String test(){
return "你好,我是"+name+",年齡:"+age+"歲。當前環境:"+version;
}
}
依次啓動serviceCenter ,configService, lisiService ,打開瀏覽器訪問:Http://localhost:8765/test,返回內容:
你好,我是lisi,年齡:18歲。當前環境:prod
這說明lisiService通過configService配置中心獲取到了遠程倉庫spingCloudConfig的配置文件內容