主要内容

  1. 在springcloud config中,使用数据库存储配置信息。

    系统默认采用git的方式,此处我们介绍使用jdbc的方式存储配置信息

准备数据库

  1. 数据库我们使用mysql。

  2. 新建库 p-config-server

  3. 创建配置需要的表,并初始化一些配置信息

    CREATE TABLE `properties` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    `application` varchar(64) COLLATE utf8_bin NOT NULL DEFAULT '',
    `profile` varchar(64) COLLATE utf8_bin NOT NULL DEFAULT '',
    `label` varchar(64) COLLATE utf8_bin NOT NULL DEFAULT '',
    `key` varchar(128) COLLATE utf8_bin NOT NULL DEFAULT '',
    `value` varchar(4096) COLLATE utf8_bin NOT NULL DEFAULT '',
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_bin; INSERT INTO `properties` VALUES ('1', 'p-config-client', 'dev', 'master', 'key-1', 'value-1');
    INSERT INTO `properties` VALUES ('2', 'p-config-client', 'dev', 'master', 'key-2', 'value-2');
    INSERT INTO `properties` VALUES ('3', 'p-config-client', 'dev1', 'master', 'key-1', 'value-3');
    INSERT INTO `properties` VALUES ('4', 'p-config-client', 'dev1', 'master', 'key-4', 'value-4');
    INSERT INTO `properties` VALUES ('5', 'p-config-client', 'dev1', 'master', 'key-5', 'value-5');

创建eureka注册中心

  1. 创建springcloud项目p-eureka

    groupId: com.ms
    artifactId: p-eureka

  1. 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>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ms</groupId>
    <artifactId>p-eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>p-eureka</name>
    <description>Demo project for Spring Boot</description> <properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties> <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-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>${spring-cloud.version}</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>
  2. application.properties改为application.yml

    server:
    port: 7001
    spring:
    application:
    name: p-eureka
    eureka:
    client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
    defaultZone: http://localhost:7001/eureka/
  3. PEurekaApplication内容

    package com.ms;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication
    @EnableEurekaServer
    public class PEurekaApplication { public static void main(String[] args) {
    SpringApplication.run(PEurekaApplication.class, args);
    } }

    注意:

    • @EnableEurekaServer:表示这是一个eureka注册中心
  4. 运行PEurekaApplication

  5. 访问http://localhost:7001/

创建配置中心(config server)

  1. 创建springcloud项目p-config-server

    groupId: com.ms
    artifactId: p-config-server

  2. 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>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ms</groupId>
    <artifactId>p-config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>p-config-server</name>
    <description>Demo project for Spring Boot</description> <properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.SR1</spring-cloud.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-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
    </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>${spring-cloud.version}</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>
  3. application.yml 内容

    server:
    port: 8080
    eureka:
    client:
    service-url:
    defaultZone: http://localhost:7001/eureka/
    spring:
    application:
    name: p-config-server
    cloud:
    config:
    enabled: true
    profile: jdbc
    server:
    jdbc:
    sql: SELECT `key`, `value` from PROPERTIES where application=? and profile=? and label=?
    datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/config-server?characterEncoding=UTF-8
    username: root
    password: root123
    type: com.zaxxer.hikari.HikariDataSource
    profiles:
    active: jdbc

    注意

    • 数据连接地址、用户名、密码,这些值根据自己的机器进行修改
  4. PConfigServerApplication内容

    package com.ms;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
    @EnableConfigServer
    @EnableEurekaClient
    public class PConfigServerApplication { public static void main(String[] args) {
    SpringApplication.run(PConfigServerApplication.class, args);
    } }

    注意:

    • @EnableConfigServer:表示这是一个注册中心应用
    • @EnableEurekaClient:将当前服务注册到eureka中,可以被其他应用发现,然后使用
  5. 运行PConfigServerApplication

  6. 访问:http://localhost:8080/master/p-config-client-dev,dev1.yml 结果如下:

    key-1: value-3
    key-2: value-2
    key-4: value-4
    key-5: value-5

创建客户端

  1. 创建springcloud项目p-config-client

    groupId: com.ms
    artifactId: p-config-client

  2. 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>
    <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ms</groupId>
    <artifactId>p-config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>p-config-client</name>
    <description>Demo project for Spring Boot</description> <properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.SR1</spring-cloud.version>
    </properties> <dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
    </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>${spring-cloud.version}</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>
  3. application.properties改为bootstrap.yml 内容

    注意:此处必须使用bootstrap名称

    server:
    port: 8081
    spring:
    application:
    name: p-config-client
    cloud:
    config:
    discovery:
    enabled: true
    service-id: p-config-server
    name: ${spring.application.name}
    profile: dev,dev1
    label: master
    override-system-properties: false
    eureka:
    client:
    service-url:
    defaultZone: http://localhost:7001/eureka/
    • config.name对应数据库properties表中的application字段

    • config.profile对应表中的profile字段,config.profile如果包含多个值,之间用英文逗号隔开

    • config.label对应表中的label字段,我们可以使用label来区分环境(dev【开发环境】、test【测试环境】、prod【线上环境】)

  4. PConfigClientApplication内容

    package com.ms;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
    @EnableEurekaClient
    public class PConfigClientApplication { public static void main(String[] args) {
    SpringApplication.run(PConfigClientApplication.class, args);
    } }

    注意:

    • @EnableEurekaClient:当前应用会访问eureka注册中心,发现config service
  5. 创建DemoController

    package com.ms;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.env.Environment;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController; import java.util.HashMap;
    import java.util.Map; @RestController
    @Slf4j
    public class DemoController { @Autowired
    private Environment environment; @RequestMapping("/{key}")
    public String index(@PathVariable("key") String key) {
    return this.environment.getProperty(key);
    }
    }
  6. 运行PConfigClientApplication

  7. 访问 http://localhost:8081/key-1 结果如下:

    value-3

总结

  1. 使用数据库存储配置信息还是比较靠谱的,相对于git来说会更好一些
  2. 代码已上传至git,获取源码方式:微信公众号javacode2018,发送:sccj

可以关注公众号:路人甲Java,获取年薪50万课程,获取最新文章。

Spring Cloud config中,使用数据库存储配置信息的更多相关文章

  1. springcloud(六):Spring Cloud 配置中心采用数据库存储配置内容

    Spring Cloud 配置中心采用数据库存储配置内容 转自:Spring Cloud Config采用数据库存储配置内容[Edgware+] Spring Cloud Server配置中心采用了G ...

  2. Spring Cloud Config中文文档

    https://springcloud.cc/spring-cloud-config.html 目录 快速开始 客户端使用 Spring Cloud Config服务器 环境库 健康指标 安全 加密和 ...

  3. 关于spring cloud “Finchley.RC2”版本在spring cloud config中的ArrayIndexOutOfBoundsException

    原文 https://www.cnblogs.com/Little-tree/p/9166382.html 在学spring cloud config的时候遇到一个ArrayIndexOutOfBou ...

  4. Spring Cloud(十)高可用的分布式配置中心 Spring Cloud Config 中使用 Refresh

    上一篇文章讲了SpringCloudConfig 集成Git仓库,配和 Eureka 注册中心一起使用,但是我们会发现,修改了Git仓库的配置后,需要重启服务,才可以得到最新的配置,这一篇我们尝试使用 ...

  5. Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!

    本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...

  6. Spring Cloud config之一:分布式配置中心入门介绍

    Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务器为各应用的所有环境提供了一个中心化的外部配置.它实现了对服务端和客户端对Spring Environm ...

  7. Spring Boot + Spring Cloud 构建微服务系统(九):配置中心(Spring Cloud Config)

    技术背景 如今微服务架构盛行,在分布式系统中,项目日益庞大,子项目日益增多,每个项目都散落着各种配置文件,且随着服务的增加而不断增多.此时,往往某一个基础服务信息变更,都会导致一系列服务的更新和重启, ...

  8. Spring Cloud学习笔记【九】配置中心Spring Cloud Config

    Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端 ...

  9. 跟我学SpringCloud | 第六篇:Spring Cloud Config Github配置中心

    SpringCloud系列教程 | 第六篇:Spring Cloud Config Github配置中心 Springboot: 2.1.6.RELEASE SpringCloud: Greenwic ...

随机推荐

  1. 创建Ajax

    Ajax的全称是Asynchronous javascript and XML = 异步传输 + JS + XML     不需要刷新页面就可以获取新的数据 创建步骤:    (1)创建XML对象也就 ...

  2. Sharepoint 对于是否签出文件进行编辑区别

    在库设置----版本控制设置 一.需要签出才能编辑 例如需要对以上通用盒进行修改时,若在“使用资源管理器中打开”粘贴文件时会提示必须先签出项目 签出文件后,再粘贴文件到文档库中,可以选择签入的版本类型 ...

  3. Springboot ResponseEntity IE无法正常下载文件

    项目在google浏览器下都很nice了,但当测试到IE的时候开始出现各种问题. 项目是前端js通过URL传参fileName到后台解析返回ResponseEntity 前端代码如下: window. ...

  4. 14、SRA数据上传

    1.ncbi登陆,进入SRA,进入new submission 2. 1)SUBMITTER 2)PROJECT TYPE Raw sequence reads 和 ranscriptome or G ...

  5. Windows使用Github

    首先,你需要执行下面两条命令,作为 git 的基础配置,作用是告诉 git 你是谁,你输入的信息将出现在你创建的提交中. git config --global user.name "你的名 ...

  6. C#类和类的实例

    类 ,顾名思义就是分类.类别的意思.我们要面向对象编程,就需要对不同的事物进行分类.类可以说是.net面向对象的核心. 类:就是具有相同的属性和功能的对象的抽象的集合. 1.类的定义  <访问修 ...

  7. php总结_1

    ?php //检测变量类型,自己用的最多的是is_array()了,其他的几乎没用过 $array = array(1,9.9,'abc',array(1,2,3),true,null,); v(is ...

  8. Protocol Buffers官方文档(proto3语言指南)

    本文是对官方文档的翻译,大部分内容都是引用其他一些作者的优质翻译使文章内容更加通俗易懂(自己是直译,读起来有点绕口难理解,本人英文水平有限),参考的文章链接在文章末尾 这篇指南描述如何使用protoc ...

  9. Codeforces Round #520 (Div. 2)B(贪心,数学)

    #include<bits/stdc++.h>using namespace std;int mi[100007];int main(){ int cnt=0; int flag=0; i ...

  10. 发邮件的python脚本

    1. 编写一个最简单的发邮件的python脚本   #coding: utf-8 import smtplib from email.mime.text import MIMEText from em ...