Spring Cloud 微服务公共配置处理

Spring Cloud Config Server提供了微服务获取配置的功能,这些配置文件(application.yml或者application.properties)通常维护在git或者数据库中,而且支持通过RefreshScope动态刷新,使用起来还是比较灵活的。但是当微服务越来越多时,会遇到下面几个问题:

  1. 配置文件的敏感数如数据库地址和账号信息,据呈现在每个配置文件中,替换起来需要一个个配置文件进行修改。
  2. 各个微服务配置文件存在很多冗余配置(如Eureka,Feign),一旦这些部分调整,需要针对每个微服务进行调整,运维压力大增。

为了解决上述问题,我们可以从configServer服务着手进行改造,示意如下:

不同的服务ABC,不管是在配置中心仓库端配置了多少个文件,从ConfigServer返回的,一定是服务最终应用的配置。获取配置的方式,通常是调用ConfigServer的一个地址,如:

http://localhost:8021/common_rent/dev/aliyun_dev

common_rent是application name,dev是profile,aliyun_dev是label(git的分支)。这个地址的处理接口,是ConfigServer的EnvironmentController,所以通过拦截这个接口,将敏感信息或者公共配置抽取到configServer的application.yml, 返回前进行替换或者拼接,即可实现上述目的。

代码示例:

  1. 拦截器实现

    @Component
    @Aspect
    public class ResourceLoaderInterceptor {
    
    private static Log logger = LogFactory.getLog(ResourceLoaderInterceptor.class);
    @Resource
    ExternalProperties externalProperties;
    
    @Around("execution(* org.springframework.cloud.config.server..*Controller.*(..)) ")
    public Object commonPropertiesResolve(ProceedingJoinPoint joinPoint) throws Throwable {
        Object returnObj = null;
        Object[] args = joinPoint.getArgs();
        StopWatch stopWatch = new StopWatch();
        try {
            stopWatch.start();
            returnObj = joinPoint.proceed(args);
            if (Environment.class.isInstance(returnObj)) {
                Environment environment = (Environment) returnObj;
                if (environment.getPropertySources() != null && environment.getPropertySources().size() > 0) {
                    for (PropertySource propertySource : environment.getPropertySources()) {
                        placeHolderResolve((Map<String, String>) propertySource.getSource());
                    }
                }
            }
        } catch (Throwable throwable) {
            logger.error(ExceptionUtils.getStackTrace(throwable));
        } finally {
            stopWatch.stop();
            System.out.println(stopWatch.getTotalTimeMillis());
        }
        return returnObj;
    
    }
    
    private void placeHolderResolve(Map<String, String> source) {
        Map<String, String> placeHolders = collectConfigSet();
        for (String key : source.keySet()) {
            String value = source.get(key);
            if (value != null && value.contains("#{")) {
                for (String variable : placeHolders.keySet()) {
                    String vk = "#{" + variable + "}";
                    value = StringUtils.replace(value, vk, placeHolders.get(variable));
                }
                source.put(key, value);
            }
        }
    }
    
    private Map<String, String> collectConfigSet() {
        Map<String, String> placeHolders = new HashMap<>();
        Field[] fields = ExternalProperties.class.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            try {
                Field propertiesField = fields[i];
                ResourcePrefix resourcePrefix = propertiesField.getAnnotation(ResourcePrefix.class);
                String prefix = resourcePrefix.value();
                ExtDataSource extDataSource = (ExtDataSource) BeanUtils.getPropertyDescriptor(ExternalProperties.class, propertiesField.getName()).getReadMethod().invoke(externalProperties);
                if (extDataSource != null) {
                    Field[] fields2 = ExtDataSource.class.getDeclaredFields();
                    for (Field datasourceField : fields2) {
                        try {
                            ResourcePrefix annotation = datasourceField.getAnnotation(ResourcePrefix.class);
                            String suffix = annotation.value();
                            String sourceFieldValue = (String) BeanUtils.getPropertyDescriptor(ExtDataSource.class, datasourceField.getName()).getReadMethod().invoke(extDataSource);
                            if (StringUtils.isNotEmpty(sourceFieldValue)) {
                                placeHolders.put(prefix + "." + suffix, sourceFieldValue);
                            }
                        } catch (Exception e) {
                            logger.error(ExceptionUtils.getStackTrace(e));
                        }
                    }
                }
            } catch (Exception e) {
                logger.error(ExceptionUtils.getStackTrace(e));
            }
        }
        return placeHolders;
    }
    }
  2. ExternalProperites实现
@ConfigurationProperties(prefix = "external", ignoreUnknownFields = true)
public class ExternalProperties implements Serializable {

    @ResourcePrefix(value = "spring.datasource")
    private ExtDataSource datasource;
    @ResourcePrefix(value = "spring.data.mongodb")
    private ExtDataSource mongodb;
    @ResourcePrefix(value = "spring.data.redis")
    private ExtDataSource redis;
    @ResourcePrefix(value = "spring.rabbitmq")
    private ExtDataSource rabbitmq;

    public ExtDataSource getDatasource() {
        return datasource;
    }

    public void setDatasource(ExtDataSource datasource) {
        this.datasource = datasource;
    }

    public ExtDataSource getRabbitmq() {
        return rabbitmq;
    }

    public void setRabbitmq(ExtDataSource rabbitmq) {
        this.rabbitmq = rabbitmq;
    }

    public ExtDataSource getMongodb() {
        return mongodb;
    }

    public void setMongodb(ExtDataSource mongodb) {
        this.mongodb = mongodb;
    }

    public ExtDataSource getRedis() {
        return redis;
    }

    public void setRedis(ExtDataSource redis) {
        this.redis = redis;
    }
}
  1. ExtDataSource实现

    public class ExtDataSource {
    @ResourcePrefix(value = "host")
    private String host;
    @ResourcePrefix(value = "port")
    private String port;
    @ResourcePrefix(value = "url")
    private String url;
    @ResourcePrefix(value = "uri")
    private String uri;
    @ResourcePrefix(value = "username")
    private String userName;
    @ResourcePrefix(value = "password")
    private String password;
    
    public String getUrl() {
        return url;
    }
    
    public void setUrl(String url) {
        this.url = url;
    }
    
    public String getHost() {
        return host;
    }
    
    public void setHost(String host) {
        this.host = host;
    }
    
    public String getPort() {
        return port;
    }
    
    public void setPort(String port) {
        this.port = port;
    }
    
    public String getUri() {
        return uri;
    }
    
    public void setUri(String uri) {
        this.uri = uri;
    }
    
    public String getUserName() {
        return userName;
    }
    
    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    }
  2. ResourcePrefix实现
    @Target({ElementType.FIELD, ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface ResourcePrefix {
    String value();
    }

然后在configServer的application.yml中增加相关信息,如

external:
  datasource:
    host: 122.122.111.111
    port: 3307
    userName: usr
    password: pwd
  mongodb:
    host: 122.122.111.111
    port: 20467
    uri: 122.122.111.111:20467,122.122.111.112:20467,122.122.111.112:20467
    userName: usr
    password:  pwd
  redis:
    uri: 122.122.111.113:6379,122.122.112.113:6379,122.122.111.113:6379
    password: redispassword
  rabbitmq:
    host: 122.122.111.113
    port: 20467
    userName: usr
    password: pwd

将ServiceA的配置文件serviceA_dev.yml中的数据库相关信息替换成变量,以mysql为例
spring.datasource.uri: url: jdbc:mysql://#{spring.datasource.host}:#{spring.datasource.port}/dbName?useUnicode=true&characterEncoding=utf8,
serviceB和serviceC配置文件做同样处理,即可实现一次性替换。

后续如果需要增加公共配置,可以直接在ConfigServer的配置中间中增加,调整下拦截器的实现逻辑即可。

原文地址:https://blog.51cto.com/10705830/2431461

时间: 2024-08-01 01:19:37

Spring Cloud 微服务公共配置处理的相关文章

Spring Cloud微服务集成配置中心

1. 搭建Spring Cloud Config配置中心(见上一篇博客) 2. 创建微服务项目bounter-simon-app,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-ins

Servlet+MyBatis项目转Spring Cloud微服务,多数据源配置修改建议

一.项目需求 在开发过程中,由于技术的不断迭代,为了提高开发效率,需要对原有项目的架构做出相应的调整. 二.存在的问题 为了不影响项目进度,架构调整初期只是把项目做了简单的maven管理,引入springboot并未做spring cloud微服务处理.但随着项目的进一步开发,急需拆分现有业务,做微服务处理.因此架构上的短板日益突出.spring cloud config 无法完全应用,每次项目部署需要修改大量配置文件.严重影响开发效率,因此便萌生了对项目架构再次调整的决心. 三.调整建议 为了

关于Spring Cloud微服务架构

微服务架构 Spring Cloud解决的第一个问题就是:服务与服务之间的解耦.很多公司在业务高速发展的时候,服务组件也会相应的不断增加.服务和服务之间有着复杂的相互调用关系,经常有服务A调用服务B,服务B调用服务C和服务D ...,随着服务化组件的不断增多,服务之间的调用关系成指数级别的增长,这样最容易导致的情况就是牵一发而动全身.经常出现由于某个服务更新而没有通知到其它服务,导致上线后惨案频发.这时候就应该进行服务治理,将服务之间的直接依赖转化为服务对服务中心的依赖.Spring Cloud

Spring Cloud微服务接口这么多怎么调试?

前言 今天和大家聊一下Spring Cloud微服务下服务接口调试及管理的话题!我们知道在微服务架构下,软件系统会被拆分成很多个独立运行的服务,而这些服务间需要交互通信,就需要定义各种各样的服务接口.具体来说,在基于Spring Cloud的微服务模式中,各个微服务会基于Spring MVC的Controller定义多个该微服务需要向外部发布的接口. 根据各个微服务功能边界定义的不同,有些微服务会提供与具体业务相关的接口,如支付接口.账户接口等:而有些微服务则会提供一些公共性质的服务接口,如短信

Spring Cloud微服务Sentinel+Apollo限流、熔断实战

在Spring Cloud微服务体系中,由于限流熔断组件Hystrix开源版本不在维护,因此国内不少有类似需求的公司已经将眼光转向阿里开源的Sentinel框架.而以下要介绍的正是作者最近两个月的真实项目实践过程,这中间被不少网络Demo示例级别水文误导过,为了以正视听特将实践过程加以总结,希望能够帮到有类似需要的朋友! 一.Sentinel概述 在基于Spring Cloud构建的微服务体系中,服务之间的调用链路会随着系统的演进变得越来越长,这无疑会增加了整个系统的不可靠因素.在并发流量比较高

Spring Cloud微服务架构在互联网中应用

夜行侠老师录制的:Spring Cloud微服务架构在互联网中应用 由大象分享网出版:http://www.itjoin.org/course/detail/5934a58c0cf2159b39641f80夜行侠课程集合:http://www.xuetuwuyou.com/user/29 第1节.Springcloud介绍第2节.Eureka的使用第3节.Eureka集群第4节.restful请求第5节.restful请求负载均衡第6节.配置中心第7节.获取配置中心数据第8节.配置中心高可用第9

Spring Cloud微服务架构在互联网中应用_SpringCloud视频教程

Spring Cloud微服务架构在互联网中应用 课程学习地址:http://www.xuetuwuyou.com/course/177 课程出自学途无忧网:http://www.xuetuwuyou.com 一.课程涉及的软件及版本: springcloud版本Dalston.SR1 springboot版本1.5.2 jdk1.8 spring4.3.7 二.适合人群: ①想学分布式微服务架构 ②想学springcloud,spring data flow ③想构建稳定的分布式微服务架构 三

spring cloud微服务分布式云架构集成项目

Spring Cloud集成项目有很多,下面我们列举一下和Spring Cloud相关的优秀项目,我们的企业架构中用到了很多的优秀项目,说白了,也是站在巨人的肩膀上去整合的.在学习Spring Cloud之前大家必须了解一下相关项目,希望可以帮助到大家. Spring Cloud Config 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. Spring Cloud Bus 事件.消息总线,用于在集群(例如,配置变化事件)中传

Spring Cloud微服务分布式云架构-集成项目简介

Spring Cloud集成项目有很多,下面我们列举一下和Spring Cloud相关的优秀项目,我们的企业架构中用到了很多的优秀项目,说白了,也是站在巨人的肩膀上去整合的.在学习Spring Cloud之前大家必须了解一下相关项目,希望可以帮助到大家. Spring Cloud Config 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. Spring Cloud Bus ?事件.消息总线,用于在集群(例如,配置变化事件)中