Java配置分离之Spring远程配置
前言
集群应用的配置文件如果写在项目的 resources 目录下面,当遇到需要修改某一个配置值时,需要将集群的所有应用的配置信息进行修改,并且将机密的配置信息比如数据库账号密码如果不进行加密配置在项目中很危险,一旦发生代码泄露问题,后果很严重。
为了避免上述情况发生,将配置信息存储到数据库中,比如数据库连接、用户名、以及密码,通过 Config 项目的一个接口提供获取配置信息。Config 项目只用于读取配置信息。
远程配置
一)新建类 RemoteProperties
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
public class RemoteProperties implements InitializingBean, FactoryBean<Properties> {
private String url = null;
private Properties properties = new Properties();
@Override
public Properties getObject() throws Exception {
return properties;
}
@Override
public Class<?> getObjectType() {
return properties.getClass();
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public void afterPropertiesSet() throws Exception {
loadProperty();
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
private void loadProperty() {
if (StringUtil.strIsNull(url)) return;
String content = HttpClientUtil.urlGet(url);
JSONObject object = JSONObject.parseObject(content);
JSONArray data = object.getJSONArray("datasource");
for (Object obj : data) {
JSONObject jsonObject = (JSONObject) obj;
String key = obj.getString("key");
String value = obj.getString("value");
properties.put(key, value);
}
}
}
此类用于发送请求获取配置信息,请求返回格式为 JSON 的配置信息, 如:
{
"datasource":[
{
"value":"com.mysql.jdbc.Driver",
"key":"jdbc.driver"
},
{
"value":"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8",
"key":"jdbc.url"
},
{
"value":"root",
"key":"jdbc.username"
},
{
"value":"root",
"key":"jdbc.password"
}
]
}
二)编写 Spring 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="propertyConfigurerUserServer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<bean id="remoteProperties" class="com.craft.partner.server.util.RemoteProperties"
p:url="http://api.xxx.com/config"/>
<!--远程配置提供接口-->
</property>
<!--还可以加载本地的properties文件-->
<property name="locations">
<list>
<value>classpath:configure.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
p:driverClass="${jdbc.driver}"
p:jdbcUrl="${jdbc.url}"
p:user="${jdbc.username}"
p:password="${jdbc.password}">
<property name="initialPoolSize" value="10" />
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="50" />
<property name="maxStatements" value="0" />
<property name="maxIdleTime" value="600" />
<property name="idleConnectionTestPeriod" value="300" />
<property name="acquireIncrement" value="5" />
<property name="autoCommitOnClose" value="true" />
<property name="checkoutTimeout" value="2000" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="find*" read-only="true" />
<tx:method name="*" propagation="REQUIRED" read-only="true"/><!--其他不符合规范的方法只允许读操作-->
</tx:attributes>
</tx:advice>
<aop:config expose-proxy="false">
<aop:pointcut id="serviceMethod" expression="execution(* com.craft.partner.server.service.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
</aop:config>
</beans>
注意: 通过 p:url="地址" 的方式,调用 RemoteProperties 的方法,发送请求获取配置信息,通过 Spring 进行注入。
注入后,可以通过 p:属性 的方式获取配置。
参考
Java配置分离之Spring远程配置的更多相关文章
- Java中如何获取spring中配置的properties文件内容
有2种方式: 一. 1.通过spring配置properties文件 [java] <bean id="propertyConfigurer" class=&qu ...
- Java之ssh框架spring配置文件配置定时任务
最近做了一个数据同步功能,要求晚上0点去定时同步数据,这是个老项目框架用的ssh,定时任务基于quartz,废话不多说,下面详细说说相关配置. 在spring的配置文件中: <!-- 0点定时任 ...
- Activiti配置实例以及Spring集成配置
public class TestDB { public static void main(String[] args) { //1. 创建Activiti配置对象的实例 ProcessEngineC ...
- spring通过配置xml文件集成quartz定时器
概述 Spring为创建Quartzde Scheduler.Trigger和JobDetail提供了方便的FactoryBean类,以便能够在Spring容器中享受注入的好处. 此外,Spring还 ...
- springcloud(五):Spring Cloud 配置中心的基本用法
Spring Cloud 配置中心的基本用法 1. 概述 本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明. 配置中心 ...
- spring boot配置mybatis和事务管理
spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...
- Spring+SprinMVC配置学习总结
一千个人有一千种spring的配置方式,真是这样.看了好多的配置,试验了很多.这里做一个总结. 1 原理上,spring和springmvc可以合并为一个配置文件然后在web.xml中加载,因为最终的 ...
- spring 事务配置方式以及事务的传播性、隔离级别
在前面的文章中总结了spring事务的5中配置方式,但是很多方式都不用而且当时的配置使用的所有参数都是默认的参数,这篇文章就看常用的两种事务配置方式并信息配置事务的传播性.隔离级别.以及超时等问题,废 ...
- 通过JVM 参数 实现spring 应用的二进制代码与配置分离。
原创文章,转载请注明出处 分离的好处就不说了.说下分离的思路.通过JVM 参数-D 添加 config.path 的property 到系统中.系统通过System.getProperty(confi ...
随机推荐
- 准备用有人云平台和tlink.io云平台和电脑做云转发
初步想的是用有人做国网电表转发,用tlink.io做综合采集模块转发,耗时一天 然后用tlink.io的做二次前端开发,耗时两天 用有人做二次前端开发,耗时两天 最后可以试试用常见的OPC公网转发到这 ...
- Vue的路由设置
一.路由基础介绍 1.什么是前端路由? 路由是根据不同的url地址展示不同的内容或页面 前端路由就是把不同路由对应不同的内容或页面的任务交给前端来做,之前是通过服务器根据url的不同返回不同的页面实现 ...
- jQuery插件初级练习4
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- zoj2607
题意:如左图,给定A,B,C,D的面积分别为大于等于a,b,c,d,求最小的面积 思路:因为a,b肯定有一个是满的(不然还可压缩到更小),同理,ac,bd,cd都只有一个是满的,所以有可能是对角满的, ...
- 纯净得只剩下字的访问IP查询API
纯净得只剩下字的访问IP查询API 实用资源 / 2018-02-26 / 3 条评论 看到一个好玩的,就随手收藏一下,本API作用:获取用户真实IP,而获取用户IP常见的坑有两个,开发支付的时候也需 ...
- 用fastreport在进行多列打印时,默认是先行后列,如何改成先列后行排记录?
例子程序中的6.fr3是在Page中设置 columns=2这样就是先行后列,7.fr3就是3列先列后行的例子 1 6 2 7 3 8 4 9 5 10 但如果 ...
- Android-AndroidStudio-AVD启动不了-emulator: Process finished with exit code 1
注意:解决此错误目前只针对Windows系统的电脑: 1.AndroidStudio-->AVDManager(Create Virtual Device): 2.提示AVD启动不了,同时Eve ...
- UvaOJ 10167
暴力搜索 #include<cstdio> struct node { int x; int y; }s[]; int main() { //freopen("input.txt ...
- C#之简易计算器设计
在学完了C#的方法和数据类型之后,写了一个简易的计算器的界面.本次界面具备加减乘除求余等五项运算.不过存在一点缺陷就是无法判断输入数据的类型,是整数还是小数,由于目前所学知识有限,等学到以后再进行完善 ...
- 添加vscode自定义代码块
以vue为例 一.打开vscode>文件>首选项>用户代码片段>vue.json二.编写代码块 其中一行一句:$1是占位符,就是你可以输入的地方."http get& ...