博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringCloud(Finchley版)6 - Config-Client
阅读量:7079 次
发布时间:2019-06-28

本文共 4827 字,大约阅读时间需要 16 分钟。

hot3.png

一, 简介

既然项目中已经有了配置中心 config-server , 那么我们怎么去消费配置服务呢? 当然就是 config-client ;

二, 创建 config-client 服务 cloud-g

1, pom.xml

4.0.0
com.gy.cloud
cloud
1.0-SNAPSHOT
cloud-g
0.0.1-SNAPSHOT
jar
${project.artifactId}
Demo project for config-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-config
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-actuator

2, bootstrap.properties

# 和 application 配置文件相比, bootstrap 配置文件具有以下几个特性:# 1, bootstrap 由父 ApplicationContext 加载,比 application 优先加载;# 2, bootstrap 里面的属性不能被覆盖;# 3, bootstrap 和application 的应用场景:#       application: 主要用于spring boot 项目的自动化配置;#       bootstrap:#           a, 使用 spring Cloud config 配置中心时, 这时需要在 bootstrap 配置文件中添加连接到配置中心的配置属性来加载外部配置中心的配置信息;#           b, 一些固定的不能被覆盖的配置; c, 一些加密/解密的场景;server.port=8767spring.application.name=config-clienteureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/# 该配置在 bootstrap.properties(.yml) 文件才能生效 (配置服务)spring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.serviceId=config-server# 该配置在 bootstrap.properties(.yml) 文件才能生效 (配置服务)#spring.cloud.config.uri= http://localhost:8766/spring.cloud.config.label=masterspring.cloud.config.profile=dev# 开放配置刷新接口 /actuator/refreshmanagement.endpoints.web.exposure.include=refresh

3, CloudGApplication

package com.gy.cloud.cloudg;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;@RefreshScope // 配置刷新@RestController@EnableDiscoveryClient@SpringBootApplicationpublic class CloudGApplication {    public static void main(String[] args) {        SpringApplication.run(CloudGApplication.class, args);        System.out.println("=== 服务G config-client 启动SUCCESS ===");    }    @Value("${testUsername}")    private String param;    @GetMapping("getConfigParam")    public String getConfigParam() {        return param;    }}

4, 启动 cloud-g

启动日志 : 

2019-01-11 11:03:04.093  INFO 10844 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://XUQ3937QAYKT5WO:8766/2019-01-11 11:03:04.729  INFO 10844 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=config-client, profiles=[dev], label=master, version=f2f23650522db4787e8a2a7d3b030058a8598899, state=null2019-01-11 11:03:04.730  INFO 10844 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://gitee.com/ge.yang/SpringBoot//src/main/resources/config/config-client-dev.properties'}]}......2019-01-11 11:03:09.462  INFO 10844 --- [           main] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient2019-01-11 11:03:09.474  INFO 10844 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 1 endpoint(s) beneath base path '/actuator'2019-01-11 11:03:09.486  INFO 10844 --- [           main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/refresh],methods=[POST],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map
)

根据启动日志, 发现 POST 接口 :  /actuator/refresh

1,   访问 :  http://localhost:8767/getConfigParam

2,   修改Git仓库 testUsername 值  访问 :  http://localhost:8767/getConfigParam

3,   POST 访问 :  http://localhost:8767/actuator/refresh

4,   访问 :  http://localhost:8767/getConfigParam

三, 总结

注意 1, bootstrap 与 application 的区别;

        2, 注意 POST /actuator/refresh 刷新接口的使用;

学习文档

 :  

项目源码:  

转载于:https://my.oschina.net/u/3681868/blog/3000105

你可能感兴趣的文章
pycharm中import动态链接库pyd有错误
查看>>
Android 在通知栏实现计时功能
查看>>
N皇后问题
查看>>
Quick-cocos2d-x3.3 Study (十三)--------- 创建物理世界的边界 ( 创建一个带物理效果的线条 )...
查看>>
【乐畅】工作积累 ---- 调节音量大小 (滑动条调节音量大小并保存起来 )
查看>>
MapReduce之Map Join
查看>>
html的字符实体
查看>>
struts2
查看>>
java分页之页面分页
查看>>
浅谈C中的指针和数组(七)
查看>>
Cordova总是弹出Connection to server was Unsuccessful
查看>>
《转》 win32多线程-在MFC程序中使用多线程
查看>>
complex类
查看>>
多表查询
查看>>
Freebsd下程序随系统开机启动的方法
查看>>
【第49题】【062题库】2019年OCP认证062考试新题
查看>>
WebApp上滑加载数据...
查看>>
MySQL常用操作
查看>>
[原]Winform自定义控件在网页上的应用
查看>>
重载,重写和super
查看>>