config多文件夹配置中心

一、搭建config配置中心

1.1 搭建springboot模板项目

参考项目:https://github.com/HLDBanana/config-server.git

  • 创建maven项目
    略…
  • 添加pom.xml依赖
<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>
  • 启动类注解
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigCenterApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterApplication.class, args);
    }
}
  • application.yml配置
1. eureka:  
2.  instance:  
3.    prefer-ip-address: true #使用IP地址向Eureka Server注册  
4.  client:  
5.    service-url:  
6.      #注册中心地址  
7.      defaultZone: http://xxxx:8761/eureka  
8.    register-with-eureka: true  
9.    fetch-registry: true  
10.  
11.spring:  
12.  application:  
13.    # 配置中心注册名称  
14.    name: config-server  
15.  cloud:  
16.    config:  
17.      discovery:  
18.        #配置中心服务名,如未配置默认{spring.application.name}  
19.        service-id: config-server  
20.        #开启配置服务发现  
21.        enabled: true  
22.      server:  
23.        git:  
24.          # 配置中心git仓库地址  
25.          uri: http://192.168.101.94:8081/Data-Middleground-Develop-Area/product-code/service/multiplecheck-config.git  
26.          # git用户名  
27.          username: xxxx  
28.          # git密码  
29.          password: xxxxx  
30.          #超时时间  
31.          timeout: 5  
32.          #仓库文件夹目录,如果是/**,就是所有目录所有文件  
33.          search-paths:  /**  
34.server:  
35.  # 端口  
36.  port: 8991  

二、 git服务器结合Config

使用git服务器,可以自己搭建gitlab服务器,或者使用github、开源中国git、阿里云git

2.1 github上创建配置中心项目

登录git创建配置中心仓库
在这里插入图片描述
创建dev节点。
在这里插入图片描述
说明:通过分支来区分生产、测试环境。
master:生产环境配置
dev:测试环境配置

二、项目集成git配置中心

2.1 客户端整合配置中心
  • pom.xml添加依赖
<!-- config配置中心-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
</dependency>
  • 在 bootstrap.yaml 引入配置中心参数
    注意:整合配置中心需要将application.yml重命名为bootstrap.yml
    这里提供数据多路校验整合示例:
1. eureka:  
2.  instance:  
3.    prefer-ip-address: true #使用IP地址向Eureka Server注册  
4.  client:  
5.    serviceUrl:  
6.      defaultZone: ${eureka.address}  
7.    fetch-registry: true    
8.spring:  
9.  application:  
10.    name: yss-datamiddle-multiplecheck  
11.  # 配置中心  
12.  cloud:  
13.    config:  
14.      #第一种集成配置中心的方式,通过config的service-id映射 
15.      discovery:  
16.        service-id: CONFIG-SERVER    # 配置中心注册名称 默认去{label}节点读取order-{profile}.yml文件  
17.        enabled: true  
18.      # 后缀 dev|prod  
19.      profile: dev  
20.      # dev|master分支,建议使用它区分生产、测试环境  
21.      #label: master  
22.      # 第二种集成配置中心的方式,这里写配置中心服务的地址信息  
23.      #uri: http://localhost:8991  

http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

  • git配置仓库添加配置文件
    首先打开git配置仓库,并创建客户端对应文件夹,文件夹命名和注册名称相同,示例如下:
    在这里插入图片描述

  • 创建dev、test、release、prod环境配置,如下:
    在这里插入图片描述

  • 多路dev开发环境配置示例如下:

1.# Tomcat  
2.server:  
3.  port: 30180  
4.  
5.mybatis:  
6.  configuration:  
7.    call-setters-on-nulls: true  
8.  
9.mybatis-plus:  
10.  mapperLocations: classpath:mapper/*.xml  
11.  type-aliases-package: com.yss.datamiddle.entity  
12.  global-config:  
13.    db-column-underline: true  
14.    refresh-mapper: true  
15.    db-config:  
16.      logic-delete-value: 1 # 逻辑已删除值  
17.      logic-not-delete-value: 0 # 逻辑未删除值  
18.    banner: false  
19.  configuration:  
20.    map-underscore-to-camel-case: true  
21.    cache-enabled: true #配置的缓存的全局开关  
22.    lazyLoadingEnabled: true #延时加载的开关  
23.    multipleResultSetsEnabled: true #开启的话,延时加载一个属性时会加载该对象全部属性,否则按需加载属性  
24.    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印sql语句,调试用  
25.    jdbc-type-for-null: null  
26.    call-setters-on-nulls: true  #当查询数据为空时字段返回为null,不加这个查询数据为空时,字段将被隐藏  
27.  
28.spring:  
29.  profiles:  
30.    include: etl  #引入配置中心外部配置文件
31.  datasource:  
32.    dynamic:  
33.      druid:  
34.        ##连接池配置  
35.        initial-size: 5 # 初始化连接大小  
36.        min-idle: 5     # 允许最小空闲连接数,空闲连接超时踢除过程会最少保留的连接数  
37.        max-active: 20  # 允许的最大同时使用中(在被业务线程持有,还没有归还给druid) 的连接数  
38.        max-wait: 60000 # 从连接池获取连接的最大等待时间,单位是毫秒  
39.        time-between-eviction-runs-millis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒  
40.        min-evictable-idle-time-millis: 300000 # 配置一个连接在池中最小生存的时间,单位是毫秒  
41.        validation-query: select 1 from dual # 用来检测连接是否有效的sql,如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用  
42.        test-while-idle: true # 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效  
43.        test-on-borrow: true # 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能  
44.        test-on-return: false # 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能  
45.        pool-prepared-statements: true # oracle配置为true,mysql可以配置为false,分库分表较多的数据库,建议配置为false  
46.        max-pool-prepared-statement-per-connection-size: 20 # 要启用PSCache,必须配置大于0,当大于0时,poolPreparedStatements自动触发修改为true。在Druid中,不会存在Oracle下PSCache占用内存过多的问题,可以把这个数值配置大一些,比如说100  
47.        filters: stat,wall,slf4j  
48.      datasource:  
49.        primary:  
50.          url: jdbc:oracle:thin:@192.168.100.133:1521:ORCL  
51.          username: multiplecheck  
52.          password: multiplecheck  
53.          driver-class-name: oracle.jdbc.driver.OracleDriver  
54.          type: com.alibaba.druid.pool.DruidDataSource  
55.        slave1:  
56.          url: jdbc:oracle:thin:@192.168.100.130:1521:ORCL  
57.          username: multiplecheck_dwd  
58.          password: multiplecheck_dwd  
59.          driver-class-name: oracle.jdbc.driver.OracleDriver  
60.          type: com.alibaba.druid.pool.DruidDataSource  
61.        slave2:  
62.          url: jdbc:oracle:thin:@192.168.100.130:1521:ORCL  
63.          username: multiplecheck  
64.          password: multiplecheck  
65.          driver-class-name: oracle.jdbc.driver.OracleDriver  
66.          type: com.alibaba.druid.pool.DruidDataSource  
67.      ## 默认数据源  
68.      primary: default  
69.  jpa:  
70.    primary:  
71.      show-sql: true  
72.      generate-ddl: true  
73.      hibernate:  
74.        ddl-auto: none  
75.      properties:  
76.        hibernate:  
77.          dialect: org.hibernate.dialect.OracleDialect  
78.      open-in-view: true  
79.    slave1:  
80.      show-sql: true  
81.      generate-ddl: true  
82.      hibernate:  
83.        ddl-auto: none  
84.      properties:  
85.        hibernate:  
86.          dialect: org.hibernate.dialect.OracleDialect  
87.      open-in-view: true  
88.    slave2:  
89.      show-sql: true  
90.      generate-ddl: true  
91.      hibernate:  
92.        ddl-auto: none  
93.      properties:  
94.        hibernate:  
95.          dialect: org.hibernate.dialect.OracleDialect  
96.      open-in-view: true  
97.  redis:  
98.    #open: false  # 是否开启redis缓存  true开启   false关闭  
99.    database: 3  
100.    host: 192.168.100.131  
101.    port: 6379  
102.    password: 123456    # 密码(默认为空)  
103.    timeout: 6000ms  # 连接超时时长(毫秒)  
104.    jedis:  
105.      pool:  
106.        max-active: 1000  # 连接池最大连接数(使用负值表示没有限制)  
107.        max-wait: -1ms    # 连接池最大阻塞等待时间(使用负值表示没有限制)  
108.        max-idle: 10      # 连接池中的最大空闲连接  
109.        min-idle: 5       # 连接池中的最小空闲连接  
110.  
111.ribbon:  
112.  ConnectTimeout: 500  
113.  ReadTimeout: 10000  
114.  MaxAutoRetries: 0  
115.  OkToRetryOnAllOperations: false  
116.  ServerListRefreshInterval: 2000  
117.  MaxAutoRetriesNextServer: 3  
118.  
119.feign:  
120.  hystrix:  
121.    enabled: true  
122.  httpclient:  
123.    enabled: false  
124.  okhttp:  
125.    enabled: true  
126.  
127.hystrix:  
128.  command:  
129.    default:  
130.      execution:  
131.        timeout:  
132.          enabled: true  
133.        isolation:  
134.          thread:  
135.            timeoutInMilliseconds: 10000   
  • 测试
    访问 配置中心ip:port/服务名/环境名,看是否能读取到配置
    例如:http://localhost:8991/yss-datamiddle-multiplecheck/dev
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值