当前位置: 首页 > news >正文

SpringCloud(Finchley版)6 - Config-Client

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

一, 简介

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

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

1, 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.gy.cloud</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>cloud-g</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>${project.artifactId}</name>
    <description>Demo project for config-client</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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-actuator</artifactId>
        </dependency>
    </dependencies>

</project>

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=8767
spring.application.name=config-client
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

# 该配置在 bootstrap.properties(.yml) 文件才能生效 (配置服务)
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server

# 该配置在 bootstrap.properties(.yml) 文件才能生效 (配置服务)
#spring.cloud.config.uri= http://localhost:8766/

spring.cloud.config.label=master
spring.cloud.config.profile=dev

# 开放配置刷新接口 /actuator/refresh
management.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
@SpringBootApplication
public 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=null
2019-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 DiscoveryClient
2019-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<java.lang.String, java.lang.String>)

根据启动日志, 发现 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://www.fangzhipeng.com/springcloud/2018/08/30/sc-f7-config/

项目源码:  https://gitee.com/ge.yang/spring-demo/tree/master/cloud

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

相关文章:

  • Golang-长连接-状态推送
  • 硬盘结构
  • 虚函数可不可以是内联函数
  • 用JS实现人脑和计算机交互,这个厉害了
  • 翻硬币
  • How cc Works 中文译文
  • 如何优雅地查看 JS 错误堆栈?
  • [转]IPTABLES中SNAT和MASQUERADE的区别
  • PaddlePaddle-GitHub的正确打开姿势
  • Codeforces 1097 Alex and a TV Show
  • 第八届(2018)CSR年度盛典在北京举办
  • 身残心不残 河北大城63岁独身老人捐献遗体
  • Spring Batch JSON 支持
  • settings配置数据库和日志
  • K-means 怎么选 K ?
  • 收藏网友的 源程序下载网
  • 《Java编程思想》读书笔记-对象导论
  • 【391天】每日项目总结系列128(2018.03.03)
  • C++类中的特殊成员函数
  • iOS 颜色设置看我就够了
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • JavaScript新鲜事·第5期
  • php面试题 汇集2
  • Python爬虫--- 1.3 BS4库的解析器
  • rabbitmq延迟消息示例
  • SAP云平台里Global Account和Sub Account的关系
  • vuex 学习笔记 01
  • 翻译 | 老司机带你秒懂内存管理 - 第一部(共三部)
  • 前端工程化(Gulp、Webpack)-webpack
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • 阿里云服务器购买完整流程
  • 交换综合实验一
  • ​2021半年盘点,不想你错过的重磅新书
  • # Apache SeaTunnel 究竟是什么?
  • ###项目技术发展史
  • (java版)排序算法----【冒泡,选择,插入,希尔,快速排序,归并排序,基数排序】超详细~~
  • (LeetCode 49)Anagrams
  • (板子)A* astar算法,AcWing第k短路+八数码 带注释
  • (初研) Sentence-embedding fine-tune notebook
  • (二)正点原子I.MX6ULL u-boot移植
  • (附源码)python旅游推荐系统 毕业设计 250623
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (每日持续更新)jdk api之FileFilter基础、应用、实战
  • (十一)c52学习之旅-动态数码管
  • (实战篇)如何缓存数据
  • (正则)提取页面里的img标签
  • (转)h264中avc和flv数据的解析
  • ****Linux下Mysql的安装和配置
  • *setTimeout实现text输入在用户停顿时才调用事件!*
  • .NET C#版本和.NET版本以及VS版本的对应关系
  • .Net IOC框架入门之一 Unity
  • .Net Web项目创建比较不错的参考文章
  • ??javascript里的变量问题
  • @reference注解_Dubbo配置参考手册之dubbo:reference
  • [ JavaScript ] JSON方法