内容简介:在《配置中心》这一篇博文里学习了如何git获取配置文件。大概的流程可以用下图来概括。《配置中心》这篇博文说的是Config Server,本篇将和大家看看如何编写一个Config Client从Config Server获取配置。
在《配置中心》这一篇博文里学习了如何git获取配置文件。大概的流程可以用下图来概括。
《配置中心》这篇博文说的是Config Server,本篇将和大家看看如何编写一个Config Client从Config Server获取配置。
1、 先在仓库中创建如下配置文件(具体参考下面地址)
https://gitee.com/hjj520/spring-cloud-2.x/tree/master/config-repos/sc-config-client
2、 创建maven项目sc-config-client,对应的pom.xml如下
<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>spring-cloud</groupId>
<artifactId>sc-config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>sc-config-client</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<!--
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
</dependencies>
</project>
其中:spring-cloud-starter-config与spring-cloud-config-client可以二选一,但是根据选择的依赖不同对应的配置文件有些许不一样。spring-cloud-starter-config已经包含spring-cloud-config-client,所以选择依赖spring-cloud-starter-config。
3、 创建配置文件bootstrap.yml
#服务端口
server:
port: 8200
eureka:
client:
serviceUrl:
defaultZone: http://localhost:5001/eureka/
spring:
application:
name: sc-config-client
cloud:
config:
label: master # 配置文件所在分支
#uri: http://127.0.0.1:8100/ #配置服务中心
profile: dev # dev根据具体情况来修改
discovery:
serviceId: sc-config-server #配置服务实例名称
enabled: true #开启配置服务发现
备注:sc-config-server为配置服务实例名称,对应sc-config-server项目的bootstrap.yml配置文件的如下配置项
4、 创建启动类ConfigClientApplication.java
package sc.config.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
//@EnableDiscoveryClient
@EnableEurekaClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
5、 为了验证是否能不能在config server获取到配置项,创建一个restful类型的controller:ConfigController.java
package sc.config.client.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigController {
// git配置文件里的key
@Value("${jdbc.driverClassName}")
private String driverClassName;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@RequestMapping(value="/config/getValue")
public Map<String, Object> getConfigFromGit() {
Map<String, Object> result = new HashMap<String, Object>();
result.put("code", "000000");
result.put("msg", "ok");
Map<String, Object> body = new HashMap<String, Object>();
body.put("driverClassName", driverClassName);
body.put("url", url);
body.put("username", username);
body.put("password", password);
result.put("body", body);
return result;
}
}
6、 先启动注册中心,对应的项目为sc-eureka-server;再启动config sever,对应的项目为sc-config-server。然后验证一下config sever是否启动成功
方式一:访问注册中心,可以看到config sever已经注册到注册中心了
方式二:访问配置文件对应的路径看看是否可以获取配置文件,如果能获取到说明启动成功
给大家一一对应一下yml问下的访问方式,这些在config server那篇博文只是大概提了一下:
{[/{name}-{profiles}.yml || /{name}-{profiles}.yaml],methods=[GET]}:
http://127.0.0.1 :8100/application-dev.yml
{[/{name}/{profiles:.}],methods=[GET]}:
http://127.0.0.1 :8100/application/dev
{[/{name}/{profiles}/{label:.*}],methods=[GET]}: http://127.0.0.1 :8100/application/dev/master
{[/{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml],methods=[GET]}:
http://127.0.0.1 :8100/master/application-dev.yml
7、 启动config client对应的项目sc-config-client
当spring.cloud.config.profile的值为dev时访问 http://127.0.0.1 :8200/config/getValue
当spring.cloud.config.profile的值为prd时访问 http://127.0.0.1 :8200/config/getValue
可以看到spring.cloud.config.profile配置不一样时,分配获取到git仓库的application-dev.yml和application-prd.yml配置文件的内容
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- Nacos 源码分析(二):获取配置流程
- Spring Security 实现 antMatchers 配置路径的动态获取 原 荐
- 腾讯 Tars-Go 服务获取自定义模版(配置)值
- mybatis系列之获取mapper.xml配置文件中的sql
- Spring Cloud-Feign使用时获取apollo配置失败问题
- ADO.NET获取数据(DataSet)同时获取表的架构实例
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
C++ 程序设计语言(特别版)(英文影印版)
[美] Bjarne Stroustrup / 高等教育出版社 / 2001-8-1 / 55.00
《C++程序设计语言》(特别版)(影印版)作者是C++的发明人,对C++语言有着全面、深入的理解,因此他强调应将语言作为设计与编程的工具,而不仅仅是语言本身,强调只有对语言功能有了深入了解之后才能真正掌握它。《C++程序设计语言》编写的目的就是帮助读者了解C++是如何支持编程技术的,使读者能从中获得新的理解,从而成为一名优秀的编程人员和设计人员。一起来看看 《C++ 程序设计语言(特别版)(英文影印版)》 这本书的介绍吧!