在結合場景談服務發現和配置中咱們講述了 Nacos 配置中心的三個典型的應用場景,包括如何在 Spring Boot 中使用 Nacos 配置中心將數據庫鏈接信息管控起來,而在「原生」的 Spring 中能夠怎麼使用 Nacos 配置中心呢?不少基於 Spring MVC 框架的 Web 開發中,Spring + MyBatis + Druid 是一個黃金組合,在此基礎上融入 Nacos 配置中心,將會發生什麼特別的變化呢?html
本文將經過一個用戶信息查詢示例,演示在 Spring Web 項目中如何將數據庫鏈接池的配置存放到 Nacos 中,統一運維管控,達到配置治理與下降數據泄露風險的目的。mysql
在測試數據庫中新建 user
表,其中包含用戶名稱等字段,與接下來的 User model 類相對應。git
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 '建立時間', `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用戶表';
添加一行測試數據:github
INSERT INTO `user` (`name`, `create_time`, `update_time`) VALUES ('Nacos', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);
該示例是 Spring 常規的 Web 項目,項目結構以下:spring
引入 Nacos Spring 的依賴包 nacos-spring-context
:sql
<dependency> <groupId>com.alibaba.nacos</groupId> <artifactId>nacos-spring-context</artifactId> <version>${latest.version}</version> </dependency>
筆者在撰寫本文時,nacos-spring-context 的最新版本爲:0.2.2-RC1數據庫
dispatcher-servlet.xml
爲示例中 Spring MVC 的入口配置,在其中經過 import
引入了 Nacos、Druid、MyBatis 的配置,其內容以下:瀏覽器
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://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
,nacos-spring-context 的擴展了 Spring 的 XML Schema 機制,自定義了 <nacos:property-source/>
等元素,詳細配置內容以下:spring-mvc
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://nacos.io/schema/nacos http://nacos.io/schema/nacos.xsd"> <nacos:global-properties server-addr="127.0.0.1:8848" /> <nacos:property-source data-id="datasource.properties"/> </beans>
其中經過 <nacos:global-properties />
設置 Nacos Server 的鏈接地址,經過 <nacos:property-source />
從 Nacos 配置中心加載了 dataId 爲 datasource.properties
的配置,nacos-spring-context 會解析獲取到的配置內容並添加到 Spring Environment 的 PropertySources 中,使得後續初始化 Druid 鏈接池的時候能獲取到數據庫鏈接地址、帳號密碼、初始鏈接池大小等信息。這就是 Nacos 配置中心與 Spring 結合的關鍵點。mybatis
這是數據庫鏈接池的配置,初始化了 DruidDataSource
的 Spring Bean,並將其經過 DataSourceTransactionManager
設置爲 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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://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>
從上面的配置內容看一看到,數據庫鏈接池不須要由於引入 Nacos 配置中作任何特殊的改變。
User 的 Model、Service 等也跟不使用 Nacos 配置中心時徹底一致,這裏就不一一貼出,完整示例代碼能夠在 nacos-examples 獲取:https://github.com/nacos-group/nacos-examples/tree/master/nacos-spring-example/nacos-spring-config-datasource-example
參照 Nacos 官網的快速開始:https://nacos.io/zh-cn/docs/quick-start.html
從 Nacos 的 github 上下載最新穩定版本的 nacos-server:https://github.com/alibaba/nacos/releases
解壓後到 Nacos 的 bin 目錄下,執行 startup 啓動腳本以單機模式啓動 Nacos Server, Windows 請使用 cmd startup.cmd
,Linux/Unix/Mac 環境請使用 sh startup.sh -m standalone
。
啓動後,瀏覽器訪問:http://localhost:8848/nacos/index.html 就能夠來到 Nacos 的控制檯,新增 dataId 爲 datasource.properties
的配置,對應上面 Spring 的 nacos.xml
中的 dataId。
配置內容則與 Spring 的 datasource.xml
中的鏈接池屬性一一對應,示例以下:
datasource.url=jdbc:mysql://localhost:3306/test datasource.username=root datasource.password=root datasource.initial-size=1 datasource.max-active=20
示例中是 UserController#get()
經過 UserServce 調用 Mybatis 的 Mapper 類(UserMapper)從數據庫中查詢指定 ID 的用戶信息,假設該示例是運行在端口爲 8080 的 Tomcat 上,訪問:http://localhost:8080/users?id=1 地址將返回:
{ "id": 1, "name": "Nacos" }
本文經過一個示例演示在「原生」 Spring 中如何使用 Nacos 配置中心,從示例能夠看出,對原有的 Spring 項目基本沒有任何侵入,只需在 pom.xml 中添加 nacos-spring-context 的依賴,而後再定義並引入 nacos.xml
配置,就能夠將數據庫鏈接池信息管控起來,作到統一運維,並下降數據泄露的風險。
試想,若是你有多個項目鏈接同一個數據庫或一個項目部署不少實例,當數據庫密碼修改時,你不須要去修改每一個項目的 datasource.properties
文件,再走一次應用的部署發佈流程,而是到 Nacos 的控制檯上修改一個配置項,再去重啓應用實例便可。固然,若是你是本身管理數據庫鏈接池,則能夠作到連「重啓應用實例」都不須要了,只需在監聽到 Nacos 配置變化時從新初始化數據庫鏈接池便可。
將 Spring 配置放置到 Nacos 配置中,還能用上「動態推送」、「版本管理」、「快速回滾」、「監聽查詢」,以及後續的 「灰度發佈」、「配置加密」、「權限管控」等功能,爲 Spring + MyBatis + Druid 插上「飛翔」的翅膀。
做爲 Nacos 配置中心的阿里雲免費產品 ACM:https://www.aliyun.com/product/acm,已經提供了上面全部的功能,若是您不想本身部署運維 Nacos Server 或者想使用「推送軌跡」、「細粒度權限控制」、「ECS 實例 RAM 角色」等高級特性,不妨嘗試下免費的 ACM。
完整示例代碼: