Spring Cloud使用数据库作为Config Server

Spring Cloud版本:Greenwich.RELEASE
Spring Boot版本:2.1.2.RELEASE

bootstrap.yml

注意spring.profiles.active必须设置为jdbc

spring.cloud.config.server.jdbc.sql可以根据自己需求自定义

spring:
  application:
    name: config-server
  profiles:
    active: jdbc
  cloud:
    consul:
      host: ${consul.host}
      port: ${consul.port:8500}
    config:
      server:
        default-label: master
        jdbc:
          sql: select name,value from app_config where application=? and profile=? and label=?
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://${db.host}:${db.port:3306}/${db.name}?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&autoReconnect=true
    username: ${db.username}
    password: ${db.password}
    maxActive: 5
    initialSize: 1
    idleTimeout: 60000
    connectionTimeout: 60000
    validationTimeout: 3000
    maxLifetime: 60000
    cachePrepStmts: true
    prepStmtCacheSize: 500
    prepStmtCacheSqlLimit: 2048
    useServerPrepStmts: true
    useLocalSessionState: true
    useLocalTransactionState: true
    rewriteBatchedStatements: true
    cacheResultSetMetadata: true
    cacheServerConfiguration: true
    elideSetAutoCommits: true
    maintainTimeStats: false
  jackson:
    date-format: "yyyy-MM-dd HH:mm:ss"
    property-naming-strategy: SNAKE_CASE
    time-zone: "GMT+8"
    

management:
  endpoint:
    health:
      show-details: ALWAYS
          
logging:
  level:
    root: INFO
  file: ${spring.application.name}.log
 
server:
  compression:
    enabled: true
    mime-types: application/json,application/xml,text/html,text/javascript,text/css,text/xml,text/plain
    min-response-size: 2048
  tomcat:
    accept-count: 400
    uri-encoding: utf-8
    min-spare-threads: 25
    max-threads: 400
  port: 8101

pom.xml



    4.0.0
    
    
    1.0.0-SNAPSHOT

    config server

    
        UTF-8
        11
        5.4.1.Final
    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.2.RELEASE
    

    
        
            aliyun
            http://maven.aliyun.com/nexus/content/groups/public/
        
    
    
        
            aliyun
            http://maven.aliyun.com/nexus/content/groups/public
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Greenwich.RELEASE
                pom
                import
            
        
    

    
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-consul-discovery
        
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.springframework.boot
            spring-boot-devtools
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            mysql
            mysql-connector-java
        
    
    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

Application

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

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

你可能感兴趣的:(Spring Cloud使用数据库作为Config Server)