1.spring cloud config支持多种形式的统一配置管理,如git,subversion,jdbc,vault等多种形式的配置管理,接下来我们来说说mysql+springcloud config来实现统一配置管理。
2.首先是config server
buildscript {
ext {
springBootVersion = '2.0.0.RELEASE'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'com.gholly.spring.cofig.db.server'
version = '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
maven { url "https://repo.spring.io/milestone" }
}
ext {
springCloudVersion = 'Finchley.M7'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-actuator')
compile('org.springframework.cloud:spring-cloud-starter-config')
compile('org.springframework.cloud:spring-cloud-config-server')
compile('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
application.yml
server:
port: 8888
spring:
profiles:
active: jdbc
application:
name: config-server
cloud:
config:
label: master
server:
jdbc:
sql: SELECT `KEY`, `VALUE` from PROPERTIES where APPLICATION=? and PROFILE=? and LABEL=?
order: 0
default-label: master
datasource:
url: jdbc:mysql://127.0.0.1:3306/test?useSSL=false
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
maxActive: 50
java文件:
package com.gholly.config.db.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class,args);
}
}
mysql:
3.客户端
@RestController
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class,args);
}
@Value("${a.b}")
public String hh;
@RequestMapping("/hh")
public String test(){
return hh;
}
}
application.yml
server:
port: 8008
spring:
cloud:
config:
uri: http://localhost:8888/
profile: default
label: master
bus:
enabled: true
application:
name: config #此名字需与mysql中的application相对应
验证:
example地址为:
https://github.com/gholly/spring-config-server-db