- 介紹
Spring Cloud Config為分布式系統中的外部配置提供伺服器和用戶端支援。使用Config Server,您可以在所有環境中管理應用程式的外部屬性。用戶端和伺服器上的概念映射與Spring Environment和PropertySource抽象相同,是以它們與Spring應用程式非常契合,但可以與任何以任何語言運作的應用程式一起使用。随着應用程式通過從開發人員到測試和生産的部署流程,您可以管理這些環境之間的配置,并确定應用程式具有遷移時需要運作的一切。伺服器存儲後端的預設實作使用git,是以它輕松支援标簽版本的配置環境,以及可以通路用于管理内容的各種工具。很容易添加替代實作,并使用Spring配置将其插入。
- 引入pom相關jar包,其中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.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>
- 在src/main/java進行ConfigApplication.java啟動檔案配置:
- org.springframework.boot.SpringApplication;
- org.springframework.boot.autoconfigure.SpringBootApplication;
- org.springframework.cloud.config.server.EnableConfigServer;
-
org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
-
class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
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
注意: 如果不從遠端git或者svn庫加載配置檔案資訊,可以配置加載本地位址,比如window下配置使用:
port: 8888
spring:
application:
name: commonservice-config-server
profiles:
active: discovery,native
cloud:
config:
server:
<span style="color: #ff0000;">native.searchLocations: d:/honghu-config</span>
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
到此,整個config服務項目配置完畢!!
從現在開始,我這邊會将近期研發的spring cloud微服務雲架構的搭建過程和精髓記錄下來,幫助更多有興趣研發spring cloud架構的朋友,大家來一起探讨spring cloud架構的搭建過程及如何運用于企業項目。(企業架構源碼可以加求球:叁五三陸二肆柒二伍玖)