javascript
ACM 配置中心实战:Spring + MyBatis + Druid + ACM
很多基于 Spring MVC 框架的 Web 開(kāi)發(fā)中,Spring + MyBatis + Druid 是一個(gè)黃金組合,在此基礎(chǔ)上融入 ACM 配置中心,將會(huì)發(fā)生什么特別的變化呢?
本文將通過(guò)一個(gè)用戶信息查詢示例,演示在 Spring Web 項(xiàng)目中如何將數(shù)據(jù)庫(kù)連接池的配置存放到 ACM 中,統(tǒng)一運(yùn)維管控,達(dá)到配置治理與降低數(shù)據(jù)泄露風(fēng)險(xiǎn)的目的。
數(shù)據(jù)表
在測(cè)試數(shù)據(jù)庫(kù)中新建 user 表,其中包含用戶名稱(chēng)等字段,與接下來(lái)的 User model 類(lèi)相對(duì)應(yīng)。
CREATE TABLE `user` (`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,`name` varchar(10) NOT NULL DEFAULT '' COMMENT '名字',`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間',`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時(shí)間',PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用戶表';添加一行測(cè)試數(shù)據(jù):
INSERT INTO `user` (`name`, `create_time`, `update_time`) VALUES ('Nacos', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);Spring
該示例是 Spring 常規(guī)的 Web 項(xiàng)目,項(xiàng)目結(jié)構(gòu)如下:
pom.xml
引入 Nacos Spring 的依賴包 nacos-spring-context:
<dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-spring-context</artifactId><version>${latest.version}</version> </dependency>筆者在撰寫(xiě)本文時(shí),nacos-spring-context 的最新版本為:0.2.2-RC1
注:Nacos 是 ACM 的開(kāi)源版本。
dispatcher-servlet.xml
dispatcher-servlet.xml 為示例中 Spring MVC 的入口配置,在其中通過(guò) import 引入了 Nacos、Druid、MyBatis 的配置,其內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><mvc:annotation-driven/><context:annotation-config/><context:component-scan base-package="com.alibaba.nacos.example.spring"/><import resource="nacos.xml"/><import resource="datasource.xml"/><import resource="spring-config-mybatis.xml"/> </beans>nacos.xml
關(guān)鍵看 nacos.xml ,nacos-spring-context 的擴(kuò)展了 Spring 的 XML Schema 機(jī)制,自定義了 <nacos:property-source/> 等元素,詳細(xì)配置內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xmlns:nacos="http://nacos.io/schema/nacos"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://nacos.io/schema/nacoshttp://nacos.io/schema/nacos.xsd"><!-- 在命名空間詳情處可以獲取到 endpoint 和 namespace;accessKey 和 secretKey 推薦使用 RAM 賬戶的 --><nacos:global-properties endpoint="xxx" namespace="xxx" access-key="xxx" secret-key="xxx" /><nacos:property-source data-id="datasource.properties"/> </beans>其中通過(guò) <nacos:global-properties /> 設(shè)置 ACM Server 的連接地址,通過(guò) <nacos:property-source /> 從 ACM 配置中心加載了 dataId 為 datasource.properties 的配置,nacos-spring-context 會(huì)解析獲取到的配置內(nèi)容并添加到 Spring Environment 的 PropertySources 中,使得后續(xù)初始化 Druid 連接池的時(shí)候能獲取到數(shù)據(jù)庫(kù)連接地址、賬號(hào)密碼、初始連接池大小等信息。這就是 ACM 配置中心與 Spring 結(jié)合的關(guān)鍵點(diǎn)。
datasource.xml
這是數(shù)據(jù)庫(kù)連接池的配置,初始化了 DruidDataSource 的 Spring Bean,并將其通過(guò) DataSourceTransactionManager 設(shè)置為 Spring 的數(shù)據(jù)庫(kù)連接池。
<?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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"init-method="init" destroy-method="close"><property name="url" value="${datasource.url}"/><property name="username" value="${datasource.username}"/><property name="password" value="${datasource.password}"/><property name="initialSize" value="${datasource.initial-size}"/><property name="maxActive" value="${datasource.max-active}"/></bean><bean id="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><tx:annotation-driven transaction-manager="txManager"/> </beans>從上面的配置內(nèi)容看到,數(shù)據(jù)庫(kù)連接池的配置不需要因?yàn)橐?ACM 配置中做任何特殊的改變。
其他
User 的 Model、Service 等也跟不使用 ACM 配置中心時(shí)完全一致,這里就不一一貼出,完整示例代碼可以在 nacos-examples 獲取:nacos-spring-config-datasource-example
ACM
打開(kāi) ACM 的控制臺(tái),新增 dataId 為 datasource.properties 的配置,對(duì)應(yīng)上面 Spring 的 nacos.xml 中的 dataId。
配置內(nèi)容則與 Spring 的 datasource.xml 中的連接池屬性一一對(duì)應(yīng),示例如下:
datasource.url=jdbc:mysql://localhost:3306/test datasource.username=root datasource.password=root datasource.initial-size=1 datasource.max-active=20注:此配置內(nèi)容使得程序最終連接了本地的 MySQL,因此需要在本地安裝 MySQL,提前新建名稱(chēng)為 test 的數(shù)據(jù)庫(kù),并且按步驟一新建 user 表,添加測(cè)試數(shù)據(jù)。
運(yùn)行
示例中是 UserController#get() 通過(guò) UserServce 調(diào)用 Mybatis 的 Mapper 類(lèi)(UserMapper)從數(shù)據(jù)庫(kù)中查詢指定 ID 的用戶信息,假設(shè)該示例是運(yùn)行在端口為 8080 的 Tomcat 上,訪問(wèn):http://localhost:8080/users?id=1 地址將返回:
{"id": 1,"name": "Nacos" }總結(jié)
本文通過(guò)一個(gè)示例演示在“原生” Spring 中如何使用 ACM 配置中心,從示例可以看出,對(duì)原有的 Spring 項(xiàng)目基本沒(méi)有任何侵入,只需在 pom.xml 中添加 nacos-spring-context 的依賴,然后再定義并引入 nacos.xml 配置,就可以將數(shù)據(jù)庫(kù)連接池信息管控起來(lái),做到統(tǒng)一運(yùn)維,并降低數(shù)據(jù)泄露的風(fēng)險(xiǎn)。
試想,如果你有多個(gè)項(xiàng)目連接同一個(gè)數(shù)據(jù)庫(kù)或一個(gè)項(xiàng)目部署很多實(shí)例,當(dāng)數(shù)據(jù)庫(kù)密碼修改時(shí),你不需要去修改每個(gè)項(xiàng)目的 datasource.properties 文件,再走一次應(yīng)用的部署發(fā)布流程,而是到 ACM 的控制臺(tái)上修改一個(gè)配置項(xiàng),再去重啟應(yīng)用實(shí)例即可。當(dāng)然,如果你是自己管理數(shù)據(jù)庫(kù)連接池,則可以做到連“重啟應(yīng)用實(shí)例”都不需要了,只需在監(jiān)聽(tīng)到 ACM 配置變化時(shí)重新初始化數(shù)據(jù)庫(kù)連接池即可。
將 Spring 配置放置到 ACM 配置中,還能用上“動(dòng)態(tài)推送”、“版本管理”、“快速回滾”、“監(jiān)聽(tīng)查詢”、 “灰度發(fā)布”、“配置加密”、“權(quán)限管控”等功能,為 Spring + MyBatis + Druid 插上“飛翔”的翅膀。
完整示例代碼:
- nacos-spring-config-datasource-example
總結(jié)
以上是生活随笔為你收集整理的ACM 配置中心实战:Spring + MyBatis + Druid + ACM的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Spring JPA 开启原生sql打印
- 下一篇: Python命名空间