一、 config 配置核心
yml 配置文件保留到 git 服务器,例如 github.com 或 gitee.com
微服务启动时,从服务器获取配置文件
1. 配置 config
1.1 新建config文件夹
1.2 将sp02,sp03,sp04,sp11四个我的项目的yml配置文件,复制到config我的项目,并改名
- item-service-dev.yml
- user-service-dev.yml
- order-service-dev.yml
- zuul-dev.yml
最初,清空四个我的项目中的application.yml
文件
1.3 禁止配置核心的配置信息笼罩客户端配置
默认配置核心配置优先级高,配置核心配置会笼罩客户端的所有配置,包含命令行参数配置,这样咱们在item-service和order-service中配置的端口号启动参数会有效,所以咱们能够设置禁止配置核心的配置将客户端配置笼罩掉,在四个配置文件中增加上面的配置
spring: application: name: xxx cloud: config: #避免配置核心的配置将客户端配置笼罩掉 override-none: true
1.4 将 config 我的项目上传到 gitee
2. 配置 config 服务器
config 配置核心从 git 下载所有配置文件。而其余微服务启动时从 config 配置核心获取配置信息。
2.1 新建 sp12-config 我的项目
2.2 增加依赖
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 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>
2.3 批改 application.yml 文件
spring: application: name: config-server cloud: config: server: git: uri: https://github.com/你的集体门路/sp-config searchPaths: config #username: your-username #password: your-password server: port: 6001 eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
2.4 ### 主程序增加 @EnableConfigServer
注解
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@SpringBootApplicationpublic class Sp12ConfigApplication { public static void main(String[] args) { SpringApplication.run(Sp12ConfigApplication.class, args); }}
2.5 启动,拜访测试
拜访 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
3. 配置 config 客户端
批改以下我的项目,从配置核心获取配置信息
- sp02-itemservice
- sp03-userservice
- sp04-orderservice
- sp11-zuul
3.1 增加 config 客户端依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId></dependency>
3.2 在四个我的项目中增加 bootstrap.yml
bootstrap.yml
,疏导配置文件,先于 application.yml 加载
- item-service
#bootstrap.yml#1.eureka地址eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eurekaspring: cloud: config: discovery: #2.从注册核心,发现配置核心 enabled: true service-id: config-server #3.从配置核心:下载 item-service-dev.yml name: item-service profile: dev
- user-service
#bootstrap.yml#1.eureka地址eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eurekaspring: cloud: config: discovery: #2.从注册核心,发现配置核心 enabled: true service-id: config-server #3.从配置核心:下载 user-service-dev.yml name: user-service profile: dev
- order-service
#bootstrap.yml#1.eureka地址eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eurekaspring: cloud: config: discovery: #2.从注册核心,发现配置核心 enabled: true service-id: config-server #3.从配置核心:下载 order-service-dev.yml name: order-service profile: dev
- zuul
#bootstrap.yml#1.eureka地址eureka: client: service-url: defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eurekaspring: cloud: config: discovery: #2.从注册核心,发现配置核心 enabled: true service-id: config-server #3.从配置核心:下载 zuul-dev.yml name: zuul-service profile: dev