第四节 springcloud config多实例集群

Config Server 可以从本地仓库读取配置文件,也可以从远处Git 仓库读取。本地仓库是指将所有的配置文件统一写在Config Server 工程目录下。
Config Sever 暴露HttpAPI 接口, ConfigClient 通过调用Config Sever 的H即API 接口来读取配置文件。


一、 构建Config Server
        1. 在Config-server工程pom.xml文件增加如下引用:(父POM文件自己注意查看)
                    <dependency>
                     <groupId>org.springframework.cloud</groupId>
                     <artifactId>spring-cloud-config-server</artifactId>
                  </dependency>
        2. 在ConfigServerApplication类上新增@EnableConfigServer注解,开启config server功能。
        3. 配置application.yml文件
            spring:
              cloud:
                config:
                  server:
                    native:
                      search-locations: classpath:/shared
              profiles:
                 active: native
            application:
                name: config-server
            server:
              port: 8769

            #配置说明:
            #1. spring.profiles.active=native 用来配置Config Server从本地读取配置文件
            #2. spring.cloud.config.server.native.search-locations指定配置文件路径

        4. 在resources/shared目录下新建config-client-dev.yml配置文件,配置数据如下:
            server:
              port: 8762

            foo: foo version 1

二、 构建Config client
        1.在config-client工程的pom.xml文件中新增如下引用: (父POM文件自己注意查看)
                    <dependency>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-web</artifactId>
                  </dependency>

                  <dependency>
                     <groupId>org.springframework.cloud</groupId>
                     <artifactId>spring-cloud-starter-config</artifactId>
                  </dependency>

        2.在config-client的配置文件bootstrap.yml中做配置。【 注意: bootstrap.yml相对于appliation具有优先的执行顺序。】
            spring:
              application:
                name: config-client
              cloud:
                config:
                  uri: http://localhost:8769
                  fail-fast: true
              profiles:
                active: dev

             #配置说明:
             # spring.cloud.config.url 指定configServer的访问url
             # spring.cloud.config.fail-fast 表示如果没有读取成功,则执行快速失败
             # sprisng.profiles.active表示读取dev环境的配置文件
             # 所有的配置加起来,config-client就会去读config-server/resource/shared目录下面的 config-client-dev.yml文件

        3.在ConfigClientApplication类中写一个API接口,读取配置文件foo变量,并通过API返回。
        4.启动config-server工程,启动config-client工程。启动config-client工程会在控制台的日志中发现config-client向url为
          http://localhost:8769的Config Server读取了配置文件,最终config-client程序启动的端口为8762,8762端口是在
          config-server/resource/shared目录中的config-client-dev.yml文件中配置的。由此可以见config-client向config-server中
          成功读取配置文件。

          日志内容如: Fetching config from server at : http://localhost:8769

        5. 访问http://localhost:8762/foo,浏览器显示:
            foo version 1

一、Eureka_server

eureka:
  instance:
    hostname: peer1     # eureka服务端的实力名称
  client:
    fetch-registry: false  # false表示自己就是注册中心。我的职责就是维护服务实例,并不需要去检索服务
    register-with-eureka: false   # false表示自己不需要向注册中心注册自己
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
  port: 8761     # 设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机版)
      #defaultZone: http://127.0.0.1:7001/eureka/

二、 github版本 去github上读取配置文件

spring:
  cloud:
     config:
       server:
         git:
           uri: https://github.com/bluewelkin/springcloudcon
           searchPaths: config
           username:
           password:
       label: master

  application:
    name: config-server
server:
  port: 8769
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka


开启两个实例 8768 、8769

三、eureka_client

spring:
  application:
    name: config-client
  cloud:
    config:
      #uri: http://localhost:8769
      fail-fast: true
      discovery:
              enabled: true
              serviceId: config-server
      label: master
  profiles:
    active: dev

读取config-server 里面的配置文件

不断运行config-client

发现控制台轮流 8768 8769

代码示例:https://gitee.com/dgx555/springcloud/tree/master/springcloud_config_duoshili

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值