前言:
在分布式系统中每个微服务会有不同的配置文件,并且在生产环境、开发环境、测试环境等可能都对应不同的配置文件,分布式配置中心可以对其进行统一管理。可以把配置文件放在一个磁盘路径下,也可以放在git仓库中。当然还是放在git仓库中好
代码:
在上一篇代码的基础上增加config-service工程
pom.xml
加入maven依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
<?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"> <parent> <artifactId>springcloud14-config</artifactId> <groupId>com.xhx.springcloud</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>config-service</artifactId> <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> </dependencies> </project>
application.yml
server: port: 8888 spring: application: name: config-service cloud: config: server: git: #git地址 uri: https://github.com/xuhaixing/springCloudConfig #配置文件所在的目录 search-paths: /** #default-label: master #username: #password: label: master # username: #password: eureka: client: service-url: default-zone: http://localhost:8761/eureka
上面配置git的地址,如果有用户名密码也可以配置上
启动类:加上@EnableConfigServer
package com.xhx.springcloud; 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; /** * xuhaixing * 2018/6/13 12:57 */ @SpringBootApplication @EnableEurekaClient @EnableConfigServer public class ConfigServiceApplication { public static void main(String[] args) { SpringApplication.run(ConfigServiceApplication.class,args); } }
然后创建一个微服务:
加入maven依赖包:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
pom.xml
加入下面依赖包:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
<?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> <artifactId>eureka-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>eureka-service</name> <parent> <groupId>com.xhx.springcloud</groupId> <artifactId>springcloud14-config</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</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> </plugin> </plugins> </build> </project>
bootstrap.yml
spring: cloud: config: discovery: #开启配置服务发现 enabled: true #配置服务实例名称 service-id: config-service #配置文件所在分支 label: master profile: prd #远程配置中心的访问地址 #uri: http://localhost:8888/ application: name: eureka-service把application.yml放到git上
eureka-service是服务id, dev/prd是profile
启动类:
package com.xhx.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class EurekaServiceApplication { public static void main(String[] args) { SpringApplication.run(EurekaServiceApplication.class, args); } }
可以先直接访问配置中心试一试:
localhost:8888/eureka-service-dev.yml
也可以用下面这种方式试试:
基本有这几种方式:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
然后启动eureka,启动config,然后启动eureka-service看看能不能拉远程配置:
证明么有问题,启动了