1、接着上一章的源码,咱们接着搭建微服务的架构。配置中心(config-server)的搭建
- 老规矩首先在项目中新建一个名字为config-server的module, 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.yqf</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.yqf</groupId>
<artifactId>eureka-demos</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
</project>
2.在ConfigServerApplication中开启配置,即添加@EnableConfigServer的配置
3.在resources目录下面新建一个名字为share的文件夹,该文件用于集中管理配置文件,文件目录如下图
4、config-server的配置文件(application.yml)配置如下
spring:
cloud:
config:
server:
native:
search-loc
ations: classpath:/shared --设置配置文件存放路径
profiles:
active: native --本地存放配置文件
application:
name: config-server
# port
server:
port: 8769
management:
security:
enabled: false
5、在eureka-client即(需要从config-server中获取配置的module)添加config的依赖
6、修改eureka-client配置文件如下图
spring:
application:
name: eureka-client
cloud:
config:
uri: http://localhost:8769 --config-server的地址
fail-fast: true
profiles:
active: dev --启动测试的配置文件
7、在config-server的share文件夹中新增一个名字为eureka-client-dev.yml的配置文件,这里文件命名规则是${spring.application.name}-${dev/prod}
前面是应用的名称后面跟上dev或prod即指明是获取测试的配置文件还是开发的配置文件
8、分别启动config-server 、eureka-server 、eureka-client, 当看到eureka-client的控制台输出如下日志时,即代表获取配置文件成功
2018-06-12 09:04:20.969 INFO [transfer,,,] 7632 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:8769
2018-06-12 09:04:21.505 INFO [transfer,,,] 7632 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=transfer, profiles=[dev], label=null, version=null, state=null
2018-06-12 09:04:21.505 INFO [transfer,,,] 7632 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='classpath:/shared/transfer-dev.yml'}, MapPropertySource {name='classpath:/shared/application.yml'}]}
2018-06-12 09:04:21.594 INFO com.jly.transfer.TransferApplication Line:663 - The following profiles are active: dev
至此spring的配置中心即搭建完成。