yml 配置文件保存到 git 服務器,例如 github.com 或 gitee.comjava
微服務啓動時,從服務器獲取配置文件git
最後,清空四個項目中的application.yml
文件github
默認配置中心配置優先級高,配置中心配置會覆蓋客戶端的全部配置,包括命令行參數配置,這樣咱們在item-service和order-service中配置的端口號啓動參數會無效spring
item-service 啓動參數:apache
--service.port=8001
--service.port=8002
order-service 啓動參數bootstrap
--service.port=8201
--service.port=8202
能夠設置禁止配置中心的配置將客戶端配置覆蓋掉,這樣當咱們從控制中心拉取配置文件後,本地的配置會覆蓋拉去來的配置
在四個配置文件中添加下面的配置服務器
spring: ...... cloud: config: override-none: true
springcloud1 工程目錄建立本地倉庫app
把本地倉庫提交推送到gitee遠程倉庫maven
config 配置中心從 git 下載全部配置文件。
而其餘微服務啓動時從 config 配置中心獲取配置信息。ide
<?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 https://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.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.tedu</groupId> <artifactId>sp12-config</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sp12-config</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Hoxton.RELEASE</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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </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: application: name: config-server cloud: config: server: git: uri: https://gitee.com/Mjp3309/springcloud1 search-paths: config #私有倉庫須要配置用戶名密碼 #username: #password: server: port: 6001 eureka: client: service-url: defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
@EnableConfigServer
和 @EnableDiscoveryClient
package cn.tedu.sp12; 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; @EnableConfigServer//啓動配置中心服務 @EnableDiscoveryClient @SpringBootApplication public class Sp12ConfigApplication { public static void main(String[] args) { SpringApplication.run(Sp12ConfigApplication.class, args); } }
訪問 item-service-dev.yml 能夠使用如下形式:
http://localhost:6001/item-service-dev.yml
http://localhost:6001/item-service/dev
測試其餘文件
http://localhost:6001/user-service/dev
http://localhost:6001/zuul/dev
http://localhost:6001/order-service/dev
修改如下項目,從配置中心獲取配置信息
右鍵點擊項目,編輯起步依賴,添加 config client 依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
bootstrap.yml
,引導配置文件,先於 application.yml 加載
spring: cloud: config: discovery: enabled: true service-id: config-server name: item-service profile: dev eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
spring: cloud: config: discovery: enabled: true service-id: config-server name: user-service profile: dev eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
spring: cloud: config: discovery: enabled: true service-id: config-server name: order-service profile: dev eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
spring: cloud: config: discovery: enabled: true service-id: config-server name: zuul profile: dev eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
spring cloud 容許運行時動態刷新配置,能夠從新從配置中心獲取新的配置信息
以 user-service
爲例演示配置刷新
user-service 的 pom.xml 中添加 actuator 依賴
右鍵點擊sp03-user-service
項目,編輯起步依賴,添加 actuator 依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>