博客
关于我
spring cloud config入门,spring cloud config mysql数据库配置配置中心
阅读量:345 次
发布时间:2019-03-04

本文共 3656 字,大约阅读时间需要 12 分钟。

Spring Cloud Config?MySQL??????

1. ?????

Spring Cloud Config?Spring Cloud????????????????????????????Git?????????????????????MySQL???????????????????MySQL????????

  • ????????????????????????
  • ?????????????????????????
  • ??????????????????

2. ????

2.1 ??MySQL

?????MySQL??????????????????MySQL???

mysql -u root -p

??MySQL root????????MySQL??????

2.2 ???????

??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);

3. Spring Boot????

3.1 pom.xml??

????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

3.2 ????

??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

4. ??????

????????????

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?

5. ???????

5.1 pom.xml??

??????pom.xml?

org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-config

5.2 ????

??bootstrap.yml???

spring:    application:        name: config-consumer    cloud:        config:            uri: http://localhost:9060            fail-fast: true    profiles:        active: devmanagement:    endpoints:        web:            exposure:                include: '*'

5.3 ?????

????????

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

6. ??

6.1 ??????

????????????????

http://localhost:9060/config/consumer/test

6.2 ???????

?MySQL??????????????

SELECT * FROM config WHERE application='config-server' AND profile='dev';

7. ??

??????????????????MySQL?Spring Cloud Config???????????????????????????????????????????????????????????????

转载地址:http://iyme.baihongyu.com/

你可能感兴趣的文章
OpenMCU(三):STM32F103 FreeRTOS移植
查看>>
OpenMCU(三):STM32F103 FreeRTOS移植
查看>>
OpenMCU(二):GD32E23xx FreeRTOS移植
查看>>
OpenMCU(五):STM32F103时钟树初始化分析
查看>>
OpenMCU(四):STM32F103启动汇编代码分析
查看>>
OpenMetadata 命令执行漏洞复现(CVE-2024-28255)
查看>>
OpenMMLab | AI玩家已上线!和InternLM解锁“谁是卧底”新玩法
查看>>
OpenMMLab | S4模型详解:应对长序列建模的有效方法
查看>>
OpenMMLab | 【全网首发】Llama 3 微调项目实践与教程(XTuner 版)
查看>>
OpenMMLab | 不是吧?这么好用的开源标注工具,竟然还有人不知道…
查看>>
OpenMMLab | 面向多样应用需求,书生·浦语2.5开源超轻量、高性能多种参数版本
查看>>
OpenMP 线程互斥锁
查看>>
OpenMV入门教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
OpenObserve云原生可观测平台本地Docker部署与远程访问实战教程
查看>>
openoffice使用总结001---版本匹配问题unknown document format for file: E:\apache-tomcat-8.5.23\webapps\ZcnsDms\
查看>>
OpenPPL PPQ量化(2):离线静态量化 源码剖析
查看>>
OpenPPL PPQ量化(3):量化计算图的加载和预处理 源码剖析
查看>>
OpenPPL PPQ量化(4):计算图的切分和调度 源码剖析
查看>>
OpenPPL PPQ量化(5):执行引擎 源码剖析
查看>>
openpyxl 模块的使用
查看>>