该系列项目整体介绍及源代码请参照前面写的一篇文章【springcloud学习(dalston.sr1)】项目整体介绍(含源代码)(一)
springcloud学习(dalston.sr1)系统文章汇总如下:
【springcloud学习(dalston.sr1)】项目整体介绍(含源代码)(一)
【springcloud学习(dalston.sr1)】Eureka服务端集群的搭建(含源代码)(二)
【springcloud学习(dalston.sr1)】Eureka单个服务端的搭建(含源代码)(三)
【springcloud学习(dalston.sr1)】Eureka 客户端服务注册(含源代码)(四)
【springcloud学习(dalston.sr1)】服务消费者通过restTemplate来访问服务提供者(含源代码)(五)
【springcloud学习(dalston.sr1)】Eureka 服务发现(含源代码)(六)
【springcloud学习(dalston.sr1)】Ribbon负载均衡(含源代码)(七)
【springcloud学习(dalston.sr1)】使用Feign实现接口调用(含源代码)(八)
【springcloud学习(dalston.sr1)】Hystrix服务熔断(含源代码)(九)
【springcloud学习(dalston.sr1)】Hystrix服务降级(含源代码)(十)
【springcloud学习(dalston.sr1)】Hystrix Dashboard服务监控(含源代码)(十一)
【springcloud学习(dalston.sr1)】Zuul路由访问映射规则配置及使用(含源代码)(十二)
【springcloud学习(dalston.sr1)】Config配置中心-ConfigServer端与Git通信(含源代码)(十三)
【springcloud学习(dalston.sr1)】Config配置中心-Configclient端通过和Config server端通信来获取配置文件信息(含源代码)(十四)
(一)config client如何与config server进行通信,进而获取到git上的配置文件
前面一篇文章【springcloud学习(dalston.sr1)】Config配置中心-ConfigServer端与Git通信(含源代码)(十三)我们讨论了config server端和git进行了通信。这篇文章,我们讨论下config client如何与config server进行通信,进而获取到git上的配置文件。
我们提前在git上准备一个microservicecloud-config-client.yml配置文件,如下图:
接下来我们将创建一个config client端的项目microservicecloud-config-client-3355,并且该项目启动时,会通过config server端,来从git服务端进行拉取microservicecloud-config-client.yml配置文件,并作为启动时的配置文件(可以从截图中看到,该配置文件也是准备了两套环境的配置,分别是dev和test环境)。
(二)config client项目的创建
创建一个config client端的项目microservicecloud-config-client-3355,该项目的整体结构如下:
(1)首先在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">
<parent>
<artifactId>springcloud2025</artifactId>
<groupId>com.company</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-config-client-3355</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.company</groupId>
<artifactId>microservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- springcloud config 配置 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</project>
(2)然后我们新建两个配置文件bootstrap.yml和application.yml(说明:bootstrap.yml的加载优先级比application.yml文件更高,说明项目启动时,会优先加载bootstrap.yml中的配置,再加载application.yml中的配置,也就是说,如果两个文件中存在相同名称的配置项,则application.yml因为是后加载的,会替换掉bootstrap.yml中的同名配置项)
其中bootstrap.yml的内容如下:
spring:
cloud:
config:
name: microservicecloud-config-client #需要从gitee上面读取的资源名称,注意没有yml后缀名
profile: test #本次访问的环境名称
lable: master #本次访问的分支名称
uri: http://localhost:3344 #http://config-3344.com:3344 #这里使用config server端的地址本服务启动后先去找3344服务,通过springcloudconfig获取gitee的服务地址
如上面代码所示,本次配置文件中,我们通过配置项spring.cloud.config.name=microservicecloud-config-client指定了需要获取的git服务器上的文件名称为microservicecloud-config-client.yml
通过配置项spring.cloud.config.profile=test指定了需要获取的git服务器上的microservicecloud-config-client.yml文件的test环境内容
通过配置项spring.cloud.config.label=master指定了需要获取的git服务器上的master分支,通过配置项spring.cloud.config.uri=http://localhost:3344,指定了config server端的服务地址。
application.yml的内容如下:
spring:
application:
name: microservicecloud-config-client
(3)然后是启动类
package com.company.config.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Config3355ClientApplication {
public static void main(String[] args) {
SpringApplication.run(Config3355ClientApplication.class, args);
}
}
(4)然后我们新建一个controller,里面设置一个接口,用于获取当前项目的配置信息
package com.company.config.client.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
private static final Logger logger = LoggerFactory.getLogger(ConfigClientController.class);
@Value("${spring.application.name}")
private String applicationName;
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServers;
@Value("${server.port}")
private String port;
@RequestMapping("/config")
public String getConfig() {
String result = "applicationName:" + applicationName + ",eurekaServers:" + eurekaServers + ",port:" + port;
logger.info("获取到配置信息为:{}" + result);
return result;
}
}
(三)config client通过config server端,拉取git服务的配置验证
依次启动config server端(microservicecloud-config-3344)和config client端(microservicecloud-config-client-3355)项目,然后我们从config client端控制台的打印信息,可以看到其端口号是8202,而这个端口号,其实我们并没有在本地配置文件(bootstrap.yml和application.yml)中进行过配置,说明这个是从git服务上拉取的配置。
另外,我么还可以通过访问config client端的接口 http://localhost:8202/config,通过返回的信息,也间接证明了,确实获取的是git上面microservicecloud-config-client.yml文件的test环境的配置信息