1. 程式人生 > >Spring Cloud Config(三):基於JDBC搭建配置中心

Spring Cloud Config(三):基於JDBC搭建配置中心

1、簡介

本文主要內容是基於jdbc搭建配置中心,使應用從配置中心讀取配置資訊併成功註冊到註冊中心,關於配置資訊表結構僅供參考,大家可以根據具體需要進行擴充套件。

2、Config Server 搭建

2.1、Maven 依賴

因為需要從資料庫讀取配置檔案,所以需要新增MySQL的依賴

 <dependency>
    <groupId>mysql</groupId>  
    <artifactId>mysql-connector-java</artifactId>
</dependency>
 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
 </dependency>

2.2、JDBC 資料庫準備

這裡的表結構只為測試使用,具體可根據業務需要進行調整,此結構僅供參考

-- ----------------------------
-- Table structure for properties
-- ----------------------------
DROP TABLE IF EXISTS `properties`;
CREATE TABLE `properties` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `key` varchar(50) DEFAULT NULL,
  `value` varchar(500) DEFAULT NULL,
  `application` varchar(50) DEFAULT NULL,
  `profile` varchar(50) DEFAULT NULL,
  `label` varchar(50) DEFAULT NULL,
  `remark` varchar(50) CHARACTER SET utf8 DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of properties
-- ----------------------------
BEGIN;
INSERT INTO `properties` VALUES (2, 'eureka.client.serviceUrl.defaultZone', '${EUREKA_SERVICE_URL:http://localhost:8888}/eureka/', 'eureka-client', 'dev', 'v0.0.1', '配置中心地址');
INSERT INTO `properties` VALUES (3, 'management.endpoint.conditions.enabled', 'true', 'eureka-client', 'dev', 'v0.0.1', '啟用終結點');
INSERT INTO `properties` VALUES (4, 'eureka.instance.prefer-ip-address', 'true', 'eureka-client', 'dev', 'v0.0.1', '使用IP地址註冊到註冊中心');
INSERT INTO `properties` VALUES (5, 'spring.application.name', 'eureka-client', 'eureka-client', 'dev', 'v0.0.1', '應用名稱');
INSERT INTO `properties` VALUES (6, 'eureka.instance.instanceId', '${spring.application.name}@${spring.cloud.client.ip-address}@${server.port}', 'eureka-client', 'dev', 'v0.0.1', '在註冊中心的例項ID');
INSERT INTO `properties` VALUES (7, 'management.endpoints.web.exposure.include', '*', 'eureka-client', 'dev', 'v0.0.1', '開放哪些監控埠');
INSERT INTO `properties` VALUES (8, 'server.port', '8000', 'eureka-client', 'dev', 'v0.0.1', '應用服務埠號');
COMMIT;


2.3、Config Server 配置

server.port=1000
spring.application.name=lkf-cloud-config-jdbc
spring.profiles.active=jdbc

#資料庫配置
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/lkf_cloud?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#讀取配置檔案的SQL語句
spring.cloud.config.server.jdbc.sql=SELECT `key`,`value` FROM properties WHERE application=? AND PROFILE=? AND label=?
# 配置中心api字首
spring.cloud.config.server.prefix=lkf

啟動配置中心,訪問 http://localhost:1000/lkf/eureka-client/dev/v0.0.1,得到應用為 eureka-client ,開放環境【dev】,版本號為【v0.0.1】 的配置資訊

{
	"name": "eureka-client",
	"profiles": ["dev"],
	"label": "v0.0.1",
	"version": null,
	"state": null,
	"propertySources": [{
		"name": "eureka-client-dev",
		"source": {
			"eureka.client.serviceUrl.defaultZone": "${EUREKA_SERVICE_URL:http://localhost:8888}/eureka/",
			"management.endpoint.conditions.enabled": "true",
			"eureka.instance.prefer-ip-address": "true",
			"spring.application.name": "eureka-client",
			"eureka.instance.instanceId": "${spring.application.name}@${spring.cloud.client.ip-address}@${server.port}",
			"management.endpoints.web.exposure.include": "*",
			"server.port": "8000"
		}
	}]
}

至此,基於jdbc的配置中心已經成功從MySQL資料庫讀取配置資訊,配置中心搭建完成

3、Config Client 配置

3.1、Maven 依賴

 <!--註冊中心客戶端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--actuator 監控-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--配置中心客戶端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

3.2、Config Client 配置

#配置環境
spring.cloud.config.profile=dev
#配置中心地址
spring.cloud.config.uri=http://localhost:1000/lkf
#配置檔名稱
spring.cloud.config.name=eureka-client
#配置檔案版本號
spring.cloud.config.label=v0.0.1

啟動應用,在瀏覽器訪問註冊中心:http://127.0.0.1:8888,發現應用已成功從註冊中心讀取配置檔案並註冊到註冊中心
在這裡插入圖片描述