非spring boot 使用Spring cloud Config (老版 spring 集成 spring cloud config ) || spring cloud config 搭建 个人博客:https://eric-ly.github.io/

背景:想自己搭建一个分布式的配置中心,发现spring Cloud Config 不错,(后台基于git,可以配置webhook 实现热更新、回滚,分环境配置等)与自己的期望相近。

虽然好用但是基于spring boot的,spring 与spring boot 还是有挺多巨别。为了能用在现有的 项目中,决定 想办法集成一下,百度 google 都没有现成的可以copy...

于是想自己实现下,查询之后 按照一个大神的想法实现了下。中间遇到许多问题,不过更加了解spring了 也会继续完善。java 新人 希望各位大神多多指导。

github:https://github.com/Eric-ly/hconfig

后面有项目的测试说明:

实现结果:

在spring 中 使用spring Cloud Config 功能,分项目/环境 获取配置文件,自动注入,自动更新 spring boot的注解无法兼容 待稍后解决。

前提:

一个简单的spring spring MVC 项目。

config server 配置好,github 配置好。 这个网上百度就有很多。

(在搭建spring cloud config (server and client)后,搭建注册应用中心 eureka 和 rabbitMq 进行自动热更新。)

实现方式:

1.创建自定义的ApplicationContext,先通过在web.xml中设置 contextClass 来指定应用使用自定义的ApplicationContext,

2.在自定义的applicationContext中 覆写 创建environment的方法,用来定制 environment,把config server的配置信息加入 应用

3.自定义的CloudEnvironment extends StandardServletEnvironment,扩展customizePropertySources方法,指定 配置的来源,然后放入 propertySource的list中,作为整个项目的 配置之一。

4.自定义propertySouce ,使用spring cloud client的ConfigClientProperties 作为 propertySouce的来源, 然后添加到PropertySource中

5.自定义配置类,将配置中心的配置按照单个/model 进行注入

后期实现:

(1)兼容@refresh的注解,使 自动更新。

(2)通过修改name 获取多个配置文件。优化config server的文件

(3)细化 回滚功能

(4)写 拦截器,打成jar包,成为一个工具包,通过自定义注解 来使用。隐藏实现。成为一个通用的功能

最近有点忙,这个项目先达到能基本用,慢慢完善。

具体实现步骤:

1.增加pom 依赖,spring mvc ,spring cloud,logback 等

    <properties><spring.version>4.2.9.RELEASE</spring.version><jstl.version>1.2</jstl.version><junit.version>4.11</junit.version><logback.version>1.0.13</logback.version><jcl-over-slf4j.version>1.7.5</jcl-over-slf4j.version><spring.cloud.version>1.2.0.RELEASE</spring.cloud.version></properties>
复制代码

2.创建自定义的ApplicationContext,先通过在web.xml中设置 contextClass 来指定自己的ApplicationContext,

contextClass

org.lybm.hconfig.clientWosb.config.CustomWebApplicationContext

ApplicationContext 是spring ioc 的体现,spring 中的容器。一般有几种实现。
FileSystemXmlApplicationContext/ClassPathXmlApplicationContext/WebXmlApplicationContex我们在搭建框架的时候一般通过注册ContextLoaderListener 监听 去自动获取 初始化(这里先去通过classContext的配置获取,而且必须可以强转为WebXmlApplicationContxt,如果没设置 创建默认的)
,这里为了使用spring boot的功能,我们需要自定义ApplicationContext
复制代码

在新定义的ApplicationContext 中我们使用自定义的Environment,为了把配置放到 environment中来使用。

public class CustomWebApplicationContext extends XmlWebApplicationContext {@Overrideprotected ConfigurableEnvironment createEnvironment() {System.out.println("-------- loaded my CustomWebApplicationContext context");return new CloudEnvironment();}}
复制代码

3.创建 自定义的Environment,用来把 配置中心 config server的配置 放到环境中。

(1)重写定制方法

自定义的CloudEnvironment extends StandardServletEnvironment,扩展customizePropertySources方法,指定 配置的来源,然后放入 propertySource的list中,作为整个项目的 配置之一。

这里说一下,spring 的所有配置,包括system配置 ,jdni,自定义的properties等 都会放到 spring的environtment的propertySource的list中,用来使用。

    @Overrideprotected void customizePropertySources(MutablePropertySources propertySources) {super.customizePropertySources(propertySources);try {//用来添加应用名到environment中propertySources.addLast( initResourcePropertySourceLocator() );//添加config Server的配置PropertySource<?> source = initConfigServicePropertySourceLocator(this);propertySources.addLast(source);} catch (Exception ex) {logger.warn("failed to initialize cloud config environment", ex);}}
复制代码

(2)获取 config server的 配置信息,生成propertySource ,创建一个来源。用到了spring cloud的方法

这里说明一下,在配置ConfigClientProperties 连接信息的时候, 后面的代码 会重新覆盖 applicationName 所以这里设置name是没有意义的。 会从environment 中 获取商品日嗯

    private PropertySource<?> initConfigServicePropertySourceLocator(Environment environment) {ConfigClientProperties configClientProperties = new ConfigClientProperties(environment);configClientProperties.setUri("http://localhost:9001/");
//        configClientProperties.setName("config-client");configClientProperties.setProfile("dev");configClientProperties.setLabel("master");logger.debug("will load the client configuration-------"+configClientProperties);ConfigServicePropertySourceLocator configServicePropertySourceLocator =new ConfigServicePropertySourceLocator(configClientProperties);return configServicePropertySourceLocator.locate(environment);}
复制代码

重新设置name的代码,如下, 这样会导致我们连接获取到的配置信息不正确。

        override.setName(environment.resolvePlaceholders("${" + ConfigClientProperties.PREFIX+ ".name:${spring.application.name:application}}"));
复制代码

于是,我在 进行这一步前 将正确的 name信息, 加入到environment中,就可以找到 正常使用。

(3)添加 应用相关配置 到environment中。

在查询相关源码后发现。我只需要 将一个resource 对象放入 environment的 资源 list中就可以,这里我新建一个properties,写入配置,然后封装成resource 就可以了。

cloud-config-context.properties
spring.application.name=config-client
demo=demo
复制代码

具体实现:

   Resource resource = new DefaultResourceLoader(this.getClass().getClassLoader()).
getResource("classpath:cloud-config-context.properties");resourcePropertySource = new ResourcePropertySource(resource);
复制代码

这里学习的过程 花费了一些时间。了解了 environment,propertySource 等spring 加载配置信息的代码。一些相关知识会在后续贴出来

4.现在environment 重写好了,创建配置信息类

我们目前可以通过两种方式获取配置,

(1)单个配置的获取。

    //单独一个字段的获取@Value("${test}")private String test;这么写 如果没有test ,程序会报错,启动不了, 所以 不要用这种写法//单独一个字段的获取@Value("${xxx:x}")private String test;
单个字段 推荐这么写,如果xxx没有,则使用默认值x ,这样比较符合 业务场景
复制代码

(2)按照配置信息类 获取

如下,将配置信息映射成一个model,可以将统一前缀 如wosb的配置信息 放到一个类里,这样方便管理,在写配置的时候 也好区分,就像一个一个配置文件。

@ConfigurationProperties(prefix = "wosb")
@Data
public class WosbProperties {private String test;private String demo;private String name;private String paht;
}
复制代码

(2.2)将配置类 注入,两种方式,

a. 通过注解,增加@Configuration,将这个类注入到 容器中

注解定义
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
复制代码

b.通过配置PropertySourcesPlaceHolderConfigurer,不太推荐

@Configuration
@EnableConfigurationProperties({WosbProperties.class})
public class PropertiesConfigurer {@Beanpublic static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {return new PropertySourcesPlaceholderConfigurer();}
}
复制代码

5.继承完成,使用/测试

(1)创建controller

(2)使用

    @Autowiredprivate SearchProperties searchProperties;//单独一个字段的获取@Value("${xxx:x}")private String test;
复制代码

(3)可以注入Environment env 来查看当前的environment信息。

项目说明:

github:https://github.com/Eric-ly/hconfig

github的配置文件地址:https://github.com/Eric-ly/hconfig-properties

cloudeureka: spring boot的应用注册中心

ConfligClientNoSpringBoot: 非spring boot 应用 使用 应用配置中心

hconfigclient:spring boot的 应用配置中心的 客户端

hconfigserver:spring boot的 应用配置中心的服务端

使用说明:

1.启动 应用注册中心

(1)入口类:EurekaServerApplication IDE debug 或者用命令

(2)配置:

server.port=8761
eureka.instance.hostname=localhost
复制代码

2.启动 配置中心的服务端

(1)入口类:ConfigServerApplication

(2)配置:

端口:

spring.application.name=config-server
server.port=9001
复制代码

gitlab :来源

spring.cloud.config.server.git.uri=https://github.com/Eric-ly/hconfig-properties.git
spring.cloud.config.server.git.searchPaths=configRepo
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=spring.cloud.config.server.git.basedir=src/main/resource/config
# //加载到ConfigServer项目所在的本地目录的位置, 可以不用配置
复制代码

消息总线,用来自动热更新,配置的是mq的地址,需要自己启

#   添加cloud bus 消息总线,配合webhook 实现 所有client的消息热更新
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
## 刷新时,关闭安全验证
management.security.enabled=false
## 开启消息跟踪
spring.cloud.bus.trace.enabled=true
复制代码

注册中心:

#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
复制代码

3.启动 非spring boot的 ConfligClientNoSpringBoot

(1)配置 应用名

cloud-config-context.properties

spring.application.name=config-client
复制代码

(2)IDE添加tomcat,然后启动

(3)测试

http://localhost:8888/hello

结果: 配置文件都读取到了

search-pproperties : collection :{{engine}} demo: {{solr}} ||| wosb-properties: demo : {{ a}} name: {{ wosb }} path: {{ /}} single value : test?{{ abcdegggg }}
复制代码

4.spring boot的 配置中新 客户端( 自动热更新)hconfigclient

(1)入口类 entryApplication

(2)配置:

(1) 应用名+ 配置中心的 服务端

spring.application.name=config-client
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:9001/
server.port=8888
复制代码

(2)

#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
复制代码

(3)

## 刷新时,关闭安全验证
management.security.enabled=false
## 开启消息跟踪
spring.cloud.bus.trace.enabled=truespring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
复制代码

(4) 测试

(提交代码触发post请求给bus/refresh ----server端接收到请求并发送给Spring Cloud Bus
----Spring Cloud bus接到消息并通知给其它客户端 ----其它客户端接收到通知,请求Server端获取最新配置
----全部客户端均获取到最新的配置)
复制代码

非spring boot (即spring) 使用/集成 Spring cloud Config 分布式配置中心相关推荐

  1. Spring cloud config 分布式配置中心(一) 服务端

    作用: 为分布式系统中的基础设施和微服务应用提供外部集中化的配置支持,分客户端和服务端 服务端: 即分布式配置中心,是一个独立的微服务应用,连接配置仓库,为客户端提供一些访问接口,如加密 / 解密信息 ...

  2. Spring Boot (三)集成spring security

    项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...

  3. 第十二章 Spring Cloud Config 统一配置中心详解

    目录 一.配置问题分析及解决方案 1.问题分析 2.解决方案 二.Spring Cloud Config 介绍 1.Spring Cloud Config特性 2.Spring Cloud Confi ...

  4. Spring Cloud入门-Config分布式配置中心(Hoxton版本)

    文章目录 Spring Cloud入门系列汇总 摘要 Spring Cloud Config 简介 在Git仓库中准备配置信息 配置仓库目录结构 master分支下的配置信息 dev分支下的配置信息 ...

  5. Spring Cloud Config的配置中心获取不到最新配置信息的问题

    本篇源于Spring Cloud Config的一个问题,但这个问题并非所有人都会遇到.如果您遇到了,那必须得看看这篇,如果没有遇到您也应该看看,防患于未然! 问题描述 之前有朋友提出Spring C ...

  6. spring cloud -- 5.分布式配置中心(Spring Cloud Config)

    此文章是看了方志朋老师的教材自己写的框架demo: 学习spring cloud推荐大家看下方志朋老师的史上最简单的 SpringCloud 教程 | 终章,地址如下: https://blog.cs ...

  7. spring clud config分布式配置中心的简单使用

    一.spring cloud config简介 微服务架构为什么要用配置中心? 配置的管理问题,没有配置中心的话,多个服务需要多次修改并依次启动,另外我们也希望修改配置后可以实时生效 配置内容的安全性 ...

  8. Spring Cloud构建微服务架构:分布式配置中心(加密解密)

    最近正好想发一篇关于配置中心加密的细节内容,结果发现基础的加密解密居然漏了,所以在这个入门系列中补充一下.后面再更新一下,使用配置中心的一些经验和教训. 在微服务架构中,我们通常都会采用DevOps的 ...

  9. Spring Boot 模板引擎FreeMarker集成

    Spring Boot 模板引擎FreeMarker集成 一.FreeMaker介绍 FreeMarker是一款免费的Java模板引擎,是一种基于模板和数据生成文本(HMLT.电子邮件.配置文件.源代 ...

  10. 从服务器基础环境配置到搭建Docker+Gitlab+Gitlab Runner,完整介绍Spring Boot项目的持续集成与持续交付具体实现!

    1. 序言 在大学的课程学习,非常注重团队协作的培养,在企业开发中,团队协作开发项目的场景更是甚多.另外,在当下的热门技术栈中,微服务开发模式.前后端分离开发模式逐渐盛行,Spring Boot.VU ...

最新文章

  1. 深蓝学院的深度学习理论与实践课程:第三章
  2. Spring Boot Bean的使用,@Repository,@Service,@Controller,@Component
  3. 自定义searchview包括修改图标样式
  4. Ueditor配置及在项目中的使用
  5. 【数据挖掘】基于密度的聚类方法 - DBSCAN 方法 ( DBSCAN 原理 | DBSCAN 流程 | 可变密度问题 | 链条现象 | OPTICS 算法引入 | 聚类层次 | 族序概念 )
  6. 车已经买有近一个月了,技术也在提升中
  7. linux C 快速排序法
  8. 基于java的数据结构学习——数组实现的栈以及简单应用
  9. 没有黑色版本?AirPods 2广告泄露:外型无变化
  10. redis相对于mysql有什么劣势
  11. Struts2学习笔记(九) 拦截器(Interceptor)(下)
  12. Elasticsearch2.2.0数据操作
  13. 服务器机房监控系统,服务器机房监控建设方案的介绍
  14. 解决vscode打开txt文件乱码
  15. 为串的模式匹配。模式匹
  16. 瀑布流插件masonry
  17. 智能管家项目总结(1)
  18. rust怎么发送求救信号_海上遇险怎么发求救信号?
  19. Java、计算税收(Java8)
  20. 2020年史上最全移动端Web整理从开发基础到实战(四)

热门文章

  1. 简单总结下8.25技术大会感受
  2. C++11中移动语义(std::move)和完美转发(std::forward)
  3. java 获取xml 版本号_java读取xml文件字段值
  4. config.o:文件无法辨识_HAZOP有哪些局限性,及系统生命周期不同阶段的危险辨识...
  5. 计算机在线拍照解答,摄影景深在线计算器
  6. deepinu盘制作工具_用U盘装系统,试试这款多系统启动盘制作工具!
  7. 计算机英语板卡,数据采集卡
  8. 每日一题(开开森森学前端之Object系列)
  9. Activity与Service之间交互并播放歌曲
  10. ubuntu14.04/Mint17上Gitolite的搭建过程