前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springcloud config server 与config client 集群配置

springcloud config server 与config client 集群配置

作者头像
用针戳左手中指指头
发布2021-01-29 11:03:17
6340
发布2021-01-29 11:03:17
举报
文章被收录于专栏:学习计划

cloud 版本:

Finchley.SR2

boot 版本:

2.0.9

这里说明下,官方给出的对应版本Finchley.SR2 - 》2.0.X

使用SpringBoot 2.0.4 和2.0.6会出现一个问题,就是post请求:/actuator/bus-refresh报错,报错信息可以看:https://blog.csdn.net/qq_28911061/article/details/90733928

config server端配置:

application.yml

代码语言:javascript
复制
server:
#  address: 192.168.168.9
  port: 17999
  address: 192.168.168.14

spring:
  application:
    name: config-server
    # 激活本地搜索 - 必须配置
  profiles:
    active: native
    #具体搜索路径
  cloud:
    config:
        server:
          native:
            search-locations: file:///D:/config-file

  #rabbitmq 配置 - 必须配置
  rabbitmq:
    host: 192.168.168.9
    port: 5672
    username: ***
    password: ***


# Eureka Server配置
gaia:
  eureka:
    server:
      ip: 192.168.168.9
      post: 18000
    server2:
      ip: 192.168.168.14
      post: 18000
    security:
      user:
        name: ***
        password: ***
eureka:
  instance:
    #IP地址
    ip-address: ${server.address}
    #显示的服务名
    instance-id: http://${server.address}:${server.port}
    #是否映射ip
    prefer-ip-address: true
  client:
    service-url:
      #集群
      defaultZone: http://${gaia.eureka.security.user.name}:${gaia.eureka.security.user.password}@${gaia.eureka.server.ip}:${gaia.eureka.server.post}/eureka/,http://${gaia.eureka.security.user.name}:${gaia.eureka.security.user.password}@${gaia.eureka.server2.ip}:${gaia.eureka.server2.post}/eureka/

    healthcheck:
      enabled: true
#必须配置
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

说明:这里配置的使用native方式,下面是git方式,只需要改一部分就可以了。

代码语言:javascript
复制
spring:
  application:
    name: config-server3
  cloud:
    config:
      server:
        git:
          #git仓库下的项目地址
          uri: https://gitee.com/LIRUIYI/springcloud-config
#          #占位符匹配git仓库,应用名称命名的仓库名
#          uri: https://gitee.com/LIRUIYI/{application}
          #配置仓库路径下的相对位置,可以配置多个
#          search-paths: cloud
          #占位符匹配以应用名称命名的文件夹名
          search-paths: '{application}'
          #连接了私有仓库需填写用户名和密码
          username: ***
          password: **
          #连接超时时间
          timeout: 4

启动类上加注解:

代码语言:javascript
复制
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class);
    }
}

到这,config server配置完毕;

然后是config client - 》 bootstrap.yml

代码语言:javascript
复制
server:
  #这里配置端口会被config server上下载下来的覆盖。
  port: 18020

#config server的地址
config:
  server:
    port: 17999
#    ip: 192.168.168.9
    serverId: CONFIG-SERVER
#    ip: 127.0.0.1

spring:
  application:
    name: config-client

  #config server 配置
  cloud:
    config:
      #单机配置
#      uri: http://${config.server.ip}:${config.server.port}
       #环境版本
      profile: test
      #配置多个文件加载;commons - 》 公共配置
      name: config-client,commons
      #配置config server 集群 ;需要该客户端先配置eureka client
      discovery:
        service-id: ${config.server.serverId}
        enabled: true

  #下面3个必须写在这里的配置。
  #mq 配置 - 必须
  rabbitmq:
    host: 192.168.168.9
    port: 5672
    username: ***
    password: ***
    #必须
  main:
    allow-bean-definition-overriding: true

#暴露刷新端口 - 必须
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh


# Eureka Server配置 - 访问集群的config server需要在这里配置eureka
gaia:
  eureka:
    server:
      ip: 192.168.168.9
      post: 18000
    server2:
      ip: 192.168.168.14
      post: 18000
    security:
      user:
        name: ***
        password: ***

# Eureka Client配置
eureka:
  instance:
    prefer-ip-address: true
    status-page-url-path: /actuator/info
    health-check-url-path: /actuator/health

    # Eureka客户端向服务端发送心跳的时间间隔,单位为秒(客户端告诉服务端自己会按照该规则)
    lease-renewal-interval-in-seconds: 10
    # Eureka服务端在收到最后一次心跳之后等待的时间上限,单位为秒,超过则剔除(客户端告诉服务端按照此规则等待自己)
    lease-expiration-duration-in-seconds: 60

  client:
    serviceUrl:
      defaultZone: http://${gaia.eureka.security.user.name}:${gaia.eureka.security.user.password}@${gaia.eureka.server.ip}:${gaia.eureka.server.post}/eureka/,http://${gaia.eureka.security.user.name}:${gaia.eureka.security.user.password}@${gaia.eureka.server2.ip}:${gaia.eureka.server2.post}/eureka/
    healthcheck:
      enabled: true

说明:

1、在这配置mq,是因为我们使用动态刷新,加入了bus,在我们访问/actuator/bus-refresh时,需要将该端点暴露,

2、allow-bean-definition-overriding 这个配置 你不配置,他都会提示你配置

3、managerment.endpoints 这个配置,暴露端点,实现动态刷新。

4、eureka 注册配置,这里我配置了集群方式的,那么就必须要eureka支持,因为这里我们写的是服务id,该文件是bootstrap,在应用程序启动加载作为程序引导使用的,所以是先加载这个文件向config server发送获取配置文件的请求,所以,本地是没有配置文件的,eureka的配置就必须配置在这个文件;

5、spring.cloud.config.name 这里是加载多个文件的,有很多个配置文件,但是基本一样的时候,我们就可以把相同配置提取出来比较方便,比如我配置了:比如client配置了spring.application.name = config-client,spring.cloud.config.profiles=dev,spring.cloud.config.name=config-client,commons

在文件系统中存放的文件有:config-client-dev.yml 、commons-dev.yml

就会加载到两个。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/06/03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档