前言:在上一节中我们搭建了SpringCloud Config的服务端部分(服务端又被称为分布式配置总控中心),而本节则是搭建SpringCloud Config的客户端部分
要搭建的是圈起来的那部分
1、创建一个客户端模块,命名为cloud-config-client-3355
(1)在父工程下新建模块
(2)选择模块的项目类型为Maven并选择模块要使用的JDK版本
(3)填写子模块的名称,然后点完成即可完成创建
效果图:
(4)修改cloud-config-client-3355子模块的pom.xml文件,然后reolad一下,下载依赖
例:
<?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>springcloud01</artifactId>
<groupId>com.ken.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-config-client-3355</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<!--lombok插件-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Eureka Clinet-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
(5)为子模块添加名为bootstrap的yml配置文件
注:application.yml是用户级资源配置项,bootstrap.yml是系统级,bootstrap.yml优先级更高,Spring Cloud会创建一个BootstrapContext,作为Spring应用的Application Context的父上下文。初始化的时候,BootstrapContext负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。Bootstrap context和Application Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证`Bootstrap Context和Application Context配置的分离。要将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml
效果图:
(6)修改bootstrap.yml文件配置
server:
port: 3355
spring:
application:
name: config-client
cloud:
#config客户端配置(下述4个配置综合起来,意思是读取http://localhost:3344地址里的master分支里的config-dev.ymL配置文件,即读取http://config-3344.com:3344/master/config-dev.yml
config:
#分支名称
label: master
#配置文件名称
name: config
#读取后缀名称
profile: dev
#配置中心地址
uri: http://localhost:3344
#服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
(7)为cloud-config-client-3355子模块新建一个主启动类,类名输入com.ken.springcloud.ConfigClientMain3355,然后创建即可
效果图:
(8)编写ConfigClientMain3355主启动类
package com.ken.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class, args);
}
}
2、在com.ken.springcloud包下新建一个名为controller的包
效果图:
3、在controller包下新建一个名为ConfigClientController的控制类
效果图:
4、编写ConfigClientController类
package com.ken.springcloud.Controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
5、分别启动eureka-server7001、cloud-config-center-3344、cloud-config-client-3355
效果图:
6、在浏览器输入http://config-3344.com:3344/master/config-dev.yml测试看配置中心是否能成功从gitee上成功取得配置内容
效果图:
由图可知读取配置成功
7、在浏览器输入http://localhost:3355/configInfo测试看客户端是否能通过配置中心从gitee上成功取得配置内容
由图可知读取配置成功