java如何配置環境變量,Java配置分離之Spring遠程配置

 2023-12-06 阅读 27 评论 0

摘要:訪問我的博客 前言 集群應用的配置文件如果寫在項目的 resources 目錄下面,當遇到需要修改某一個配置值時,需要將集群的所有應用的配置信息進行修改,并且將機密的配置信息比如數據庫賬號密碼如果不進行加密配置在項目中很危險,一旦發生代碼泄露問

訪問我的博客

前言

集群應用的配置文件如果寫在項目的 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();@Overridepublic Properties getObject() throws Exception {return properties;}@Overridepublic Class<?> getObjectType() {return properties.getClass();}@Overridepublic boolean isSingleton() {return true;}@Overridepublic 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);}}
}

java如何配置環境變量、此類用于發送請求獲取配置信息,請求返回格式為 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"?>
<beansxmlns="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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://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:屬性 的方式獲取配置。

參考

  • http://www.cnblogs.com/yjmyzz/p/how-to-load-remote-config-in-spring.html

轉載于:https://www.cnblogs.com/vcmq/p/9484351.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/189726.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息