本文共 3716 字,大约阅读时间需要 12 分钟。
Spring Cloud Config?Spring Cloud????????????????????????????Git?????????????????????MySQL???????????????????MySQL????????
?????MySQL??????????????????MySQL???
mysql -u root -p
??MySQL root????????MySQL??????
??MySQL????????????
CREATE DATABASE config;USE config;CREATE TABLE config ( id INT PRIMARY KEY AUTO_INCREMENT, key1 VARCHAR(500) NOT NULL, value1 VARCHAR(500) NOT NULL, application VARCHAR(50) NOT NULL, profile VARCHAR(50) NOT NULL, label VARCHAR(50) DEFAULT NULL);
????pom.xml?????????
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-config-server mysql mysql-connector-java 5.1.21 org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-data-jpa
??application.properties??bootstrap.properties?????????
spring: datasource: url: jdbc:mysql://localhost:3306/config username: root password: root driver-class-name: com.mysql.jdbc.Driver cloud: config: server: jdbc: sql: "select key1,value1 from config where application=? and profile=? and label=?" default-label: master application: name: config-serverserver: port: 9060
????????????
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class); }} ?????
mvn spring-boot:run
????????????http://localhost:9060?
??????pom.xml?
org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-config
??bootstrap.yml???
spring: application: name: config-consumer cloud: config: uri: http://localhost:9060 fail-fast: true profiles: active: devmanagement: endpoints: web: exposure: include: '*'
????????
import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/config/consumer")@RefreshScopepublic class ConsumerController { @Value("${hello}") private String hello; @RequestMapping("/test") public String testConfig() { return String.format("hello is %s", hello); }} ?????
mvn spring-boot:run
????????????????
http://localhost:9060/config/consumer/test
?MySQL??????????????
SELECT * FROM config WHERE application='config-server' AND profile='dev';
??????????????????MySQL?Spring Cloud Config???????????????????????????????????????????????????????????????
转载地址:http://iyme.baihongyu.com/