Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

第 7 章 Spring Cloud

目錄

7.1. Spring Cloud Config
7.1.1. Maven 項目 pom.xml 檔案
7.1.2. Server
7.1.2.1. Maven config 模組
7.1.2.2. Application
7.1.2.3. application.properties
7.1.2.4. Git 倉庫
7.1.2.5. 測試伺服器
7.1.3. Client
7.1.3.1. Maven pom.xml
7.1.3.2. Application
7.1.3.3. bootstrap.properties
7.1.3.4. 測試 client
7.1.4. Config 高級配置
7.1.4.1. 倉庫配置
7.1.4.1.1. 分支
7.1.4.1.2. basedir
7.1.4.1.3. HTTP Auth
7.1.4.1.4. 本地git倉庫
7.1.4.1.5. native 本地配置
7.1.4.2. Config server 用戶認證
7.1.4.2.1. Server 配置
7.1.4.2.2. Client 配置
7.1.4.3. 加密敏感數據
7.1.4.4. Spring Cloud Config JDBC Backend
7.1.4.4.1. Maven pom.xml
7.1.4.4.2. 資料庫表結構
7.1.4.4.3. Config 伺服器
7.1.4.4.4. application.properties
7.1.5. Old
7.1.5.1. Server (Camden.SR5)
7.1.5.2. Client (Camden.SR5)
7.2. Spring Cloud Consol
7.2.1. Spring Cloud Consul 配置
7.2.2. Maven 父項目
7.2.3. Consul 服務生產者
7.2.3.1. Maven
7.2.3.2. application.properties
7.2.3.3. SpringApplication
7.2.3.4. TestController
7.2.4. Consul 服務消費者
7.2.4.1. Maven
7.2.4.2. application.properties
7.2.4.3. SpringApplication
7.2.4.4. TestController
7.2.5. Openfeign
7.2.5.1. Maven
7.2.5.2. application.properties
7.2.5.3. SpringApplication
7.2.5.4. Feign 介面
7.2.5.5. TestController
7.3. Spring Cloud Netflix
7.3.1. Eureka Server
7.3.1.1. Maven
7.3.1.2. Application
7.3.1.3. application.properties
7.3.1.4. 檢查註冊伺服器
7.3.2. Eureka Client
7.3.2.1. Maven
7.3.2.2. Application
7.3.2.3. RestController
7.3.2.4. application.properties
7.3.2.5. 測試
7.3.3. Feign client
7.3.3.1. Maven
7.3.3.2. Application
7.3.3.3. interface
7.3.3.4. application.properties
7.3.3.5. 測試
7.3.3.6. fallback
7.3.4. 為 Eureka Server 增加用戶認證
7.3.4.1. Maven
7.3.4.2. application.properties
7.3.4.3. Eureka Client
7.3.4.4. Feign Client
7.3.5. Eureka 配置項
7.3.5.1. /eureka/apps
7.3.5.2. Eureka instance 配置項
7.3.5.3. Eureka client 配置項
7.3.5.4. Eureka Server配置項
7.3.6. ribbon
7.3.6.1.
7.3.6.2. LoadBalancerClient 實例
7.3.6.2.1. application.properties
7.3.6.2.2. LoadBalancerClient 獲取伺服器列表
7.3.6.3. Ribbon 相關配置
7.3.6.3.1. 內置負載均衡策略
7.3.7. 獲取 EurekaClient 信息
7.3.8. Zuul
7.3.8.1. Maven
7.3.8.2. EnableZuulProxy
7.3.8.3. application.yml
7.3.8.4. 負載均衡配置
7.4. Openfeign
7.4.1. Openfeign 掃瞄包配置
7.4.2. 用戶認證
7.4.3. 配置連接方式
7.4.4.
7.5. Spring Cloud Gateway
7.5.1. Gateway 例子
7.5.1.1. Maven
7.5.1.2. SpringApplication
7.5.1.3. application.yml
7.5.1.4. RouteLocator 方式
7.5.2. 路由配置
7.5.2.1. 轉發操作
7.5.2.2. URL 參數
7.6. Spring Cloud Stream
7.7. Spring Cloud Bus
7.8. Spring Cloud Sleuth
7.9. Spring Cloud 相關的 application.properties 配置
7.9.1. 啟用或禁用 bootstrap
7.9.2. bootstrap.properties 配置檔案
7.10. Spring Cloud with Kubernetes
7.10.1. Config
7.10.1.1. Maven 依賴
7.10.1.2. Spring Cloud 配置檔案
7.10.1.3. 程序檔案
7.10.1.3.1. SpringBootApplication 啟動檔案
7.10.1.3.2. 配置類
7.10.1.3.3. 控製器
7.10.1.4. Kubernetes 編排腳本
7.10.1.5. 測試
7.10.2. 註冊發現
7.10.2.1. Maven 父項目
7.10.2.2. provider
7.10.2.2.1. Maven 依賴
7.10.2.2.2. Springboot 啟動類
7.10.2.2.3. 控製器
7.10.2.2.4. application.properties 配置檔案
7.10.2.2.5. Kubernetes provider 編排腳本
7.10.2.3. consumer
7.10.2.3.1. Maven 依賴
7.10.2.3.2. Springboot 啟動類
7.10.2.3.3. 控製器
7.10.2.3.4. FeignClient 介面
7.10.2.3.5. application.properties 配置檔案
7.10.2.3.6. Kubernetes consumer 編排腳本
7.10.2.4. 測試
7.10.2.5.
7.11. FAQ
7.11.1. Cannot execute request on any known server
7.11.2. @EnableDiscoveryClient與@EnableEurekaClient 區別
7.11.3. Feign請求超時
7.11.4. 已停止的微服務節點註銷慢或不註銷
7.11.5. Feign 啟動出錯 PathVariable annotation was empty on param 0.
7.11.6. Feign 提示 Consider defining a bean of type 'common.feign.Cms' in your configuration.
7.11.7. Load balancer does not have available server for client
7.11.8. Eureka Client (Dalston.SR1)
7.11.8.1. Maven
7.11.8.2. Application
7.11.8.3. RestController
7.11.8.4. application.properties
7.11.8.5. 測試
7.11.9. Config Server(1.3.1.RELEASE)
7.11.9.1. Server
7.11.9.1.1. Maven
7.11.9.1.2. Application
7.11.9.1.3. application.properties
7.11.9.1.4. Git 倉庫
7.11.9.1.5. 測試伺服器
7.11.9.2. Client
7.11.9.2.1. Maven pom.xml
7.11.9.2.2. Application
7.11.9.2.3. bootstrap.properties
7.11.9.2.4. 測試 client

7.1. Spring Cloud Config

7.1.1. Maven 項目 pom.xml 檔案

		
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>cn.netkiller</groupId>
	<artifactId>microservice</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>pom</packaging>

	<name>microservice</name>
	<url>http://www.netkiller.cn</url>
	<description>Demo project for Spring Boot</description>

	<organization>
		<name>Netkiller Spring Cloud 手札</name>
		<url>http://www.netkiller.cn</url>
	</organization>

	<developers>
		<developer>
			<name>Neo</name>
			<email>netkiller@msn.com</email>
			<organization>Netkiller Spring Cloud 手札</organization>
			<organizationUrl>http://www.netkiller.cn</organizationUrl>
			<roles>
				<role>Author</role>
			</roles>
		</developer>
	</developers>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>14</java.version>
		<maven.compiler.source>${java.version}</maven.compiler.source>
		<maven.compiler.target>${java.version}</maven.compiler.target>
		<maven.compiler.release>${java.version}</maven.compiler.release>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.3.3.RELEASE</version>
		<relativePath />
	</parent>

	<repositories>
		<repository>
			<id>alimaven</id>
			<name>Maven Aliyun Mirror</name>
			<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Hoxton.SR8</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>
	</dependencies>

	<modules>
		<module>eureka</module>
		<module>gateway</module>
		<module>config</module>
		<module>webflux</module>
		<module>openfeign</module>
		<module>restful</module>
	</modules>
</project>		
		
		

7.1.2. Server

7.1.2.1. Maven config 模組

			
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.netkiller</groupId>
		<artifactId>microservice</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>cn.netkiller</groupId>
	<artifactId>config</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>config</name>
	<url>http://www.netkiller.cn</url>
	<description>Config project for Spring cloud</description>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>11</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<!-- <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-monitor</artifactId> </dependency> -->
		<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> -->
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<mainClass>cn.netkiller.config.Application</mainClass>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>
			
			

7.1.2.2. Application

Application

			
package cn.netkiller.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class Application {
	public static void main(String[] args) {
		System.out.println("Config Server Starting...");
		SpringApplication.run(Application.class, args);
	}
}
			
			

7.1.2.3. application.properties

			
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/netkiller/config.git
			
			

7.1.2.4. Git 倉庫

克隆倉庫

		
git clone https://github.com/netkiller/config.git
		
			

創建配置檔案 server-development.properties

		
vim server-development.properties

test.a=KKOOKK
message=Hello world
		
			

提交配置檔案

		
git commit -a
git push
		
			

7.1.2.5. 測試伺服器

			
neo@netkiller $ curl http://localhost:8888/server-development.json
{"message":"Hello world","test":{"a":"KKOOKK"}}
			
			

7.1.3. Client

7.1.3.1. Maven pom.xml

			
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>cn.netkiller</groupId>
		<artifactId>microservice</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<groupId>cn.netkiller</groupId>
	<artifactId>restful</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>restful</name>
	<url>http://maven.apache.org</url>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>
	<dependencies>
		<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-web</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<mainClass>cn.netkiller.Application</mainClass>
				</configuration>
			</plugin>
			<plugin>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>		
			
			

7.1.3.2. Application

			
package cn.netkiller.cloud.client;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}
}

@RefreshScope
@RestController
class MessageRestController {

	@Value("${message:Hello default}")
	private String message;

	@RequestMapping("/message")
	String getMessage() {
		return this.message;
	}
}			
			
			

注意 @RefreshScope 註解

7.1.3.3. bootstrap.properties

			
spring.application.name=server-development
spring.cloud.config.uri=http://localhost:8888
management.security.enabled=false			
			
			

7.1.3.4. 測試 client

						
neo@netkiller $ curl http://localhost:8080/message.json
Hello world
			
			

7.1.4. Config 高級配置

7.1.4.1. 倉庫配置

配置檔案格式

		
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties		
		
			

{application} 映射到客戶端的"spring.application.name" 或 “spring.cloud.config.name”;

{profile}映射到客戶端上的"spring.profiles.active" 或 “spring.cloud.config.profile”;

{label} 是可選的 git 標籤,預設 master;

			
nickname: netkilleriMac:Java neo$ curl http://localhost:8769/webflux-dev.json
{"name":"Neo","nickname":"netkiller"}
			
iMac:Java neo$ curl http://localhost:8769/webflux-dev.properties
name: Neo
nickname: netkiller

iMac:Java neo$ curl http://localhost:8769/webflux-dev.yml
name: Neo
nickname: netkiller

iMac:Java neo$ curl http://localhost:8769/webflux-dev.yaml
name: Neo
nickname: netkiller
			
			
7.1.4.1.1. 分支

label 是指 git 的分支

			
spring.cloud.config.label=mybranch
			
				
7.1.4.1.2. basedir
			
spring.cloud.config.server.git.basedir=api/configs
			
				
7.1.4.1.3. HTTP Auth
			
spring.application.name=config-server
spring.cloud.config.server.git.uri=https://netkiller:xxxxxx@github.com/xyz/microservices-configs.git
			
				

			
spring.application.name=config-server
spring.cloud.config.server.git.uri=https://github.com/xyz/microservices-configs.git
spring.cloud.config.server.git.username=netkiller
spring.cloud.config.server.git.password=password
			
				
7.1.4.1.4. 本地git倉庫

創建本地倉庫

			
mkdir ~/config
cd config
git init
git config --global user.email "neo.chen@live.com"
git config --global user.name "Neo Chen"
			
				

創建測試配置檔案

			
# cat app-test.properties 
name=neo
age=10
			
				

提交配置檔案

			
git add app-test.properties 
git commit -a
			
				

檢查檔案是否提交成功

			
[root@netkiller config]# git log
commit aee6c35bacf1740004e02f8ecdcf2fd322422405
Author: Neo Chen <neo.chen@live.com>
Date:   Thu Nov 2 14:18:48 2017 +0800

        new file:   app-test.properties
			
				

配置 Spring cloud config 伺服器,修改 application.properties 檔案

			
server.port=8888
#spring.cloud.config.server.git.uri=/opt/config
spring.cloud.config.server.git.uri= file://${user.home}/config
security.user.name=cfg
security.user.password=s3cr3t

## Spring cloud GIT Repository file
${user.home}/config/root-server.properties		
			
				

檢驗配置中心

			
[root@netkiller config]# curl http://cfg:test@localhost:8888/app-test.properties
age: 10
name: neo
			
				
7.1.4.1.5. native 本地配置

載入本地配置檔案 resources/shared/config-client-dev.yml

			
server:
  port: 8762
foo: foo version 1			
			
				

配置中心服務端 resources/application.yml 配置檔案

			
server:
  port: 8769
spring:
  application:
    name: config-server
  profiles:
    active: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared			
			
				

測試配置檔案

			
iMac:Java neo$ curl http://localhost:8769/config-client-dev.json
{"server":{"port":8762},"foo":"foo version 1"}
			
				

7.1.4.2. Config server 用戶認證

7.1.4.2.1. Server 配置
7.1.4.2.1.1. application.properties
				
server.port=8888
spring.cloud.config.server.git.uri=ssh://localhost/config-repo
spring.cloud.config.server.git.clone-on-start=true
security.user.name=cfg
security.user.password=s3cr3t
				
					
7.1.4.2.1.2. Maven
				
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>cn.netkiller</groupId>
	<artifactId>config</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>config</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.7.RELEASE</version>
		<relativePath />
	</parent>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-config</artifactId>
				<version>1.3.1.RELEASE</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
				
				
					
7.1.4.2.1.3. 測試是否生效
				
neo@MacBook-Pro ~/deployment % curl http://cfg:s3cr3t@localhost:8888/neo-development.json
{"message":"Hello world","test":{"name":"neo"}}		
				
					
7.1.4.2.2. Client 配置

bootstrap.properties:

			
spring.application.name=project
spring.profiles.active=development
spring.cloud.config.uri=http://localhost:8888
spring.cloud.config.username=cfg
spring.cloud.config.password=s3cr3t
			
				

7.1.4.3. 加密敏感數據

Config server 創建證書

		
keytool -genkeypair -alias config-server-key \
       -keyalg RSA -keysize 4096 -sigalg SHA512withRSA \
       -dname 'CN=Config Server,OU=Spring Cloud,O=Netkiller' \
       -keypass s3cr3t -keystore config-server.jks \
       -storepass passw0rd		
		
			

application.properties 中配置證書

		
# spring.cloud.config.server.encrypt.enabled=true
encrypt.key-store.location=classpath:/config-server.jks
encrypt.key-store.alias=config-server-key
encrypt.key-store.secret=s3cr3t
encrypt.key-store.password=passw0rd
		
			

測試加密

		
curl -X POST --data-urlencode mypassword http://localhost:8888/encrypt
		
			
		
$ PASSWORD=$(curl -X POST --data-urlencode passw0rd http://cfg:s3cr3t@localhost:8888/encrypt)
$ echo "user.password=$PASSWORD" >> api-interface-development.properties
$ git commit -am 'Added encrypted password'

# 刷新配置
$ curl -X POST http://cfg:s3cr3t@localhost:8888/refresh		
		
			

7.1.4.4. Spring Cloud Config JDBC Backend

7.1.4.4.1. Maven pom.xml
			
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.11.RELEASE</version>
    <relativePath/>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.flywaydb</groupId>
        <artifactId>flyway-core</artifactId>
        <version>5.0.3</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.21</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>			
			
				
7.1.4.4.2. 資料庫表結構
			
CREATE TABLE `properties` (
  `key` varchar(50) NOT NULL,
  `value` varchar(500) NOT NULL,
  `application` varchar(50) NOT NULL,
  `profile` varchar(50) NOT NULL,
  `label` varchar(50) NOT NULL,
  PRIMARY KEY (`KEY`, `APPLICATION`, `PROFILE`, `LABEL`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;			
			
				
7.1.4.4.3. Config 伺服器
			
@EnableConfigServer
@SpringBootApplication
public class Application {

	//@Autowired
	//JdbcTemplate jdbcTemplate;

    public static void main(String[] args) {
    	SpringApplication.run(Application.class, args);
        // 測試用數據,僅用於本文測試使用
        JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
        jdbcTemplate.execute("delete from properties");
        jdbcTemplate.execute("INSERT INTO properties VALUES('neo.message', 'helloworld', 'api', 'stage', 'master')");
        jdbcTemplate.execute("INSERT INTO properties VALUES('neo.message', 'helloworld', 'new', 'online', 'master')");
        jdbcTemplate.execute("INSERT INTO properties VALUES('neo.message', 'helloworld', 'test', 'online', 'develop')");
        jdbcTemplate.execute("INSERT INTO properties VALUES('neo.message', 'helloworld', 'cms', 'online', 'master')");
    }

}			
			
				
7.1.4.4.4. application.properties

spring.profiles.active=jdbc 將配置中心的存儲實現切換到jdbc的方式, 必須設置。

			
server.port=8888
spring.profiles.active=jdbc

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/config-server-db
spring.datasource.username=root
spring.datasource.password=xxxx

# or

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url= jdbc:postgresql://localhost:5432/configdb
spring.datasource.username=xxxxxx
spring.datasource.password=xxxxxx			
			
				

7.1.5. Old

7.1.5.1. Server (Camden.SR5)

Maven pom.xml 請使用最新版本 1.3.1, 下面的 maven 是早期 Camden.SR5 版本的配置

			
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>netkiller.cn</groupId>
	<artifactId>cloud</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Neo</name>
	<description>http://www.netkiller.cn</description>
	<packaging>jar</packaging>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.3.RELEASE</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>

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

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.SR5</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
			
			

7.1.5.2. Client (Camden.SR5)

Maven pom.xml Camden.SR5 為早期版本,盡可以使用新版

			
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>netkiller.cn</groupId>
	<artifactId>cloud</artifactId>
	<version>0.0.1-SNAPSHOT</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

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

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Camden.SR5</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>