访问我的博客

前言

集群应用的配置文件如果写在项目的 resources 目录下面,当遇到需要修改某一个配置值时,需要将集群的所有应用的配置信息进行修改,并且将机密的配置信息比如数据库账号密码如果不进行加密配置在项目中很危险,一旦发生代码泄露问题,后果很严重。

为了避免上述情况发生,将配置信息存储到数据库中,比如数据库连接、用户名、以及密码,通过 Config 项目的一个接口提供获取配置信息。Config 项目只用于读取配置信息。

远程配置

一)新建类 RemoteProperties


  1. import com.alibaba.fastjson.JSONArray;
  2. import com.alibaba.fastjson.JSONObject;
  3. public class RemoteProperties implements InitializingBean, FactoryBean<Properties> {
  4. private String url = null;
  5. private Properties properties = new Properties();
  6. @Override
  7. public Properties getObject() throws Exception {
  8. return properties;
  9. }
  10. @Override
  11. public Class<?> getObjectType() {
  12. return properties.getClass();
  13. }
  14. @Override
  15. public boolean isSingleton() {
  16. return true;
  17. }
  18. @Override
  19. public void afterPropertiesSet() throws Exception {
  20. loadProperty();
  21. }
  22. public String getUrl() {
  23. return url;
  24. }
  25. public void setUrl(String url) {
  26. this.url = url;
  27. }
  28. private void loadProperty() {
  29. if (StringUtil.strIsNull(url)) return;
  30. String content = HttpClientUtil.urlGet(url);
  31. JSONObject object = JSONObject.parseObject(content);
  32. JSONArray data = object.getJSONArray("datasource");
  33. for (Object obj : data) {
  34. JSONObject jsonObject = (JSONObject) obj;
  35. String key = obj.getString("key");
  36. String value = obj.getString("value");
  37. properties.put(key, value);
  38. }
  39. }
  40. }

此类用于发送请求获取配置信息,请求返回格式为 JSON 的配置信息, 如:

  1. {
  2. "datasource":[
  3. {
  4. "value":"com.mysql.jdbc.Driver",
  5. "key":"jdbc.driver"
  6. },
  7. {
  8. "value":"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8",
  9. "key":"jdbc.url"
  10. },
  11. {
  12. "value":"root",
  13. "key":"jdbc.username"
  14. },
  15. {
  16. "value":"root",
  17. "key":"jdbc.password"
  18. }
  19. ]
  20. }

二)编写 Spring 配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans
  3. xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
  11. http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  13. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  14. <bean id="propertyConfigurerUserServer"
  15. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  16. <property name="properties">
  17. <bean id="remoteProperties" class="com.craft.partner.server.util.RemoteProperties"
  18. p:url="http://api.xxx.com/config"/>
  19. <!--远程配置提供接口-->
  20. </property>
  21. <!--还可以加载本地的properties文件-->
  22. <property name="locations">
  23. <list>
  24. <value>classpath:configure.properties</value>
  25. </list>
  26. </property>
  27. </bean>
  28. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
  29. p:driverClass="${jdbc.driver}"
  30. p:jdbcUrl="${jdbc.url}"
  31. p:user="${jdbc.username}"
  32. p:password="${jdbc.password}">
  33. <property name="initialPoolSize" value="10" />
  34. <property name="minPoolSize" value="10" />
  35. <property name="maxPoolSize" value="50" />
  36. <property name="maxStatements" value="0" />
  37. <property name="maxIdleTime" value="600" />
  38. <property name="idleConnectionTestPeriod" value="300" />
  39. <property name="acquireIncrement" value="5" />
  40. <property name="autoCommitOnClose" value="true" />
  41. <property name="checkoutTimeout" value="2000" />
  42. </bean>
  43. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  44. <property name="dataSource" ref="dataSource"></property>
  45. </bean>
  46. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  47. <tx:attributes>
  48. <tx:method name="save*" propagation="REQUIRED" />
  49. <tx:method name="update*" propagation="REQUIRED" />
  50. <tx:method name="delete*" propagation="REQUIRED" />
  51. <tx:method name="find*" read-only="true" />
  52. <tx:method name="*" propagation="REQUIRED" read-only="true"/><!--其他不符合规范的方法只允许读操作-->
  53. </tx:attributes>
  54. </tx:advice>
  55. <aop:config expose-proxy="false">
  56. <aop:pointcut id="serviceMethod" expression="execution(* com.craft.partner.server.service.*.*(..))"/>
  57. <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
  58. </aop:config>
  59. </beans>

注意: 通过 p:url="地址" 的方式,调用 RemoteProperties 的方法,发送请求获取配置信息,通过 Spring 进行注入。

注入后,可以通过 p:属性 的方式获取配置。

参考

Java配置分离之Spring远程配置的更多相关文章

  1. Java中如何获取spring中配置的properties文件内容

    有2种方式: 一. 1.通过spring配置properties文件 [java]  <bean id="propertyConfigurer"      class=&qu ...

  2. Java之ssh框架spring配置文件配置定时任务

    最近做了一个数据同步功能,要求晚上0点去定时同步数据,这是个老项目框架用的ssh,定时任务基于quartz,废话不多说,下面详细说说相关配置. 在spring的配置文件中: <!-- 0点定时任 ...

  3. Activiti配置实例以及Spring集成配置

    public class TestDB { public static void main(String[] args) { //1. 创建Activiti配置对象的实例 ProcessEngineC ...

  4. spring通过配置xml文件集成quartz定时器

    概述 Spring为创建Quartzde Scheduler.Trigger和JobDetail提供了方便的FactoryBean类,以便能够在Spring容器中享受注入的好处. 此外,Spring还 ...

  5. springcloud(五):Spring Cloud 配置中心的基本用法

    Spring Cloud 配置中心的基本用法 1. 概述 本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明. 配置中心 ...

  6. spring boot配置mybatis和事务管理

    spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...

  7. Spring+SprinMVC配置学习总结

    一千个人有一千种spring的配置方式,真是这样.看了好多的配置,试验了很多.这里做一个总结. 1 原理上,spring和springmvc可以合并为一个配置文件然后在web.xml中加载,因为最终的 ...

  8. spring 事务配置方式以及事务的传播性、隔离级别

    在前面的文章中总结了spring事务的5中配置方式,但是很多方式都不用而且当时的配置使用的所有参数都是默认的参数,这篇文章就看常用的两种事务配置方式并信息配置事务的传播性.隔离级别.以及超时等问题,废 ...

  9. 通过JVM 参数 实现spring 应用的二进制代码与配置分离。

    原创文章,转载请注明出处 分离的好处就不说了.说下分离的思路.通过JVM 参数-D 添加 config.path 的property 到系统中.系统通过System.getProperty(confi ...

随机推荐

  1. java基础-day13

    第01天 java面向对象 今日内容介绍 u 继承 u 抽象类 第1章   继承 1.1  继承的概述 在现实生活中,继承一般指的是子女继承父辈的财产.在程序中,继承描述的是事物之间的所属关系,通过继 ...

  2. java基础-day7

    第07天 面向对象基础 今日内容介绍 u 面向对象概述 u 面向对象特性之封装 u 面向对象之构造方法 u 类名作为形参和返回值案例 第1章   面向对象概述 1.1      面向对象思想 1.1. ...

  3. IntelliJ IDEA配置Tomcat(完整版教程)

    查找该问题的童鞋我相信IntelliJ IDEA,Tomcat的下载,JDK等其他的配置都应该完成了,那我直接进入正题了. 1.新建一个项目 2.由于这里我们仅仅为了展示如何成功部署Tomcat,以及 ...

  4. 最大m段子段和

    hdu1024 最大m子序列和 给定你一个序列,让你求取m个子段(不想交的子段)并求取这m个子段和的最大值 从二维开始来看dp[i][j]表示取第j个数作为第i个子段的元素所得到的前i个子段和的最大值 ...

  5. bootstrap2.2相关文档

    本节课我们主要学习一下 Bootstrap表单和图片功能,通过内置的 CSS定义,显示各种丰富的效果. 一.表单 Bootstrap提供了一些丰富的表单样式供开发者使用. 1.基本格式 //实现基本的 ...

  6. Android记录10--android.os.NetworkOnMainThreadException异常解决办法

    2013年11月1日小光棍节 有一段时间没有发表新的博客了,最近一直在忙着开发新浪微博客户端遇到很多问题比较头痛,比如说本篇博客要讲的NetworkOnMainThreadException这个异常, ...

  7. FastReport套打 和连续打印

    FastReport套打,纸张是连续的带锯齿的已经印刷好的,类似于通信公司发票这里设计的是客户销售记录.客户有两个要求:1.因为打印纸张是印刷的,明细记录只有8行,所以,如果明细记录如果不到8行,就将 ...

  8. 使用PerfView监测.NET程序性能(二):Perfview的使用

    在上一篇博客中,我们了解了对Windows及应用程序进行性能分析的基础:Event Trace for Windows (ETW).现在来看看基于ETW的性能分析工具——Perfview.exe Pe ...

  9. 搭建ELK集群

    环境准备 基础环境介绍 操作系统 部署应用 应用版本号 IP地址 主机名 CentOS 7.4 Elasticsearch/Logstash 6.4.3 192.168.1.1 elk1 CentOS ...

  10. 开发 C# OPC 客户端

    编写 opc 客户端的思路 1. 使用OPC Client浏览服务器, 查看测试代码修改后的结果. 2. 根据OPC Client搜集到的服务器信息编写代码和服务器交互 3. OPC Client 操 ...