故事
首页
指南
  • Java
  • Python
  • Linux
  • 前端
  • Docker
  • 实践
  • 折腾
  • 分类
  • 标签
  • 归档
壁纸 (opens new window)
GitHub (opens new window)
首页
指南
  • Java
  • Python
  • Linux
  • 前端
  • Docker
  • 实践
  • 折腾
  • 分类
  • 标签
  • 归档
壁纸 (opens new window)
GitHub (opens new window)
  • Java基础

    • String类的深入学习
    • HashMap源码学习
    • Java8新特性回顾
  • Java框架

    • POI事件模式解析并读取Excel文件数据
    • SpringMVC的执行流程源码分析
    • netty+websocket实现即时通讯功能
    • 分布式定时任务解决方案xxl-job
    • spring-session实现集群session共享
    • springcloud优雅下线服务
    • Java日志发展历史
    • Mybatis Generator配置
    • JavaSPI机制和Springboot自动装配原理
    • Spring Cloud Config接入
      • Spring Cloud Config
      • 配合git仓库使用
      • 服务端
        • 依赖
        • 配置文件
        • 启动类
      • 服务端
        • 依赖
        • 配置文件
        • Discovery First Bootstrap
    • 使用spring validation进行参数校验
    • dubbo接口超时配置
    • ConfigurationProperties注解
    • EasyExcel导出excel
  • 中间件

    • 分布式锁解决方案
    • 关于消息中间件MQ
    • kafka学习记录
    • kakfa实践
    • kakfa重复消费问题处理
  • 数据库

    • MySql索引
    • SQL优化学习
    • Otter实现数据全量增量同步
  • 开发工具

    • Maven的生命周期
    • nexus无法下载依赖的问题记录
  • 其他

    • linux服务器安装OpenOffice踩坑
    • POI踩坑-zip file is closed
    • OpenJDK没有jstack等命令的解决办法
    • 微信小程序加密数据对称解密工具类
    • mybatis的classpath配置导致的jar包读取问题
    • feign请求导致的用户ip获取问题记录
    • ribbon刷新服务列表间隔和canal的坑
    • cpu占用率高排查思路
  • 简介
  • java
  • Java框架
storyxc
2022-04-16

Spring Cloud Config接入

# Spring Cloud Config接入

微服务配置中心能帮助统一管理多个环境、多个应用程序的外部化配置,不需要在某个配置有改动或新增某个配置项时去多个节点上一个个修改,做到了一次改动,处处使用。这里和Eureka注册中心配合使用。

# Spring Cloud Config

SpringCloud子项目,提供了分布式系统中外部配置管理的能力,分为server和client两部分。官网地址 (opens new window)

# 配合git仓库使用

建立一个git仓库,将配置文件放在仓库里,配置文件格式应用名-profile.properties(yml),多服务的情况包名可根据服务前缀命名,在配置中心server端配置中增加search-path配置即可,比如多个包名app-1/app-2,可以配置search-path:app-*

# 服务端

# 依赖

<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
</dependencies>
1
2
3
4
5
6
7
8
9
10

# 配置文件

server:
  port: 8077
spring:
  application:
    name: spring-cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: git仓库地址
          username: 账号
          password: 密码
          search-paths: app-* #搜索路径,使用通配符

eureka:
  client:
    service-url:
      defaultZone: http://admin:admin@127.0.0.1:19991/eureka/
      fetch-registry-interval-seconds: 5

  instance:
    prefer-ip-address: true
    lease-expiration-duration-in-seconds: 30
    lease-renewal-interval-in-seconds: 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 启动类

@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigCenterApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterApplication.class, args);
    }
}
1
2
3
4
5
6
7
8

# 服务端

# 依赖

<!--springcloud config客户端-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-client</artifactId>
</dependency>
1
2
3
4
5

# 配置文件

spring:
  application:
    name: app-1
  cloud:
    config:
      profile: @spring.profiles.active@
      label: master
      discovery:
        enabled: true
        service-id: spring-cloud-config-center
eureka:
  client:
    service-url:
      defaultZone: http://admin:admin@127.0.0.1:19991/eureka/
1
2
3
4
5
6
7
8
9
10
11
12
13
14

提示

SpringCloudConfig配合服务发现使用时,必须在bootstrap.yaml(或环境变量)开启服务发现spring.cloud.config.discovery.enabled=true,并且要配置注册中心的地址eureka.client.serviceUrl.defaultZone,服务启动时会首先去注册中心找到配置中心server。

# 以下引自官网

# Discovery First Bootstrap (opens new window)

If you use a DiscoveryClient implementation, such as Spring Cloud Netflix and Eureka Service Discovery or Spring Cloud Consul, you can have the Config Server register with the Discovery Service. However, in the default “Config First” mode, clients cannot take advantage of the registration.

If you prefer to use DiscoveryClient to locate the Config Server, you can do so by setting spring.cloud.config.discovery.enabled=true (the default is false). The net result of doing so is that client applications all need a bootstrap.yml (or an environment variable) with the appropriate discovery configuration. For example, with Spring Cloud Netflix, you need to define the Eureka server address (for example, in eureka.client.serviceUrl.defaultZone). The price for using this option is an extra network round trip on startup, to locate the service registration. The benefit is that, as long as the Discovery Service is a fixed point, the Config Server can change its coordinates. The default service ID is configserver, but you can change that on the client by setting spring.cloud.config.discovery.serviceId (and on the server, in the usual way for a service, such as by setting spring.application.name).

编辑 (opens new window)
#springcloud
上次更新: 2022/04/21, 18:12:24
JavaSPI机制和Springboot自动装配原理
使用spring validation进行参数校验

← JavaSPI机制和Springboot自动装配原理 使用spring validation进行参数校验→

Theme by Vdoing | Copyright © 2019-2023 story | 豫ICP备19046036号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式