spring cloud 分散式配置中心
阿新 • • 發佈:2018-11-04
簡介
在spring cloud中,有分散式配置中心元件spring cloud config,它支援配置服務放在配置服務的記憶體中(即本地),也支援放在遠端git倉庫中,在該元件中,分兩個角色,一是config server, 二是config client。
實踐
1、構建config server
(1)在原有工程上,建立一個spring-boot專案,取名為config-server,其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> <groupId>com.forezp</groupId> <artifactId>config-server</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config-server</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.forezp</groupId> <artifactId>sc-f-chapter6</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
(2)在程式的入口Application類加上@EnableConfigServer註解開啟配置伺服器的功能。
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
(3)application.yml配置
spring: application: name: config-server cloud: config: server: git: uri: https://github.com/forezp/SpringcloudConfig/ #配置git倉庫地址 search-paths: respo #配置倉庫路徑 username: #如果git倉庫為公開倉庫,可以填寫使用者名稱客密碼,如果是私有倉庫需要填寫 password: label: master #配置倉庫分支 server: port: 8888
(4)啟動程式
2、構建一個config client
(1)重新建立一個spring boot專案,取名為config-client,其pom檔案
<?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> <groupId>com.forezp</groupId> <artifactId>config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>config-client</name> <description>Demo project for Spring Boot</description> <parent> <groupId>com.forezp</groupId> <artifactId>sc-f-chapter6</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
(2)application.yml檔案
spring:
application:
name: config-client
cloud:
config:
label: master #遠端倉庫的分支
profile: dev
uri: http://localhost:8888/ #配置服務中心的網址
server:
port: 8881
(3)程式的入口類,寫一個API介面,返回從配置中心讀取的foo變數的值
@SpringBootApplication
@RestController
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
@Value("${foo}")
String foo;
@RequestMapping(value = "/hi")
public String hi(){
return foo;
}
}
(4)啟動程式,訪問http://localhost:8881/hi