SpringCloud的配置实战

SpringCloud的配置实战

1.介绍:

其实前面client的配置案例都是帮助理解这个组件为主,并没有很大的实际意义。。。。。。这节的案例中是配置一个Provider,一个eureka,他们的配置统一在github上获取,实现统一配置分布式管理和多环境变更,这个才比较有实战意义。

2、实现过程:

1. 先写好provider和Eureka的配置yml文件,这两个文件和平常配置没什么不同,因为这里主要是说config,所以就没有配置集群,上传yml到github

  • Eureka配置文件springcloud-study-config-eureka-client.yml示例:
spring:
  profiles:
    active: dev
---    
server: 
  port: 7001 #注册中心占用7001端口,冒号后面必须要有空格
spring:
  profiles: dev #开发环境
  application:
    name: springcloud-study-config-eureka-client
      
eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false #false表示不向注册中心注册自己
    fetch-registry: false #false 表示自己就是注册中心,职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #设置与eureka server 交互的地址查询服务和注册服务都需要依赖的地址
---    
server: 
  port: 7001 #注册中心占用7001端口,冒号后面必须要有空格
spring:
  profiles: test #测试环境
  application:
    name: springcloud-study-config-eureka-client
      
eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false #false表示不向注册中心注册自己
    fetch-registry: false #false 表示自己就是注册中心,职责就是维护服务实例,并不需要去检索服务
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka #设置与eureka server 交互的地址查询服务和注册服务都需要依赖的地址
  • Provider配置文件springcloud-study-config-dept-client.yml示例:
spring:
  profiles:
    active: dev
---
server: 
  port: 8001 #部门微服务
spring:
  profiles: dev #开发环境
  application:
    name: springcloud-study-config-dept-client
    
  datasource:
    url: jdbc:mysql://localhost:3306/clouddb01?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root1234
    driver-class-name: com.mysql.jdbc.Driver #mysql驱动包
    type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型

    #druid连接池配置
    initialSize: 5  #初始化连接数
    minIdle: 5      #数据库连接池最小连接维持数
    maxActive: 20   #数据库最大活动连接数
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    # 配置监控统计拦截的filters,去掉监控界面sql无法统计,‘wall’用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    userGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

#Mybatis配置
mybatis:
  #type-aliases-package: com.blj.springcloud.entities        #所有Entities实体类别名所在包
  mapper-locations: classpath:mybatis/mapper/**/*.xml        #mapper映射文件

  configuration:
    map-underscore-to-camel-case: true #开启驼峰命名
    cache-enabled: true #开启二级缓存
  type-aliases-package: com.blj.springcloud.entities        #所有Entities实体类别名所在包

#客户端注册进eureka服务列表
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
     

  instance:
    instance-id:  cloud-dept8001 #自定义服务名称信息
    prefer-ip-address: true   #访问路径可以显示ip地址
info:
  app.name: study-springcloud-dept
  company.name: www.blj.com
  build.artifactId: ${project.artifactId}
  build.version: ${project.version}    
    
---
server: 
  port: 8001 #部门微服务
spring:
  profiles: test #测试环境
  application:
    name: springcloud-study-config-dept-client
    
  datasource:
    url: jdbc:mysql://localhost:3306/clouddb02?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root1234
    driver-class-name: com.mysql.jdbc.Driver #mysql驱动包
    type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型

    #druid连接池配置
    initialSize: 5  #初始化连接数
    minIdle: 5      #数据库连接池最小连接维持数
    maxActive: 20   #数据库最大活动连接数
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    # 配置监控统计拦截的filters,去掉监控界面sql无法统计,‘wall’用于防火墙
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    userGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

#Mybatis配置
mybatis:
  #type-aliases-package: com.blj.springcloud.entities        #所有Entities实体类别名所在包
  mapper-locations: classpath:mybatis/mapper/**/*.xml        #mapper映射文件

  configuration:
    map-underscore-to-camel-case: true #开启驼峰命名
    cache-enabled: true #开启二级缓存
  type-aliases-package: com.blj.springcloud.entities        #所有Entities实体类别名所在包

#客户端注册进eureka服务列表
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
     

  instance:
    instance-id:  cloud-dept8001 #自定义服务名称信息
    prefer-ip-address: true   #访问路径可以显示ip地址
info:
  app.name: study-springcloud-dept
  company.name: www.blj.com
  build.artifactId: ${project.artifactId}
  build.version: ${project.version}   
  1. 新开eureka和provide的模块并在pom.xml中添加依赖,其他必要依赖和之前的案例一样,但是config的依赖一定要添加上
 		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
  1. 两个模块都要编写bootstrap.yml文件,和上面的案例一样
spring:
  cloud:
    config:
      name: application_config #需要从github上读取的资源名称,注意没有yml后缀名
      profile: test   #本次访问的配置项
      label: master
      uri: http://config3344.com:3344  #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
  1. (可选)两个模块中编写application.yml文件,可以配置一下服务名
spring:
  application:
    name: application_config
  1. 两个模块的主启动类,Eureka的正常加EurekaServer注解,Provider加EurekaClient注解,不详述

  2. 编写Provider模块的业务代码

  3. 启动测试,因为这两个模块都要通过3344ConfigServer为其在github上获取配置,所以要先启动3344模块,然后再一次启动eureka和provider模块,进行测试即可。

7.测试:因为这两个模块都要通过3344ConfigServer为其在github上获取配置,所以要先启动3344模块,然后再一次启动eureka和provider模块,进行测试即可。
http://127.0.0.1:8001/dept/list
运行结果:
在这里插入图片描述
在这里插入图片描述
现在将当前环境切换成dev,重启启动并再次测试,看是不是连接的是clouddb01数据库。
在这里插入图片描述
运行结果:在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值