1. 介紹
Spring Cloud Config為分布式系統(tǒng)中的外部配置提供服務(wù)器和客戶(hù)端支持。使用Config Server况凉,您可以在所有環(huán)境中管理應(yīng)用程序的外部屬性。客戶(hù)端和服務(wù)器上的概念映射與SpringEnvironment和PropertySource抽象相同,因此它們與Spring應(yīng)用程序非常契合媳叨,但可以與任何以任何語(yǔ)言運(yùn)行的應(yīng)用程序一起使用议双。隨著應(yīng)用程序通過(guò)從開(kāi)發(fā)人員到測(cè)試和生產(chǎn)的部署流程,您可以管理這些環(huán)境之間的配置赔蒲,并確定應(yīng)用程序具有遷移時(shí)需要運(yùn)行的一切。服務(wù)器存儲(chǔ)后端的默認(rèn)實(shí)現(xiàn)使用git损趋,因此它輕松支持標(biāo)簽版本的配置環(huán)境,以及可以訪(fǎng)問(wèn)用于管理內(nèi)容的各種工具椅寺。很容易添加替代實(shí)現(xiàn)浑槽,并使用Spring配置將其插入蒋失。
2. 引入pom相關(guān)jar包,其中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.0?http://maven.apache.org/xsd/maven-4.0.0.xsd">??
<modelVersion>4.0.0</modelVersion>??
<parent>??
<groupId>com.ml.honghu</groupId>??
<artifactId>commonservice</artifactId>??
<version>0.0.1-SNAPSHOT</version>??
</parent>??
<artifactId>commonservice-config</artifactId>??
<packaging>jar</packaging>??
<name>commonservice-config</name>??
<description>Config?Server</description>??
<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>??
<dependency>??
<groupId>org.springframework.boot</groupId>??
<artifactId>spring-boot-starter-security</artifactId>??
</dependency>??
<dependency>??
<groupId>org.springframework.boot</groupId>??
<artifactId>spring-boot-starter-test</artifactId>??
<scope>test</scope>??
</dependency>??
</dependencies>??
<build>??
<plugins>??
<plugin>??
<groupId>org.springframework.boot</groupId>??
<artifactId>spring-boot-maven-plugin</artifactId>??
<executions>??
<execution>??
<id>1</id>??
<goals>??
<goal>repackage</goal>??
</goals>??
</execution>??
<execution>??
<id>2</id>??
<goals>??
<goal>build-info</goal>??
</goals>??
</execution>??
</executions>??
</plugin>??
</plugins>??
</build>??
</project>??
?3. 在src/main/java進(jìn)行ConfigApplication.java啟動(dòng)文件配置:
package?com.ml.honghu;??
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;??
@EnableConfigServer??
@EnableEurekaClient??
@SpringBootApplication??
public?class?ConfigApplication?{??
public?static?void?main(String[]?args)?{??
SpringApplication.run(ConfigApplication.class,?args);??
????}??
}??
?4. 在src/main/resource下進(jìn)行bootstrap.yml配置
server:? ?
port:8888??
spring:??
??application:??
????name:?commonservice-config-server??
??profiles:??
active:?discovery,native??
??cloud:??
????config:??
??????server:??
????????git:??
uri:?http://192.168.0.254/honghu.../honghu-config.git??
??????????username:?honghu??
password:123456??
??????????searchPaths:?config-dev??
security:??
??basic:??
enabled:true??
??user:??
????name:?honghu??
password:123456??
eureka:??
??client:??
????serviceUrl:??
defaultZone:?http://honghu:123456@localhost:8761/eureka/??
honghuZone:?http://honghu:123456@localhost:8761/eureka/??
registry-fetch-interval-seconds:300??
????availability-zones:??
??????honghu:?honghuZone??
??instance:??
prefer-ip-address:true??
????metadataMap:??
version:1.0??
??????variant:?A??
??????user:?${security.user.name}??
??????password:?${security.user.password}??
management:??
??security:??
enabled:false??
?注意: 如果不從遠(yuǎn)程git或者svn庫(kù)加載配置文件信息括荡,可以配置加載本地地址高镐,比如window下配置使用:?
server:???
port:8888??
spring:??
??application:??
????name:?commonservice-config-server??
??profiles:??
active:?discovery,native??
??cloud:??
????config:??
??????server:??
security:??
??basic:??
enabled:true??
??user:??
????name:?honghu??
password:123456??
eureka:??
??client:??
????serviceUrl:??
defaultZone:?http://honghu:123456@localhost:8761/eureka/??
honghuZone:?http://honghu:123456@localhost:8761/eureka/??
registry-fetch-interval-seconds:300??
????availability-zones:??
??????honghu:?honghuZone??
??instance:??
prefer-ip-address:true??
????metadataMap:??
version:1.0??
??????variant:?A??
??????user:?${security.user.name}??
??????password:?${security.user.password}??
management:??
??security:??
enabled:false??
?到此,整個(gè)config服務(wù)項(xiàng)目配置完畢;濉<邓琛(企業(yè)架構(gòu)源碼可以加求球:三五三六二四七二五九)
從現(xiàn)在開(kāi)始,我這邊會(huì)將近期研發(fā)的spring cloud微服務(wù)云架構(gòu)的搭建過(guò)程和精髓記錄下來(lái)邑闲,幫助更多有興趣研發(fā)spring cloud框架的朋友算行,大家來(lái)一起探討spring cloud架構(gòu)的搭建過(guò)程及如何運(yùn)用于企業(yè)項(xiàng)目。