七、SpringCloud-Config

本文介绍了如何搭建Spring Cloud Config Server,从Git仓库中获取配置,并在Eureka服务发现模块和提供者Dept模块中使用配置。配置文件根据不同环境加载,实现配置的动态管理和版本控制。
摘要由CSDN通过智能技术生成

1. 新建springcloud-config-server-3344模块

2. 导入依赖并在启动类添加注解

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
<!--config-server-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
        <version>2.1.1.RELEASE</version>
    </dependency>
</dependencies>
@EnableConfigServer

3. application.yml连接到git

server:
  port: 3344
spring:
  application:
    name: springcloud-config-server
    # 连接远程仓库
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/yuantu123/springcloud-config.git # https ,不是 git

# 通过 config-server可以连接到git,访问其中的资源以及配置~

4. 改造springcloud-eureka-7001

  1. 添加依赖

    		<!--config-starter-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
    
  2. bootstrap.yml获取git配置文件

    # 系统级别的配置
    spring:
      cloud:
        config:
          name: config-eureka   # 需要从git上读取的资源名称,不要后缀
          profile: dev
          label: master
          uri: http://localhost:3344   #调用springcloud-config-server-3344获取git
    
  3. git上的文件config-eureka.yml

    spring:
      profiles:
        active: dev
    ---    
    spring:
      profiles: dev
      application:
        name: springcloud-config-eureka-7001
    
    server:
      port: 7001
    
    #Eureka配置
    eureka:
      instance:
        hostname: eureka7001.com #Eureka服务端的实例名称
      client:
        register-with-eureka: false # 表示是否向eureka注册中心注册自己
        fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
        service-url:
          #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
          
          
    ---
    spring:
      profiles: test
      application:
        name: springcloud-config-eureka-7001
    
    server:
      port: 7001
    
    #Eureka配置
    eureka:
      instance:
        hostname: eureka7001.com #Eureka服务端的实例名称
      client:
        register-with-eureka: false # 表示是否向eureka注册中心注册自己
        fetch-registry: false #fetch-registry如果为false,则表示自己为注册中心
        service-url:
          #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
          defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
    

5. 改造 springcloud-provider-dept-8001

  1. 添加依赖

    		<!--config-starter-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
                <version>2.1.1.RELEASE</version>
            </dependency>
    
  2. bootstrap.yml获取git配置文件

    # 系统级别的配置
    spring:
      cloud:
        config:
          name: config-dept   # 需要从git上读取的资源名称,不要后缀
          profile: dev
          label: master
          uri: http://localhost:3344
    
  3. git上的文件config-dept.yml

    spring:
      profiles:
        active: dev
    ---    
    server:
      port: 8001
    
    #mybatis配置
    mybatis:
      type-aliases-package: com.jx.springcloud.pojo
      config-location: classpath:mybatis/mybatis-config.xml
      mapper-locations: classpath:mybatis/mapper/*.xml
    
    
    #spring的配置
    spring:
      profiles: dev
      application:
        name: springcloud-config-dept
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8
        username: root
        password: 123456
    
    #Eureka的配置,服务注册到哪里
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: springcloud-provider-dept8001 # 修改eureka上的默认描述信息!
        prefer-ip-address: true  # true,可以显示服务的IP地址 ~
    
    #info配置
    info:
      app.name: jx-springcloud
      company.name: jx.com
    ---
    server:
      port: 8001
    
    #mybatis配置
    mybatis:
      type-aliases-package: com.jx.springcloud.pojo
      config-location: classpath:mybatis/mybatis-config.xml
      mapper-locations: classpath:mybatis/mapper/*.xml
    
    
    #spring的配置
    spring:
      profiles: test
      application:
        name: springcloud-config-dept
      datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db02?useUnicode=true&characterEncoding=utf-8
        username: root
        password: 123456
    
    #Eureka的配置,服务注册到哪里
    eureka:
      client:
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
      instance:
        instance-id: springcloud-provider-dept8001 # 修改eureka上的默认描述信息!
        prefer-ip-address: true  # true,可以显示服务的IP地址 ~
    
    #info配置
    info:
      app.name: jx-springcloud
      company.name: jx.com
    

6. 删除eureka和8001的application.yml,启动测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值