首先我搭建了一個Eureka 註冊中心,這裏就不着重介紹了,不知道的小夥伴能夠
網上查資料!java
1.搭建Config 配置中心
POM 文件:git
<?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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.sg.config</groupId> <artifactId>config-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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>
這裏面Config用到的依賴是 spring-cloud-config-server ,其餘本身看着引入便可github
2.application.yml 的配置
server: port: 8095 spring: application: name: config-demo cloud: config: server: git: uri: https://github.com/****/spring-cloud-demo.git username: password: search-paths: spring-cloud-demo-config eureka: client: service-url: defaultZone: http://127.0.0.1:8090/eureka/ register-with-eureka: true fetch-registry: true
Config Server 的配置主要是spring.cloud.config.server.git 下面的東西,其餘按需配置便可。 web
3.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; @SpringBootApplication @EnableConfigServer @EnableDiscoveryClient public class ConfigDemoApplication { public static void main(String[] args) { SpringApplication.run(ConfigDemoApplication.class, args); } }
至此 Config Server 端就配置完成了。 spring
4.配置Config Client 端
首先引入依賴apache
<?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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.sg.eureka.client</groupId> <artifactId>eureka-client-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-client-demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.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-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.sg.common</groupId> <artifactId>spring-cloud-common</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </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>
主要有關的依賴是 spring-cloud-starter-config ,其餘的按需引入便可bootstrap
5.application.yml 配置文件
server: port: 8091 spring: application: name: eureka-client-demo cloud: config: profile: test uri: http://127.0.0.1:8095/ label: master discovery: enabled: true service-id: config-demo eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://127.0.0.1:8090/eureka/ ### 端點控制 management: endpoints: web: exposure: # 開啓指定端點 include: hystrix.stream project: name: hahha
啓動類上什麼配置也不用添加,就能夠了,重要的一點:
1:若是用了Eureka ,則須要配置 spring.cloud.config.discovery.enable: true 和 spring.cloud.config.discovery.service-id: config-demo 這兩個屬性 app
6.讀取Server中配置的屬性
package com.sg.eureka.client.controller; import com.sg.common.vo.UserVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.Environment; import org.springframework.web.bind.annotation.*; /** * @author liuhailong * @date 2019-08-08 */ @RestController @RequestMapping(value = "users") @Configuration public class UserController { @Autowired private Environment environment; @Value("${project.name}") private String name; @GetMapping(value = "/config") public String getConfig(@RequestParam("key")String key){ return environment.getProperty(key); } @GetMapping(value = "/projectName") public String projectName(){ return name; } }
我這裏用了 兩種方式去讀取配置文件中的內容
1:使用Spring core中的Environment 類 中的getProperty 能夠取到
2:使用Spring 的 @Value("${project.name}") 註解 maven
6.接下來驗證一下:訪問 http://localhost:8091/users/config?key=project.name 結果發現獲取不到Config Server中配置的參數。