My Avatar

licaibo

在JDK改来改去!

SpringCloud构建微服务(二) 配置中心

2017年09月25日 星期一, 发表于 海南 东方

如果你对本文有任何的建议或者疑问, 可以在 这里给我提 Issues, 谢谢! :)

SpringCloud提供了config的分布式配置中心,允许将我们的配置信息统一进行管理,每个服务启动时,根据配置文件名字以及环境动态的去加载配置。这里我先搭建一个配置中心,为后续服务提供者或是服务调用者提供动态配置。

新建配置文件仓库

SpringCloud Config允许我们存储在git或是svn,这里我选择的是git仓库进行存储。在我的GitHub新建config-repo仓库,用于存放service-dev.properties配置文件,里面我写的是Mysql的配置。注意:配置文件遵循SpringBoot提供的规范,这里xxx-dev指的是开发环境的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/your_dadabase_name?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=3000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validation-query=select 1 from dual
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.testOnBorrow=true
spring.datasource.testOnReturn=true

mybatis.mapper-locations=classpath:mapper/**/*.xml
mybatis.config-location=classpath:config/mybatis-config.xml
1
2
3
4
5
6
7
8
9
@Configuration
@EnableAutoConfiguration
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class,args);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
server:
  port: 5555

spring:
  application:
    name: config-server

management:
  context-path: /admin

logging:
  level:
    com.netflix.discovery: 'OFF'
    org.springframework.cloud: 'INFO'

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    statusPageUrlPath: /admin/info
    healthCheckUrlPath: /admin/health
  client:
      serviceUrl:
        defaultZone: http://localhost:8765/eureka/

spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/licaibo/config-repo.git #配置文件git仓库地址
          username: xxx
          password: xxx

cmd-markdown-logo

cmd-markdown-logo