SpringCloud教程 | 第11篇:分布式配置中心(Spring Cloud Config) 客户端实战

一、准备工作,

  1.github  springcloud-config 中添加


2.deptConfig-dev.yml 内容

server:
  port: 8001

mybatis:
  type-aliases-package: com.linjia.springcloud.entity          # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/*.xml                       # mapper映射文件

spring:
  profiles: dev  #开发环境
  application:
    name: microservicecloud-config-dept-client
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://192.168.0.38:3306/test              # 数据库名称
    username: root
    password: linjia
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间
  jpa:
      hibernate:
        ddl-auto: update
      show-sql: true
      properties:
            hibernate.format_sql: true
      database-platform: org.hibernate.dialect.MySQL5InnoDBDialect

eureka:
  client:
    service-url:
        defaultZone: http://localhost:7001/eureka   #将客户端注册进eureka服务列表内
       # defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
  instance:
    instance-id: ${spring.cloud.client.ipAddress}:${server.port}   #自定义服务名称信息  启动多个服务做集群需要设置为不同的ID值
    prefer-ip-address: true   #访问路径可以显示IP地址

     


 

3.deptConfig-test.yml 内容

server:
  port: 8002

mybatis:
  type-aliases-package: com.linjia.springcloud.entity          # 所有Entity别名类所在包
  mapper-locations:
  - classpath:mybatis/mapper/*.xml                       # mapper映射文件

spring:
  profiles: test  #开发环境
  application:
    name: microservicecloud-config-dept-client
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource            # 当前数据源操作类型
    driver-class-name: org.gjt.mm.mysql.Driver              # mysql驱动包
    url: jdbc:mysql://192.168.0.38:3306/test_1              # 数据库名称
    username: root
    password: linjia
    dbcp2:
      min-idle: 5                                           # 数据库连接池的最小维持连接数
      initial-size: 5                                       # 初始化连接数
      max-total: 5                                          # 最大连接数
      max-wait-millis: 200                                  # 等待连接获取的最大超时时间
  jpa:
      hibernate:
        ddl-auto: update
      show-sql: true
      properties:
            hibernate.format_sql: true
      database-platform: org.hibernate.dialect.MySQL5InnoDBDialect

eureka:
  client:
    service-url:
         defaultZone: http://localhost:7001/eureka   #将客户端注册进eureka服务列表内
        #defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka
  instance:
    instance-id: ${spring.cloud.client.ipAddress}-hystrix:${server.port}   #自定义服务名称信息  启动多个服务做集群需要设置为不同的ID值
    prefer-ip-address: true   #访问路径可以显示IP地址
    leaseRenewalIntervalInSeconds: 10          #剔除不健康节点。
    leaseExpirationDurationInSeconds: 30       #剔除不健康节点。
  
 
 
 

4.bootstrap.yml配置

spring:
  cloud:
    config:
      name: deptConfig #需要从github上读取的资源名称,注意没有yml后缀名
      #profile配置是什么就取什么配置dev or test
      #profile: dev
      profile: test
      label: master
      uri: http://localhost:3344  #SpringCloudConfig获取的服务地址

启动注册中心eureka,config,修改profile获取不同环境的配置参数


猜你喜欢

转载自blog.csdn.net/yangliuhbhd/article/details/80511680