SpringCloud笔记(Dalston)——分布式配置中心

配置中心:SpringCloud Config

  • 应用服务除了实现系统功能,还需要连接资源和其他应用,经常有很多需要在外部配置的数据用于调整应用的行为,如切换不同的数据库,设置功能开关等。
  • 随着微服务数量的不断增加,需要系统具备可伸缩和可扩展性,除此之外就是能够管理相当多的服务实例的配置数据。
  • 在应用的开发阶段,配置信息由各个服务自治管理 ,但是到了生产环境之后会给运维带来很大的麻烦,特别是微服务的规模比较大,配置的更新更为麻烦 为此,系统需要建立一个统一的配置管理中心。

常见的配置中心的实现方法有:

  1. 硬编码,缺点是需要修改代码,风险大。
  2. 放在xml等配置文件中,和应用一起打包,缺点是更新需要重新打包和重启。
  3. 文件系统,缺点是依赖操作系统等。
  4. 读取系统的环境变量,缺点是有大量的配置需要人工设置到环境变量中,不便于管理,且依赖平台。
  5. 云端存储,缺点是与其他应用相耦合。

简介

SpringCloud中提供了分布式配置中Spring Cloud Config,为外部配置提供了客户端和服务器端的支持。基于Config服务器,就可以集中管理各种环境下的各种应用的配置信息。其中,将包括两个部分:配置服务器和配置客户端。Config Server即是配置服务器,为客户端提供其对应的配置信息,配置信息的来源为配置仓库,启动时需要拉取配置仓库的信息,储存到本地仓库中; Config Client客户端,只会在本 配置必要的信息,如指定获取配置的Config Server地址,启动时从配置服务器获取配置信息,并支持动态刷新配置仓库中的属性值。

创建Config Server

准备工作

创建一个spring-boot项目,取名为config-server,添加maven依赖。

添加依赖

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

开启配置服务器

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

配置yml

server:
  port: 8500
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/fengfangithub/study-springcloud.git
          search-paths: springcloud-config
          default-label: master
          username:
          password: 
  • spring.cloud.config.server.git.uri:配置git仓库地址
  • spring.cloud.config.server.git.searchPaths:配置仓库路径
  • spring.cloud.config.label:配置仓库的分支
  • spring.cloud.config.server.git.username:访问git仓库的用户名
  • spring.cloud.config.server.git.password:访问git仓库的用户密码

新增application-dev.properties文件

config.name=myconfig
config.age=18

启动并测试

Spring Cloud Config有它的一套访问规则,我们通过这套规则在浏览器上直接访问就可以。

/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
  • {profile} 就是配置文件的版本,我们的项目有开发版本、测试环境版本、生产环境版本,对应到配置文件上来就是以
  • {label} 表示 git 分支,默认是master分支,如果项目是以分支做区分也是可以的,那就可以通过不同的 label 来控制访问不同的配置文件了。
    在这里插入图片描述

创建Config Client

准备工作

创建一个spring-boot项目,取名为config-client,添加maven依赖。

添加依赖

<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>

bootstrap.yml配置

spring:
  application:
    name: config-client
  cloud:
    config:
      uri: http://localhost:8500/ #配置服务地址
      label: master
      profile: dev

编写测试接口

@RestController
@RequestMapping("/api")
public class ConfigPrintController {
    @Value("config.name")
    private String name;
    @Value("config.age")
    private int age;

    @PostMapping("/printConfig")
    public String printConfig(){
        return "name:" + name + ",age:" + age;
    }
}

启动config-server和client

在这里插入图片描述

高可用的配置中心

准备工作

配置中心可以做成一个微服务,将其集群化,从而达到高可用。其中注册中心还是沿用前面的eureka-serve,修改config-server、config-client

新增依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

修改config-server

server:
  port: 8500
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/fengfangithub/study-springcloud.git
          search-paths: springcloud-config
          default-label:   master
          username: 
          password: 

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    # 自定义服务名称信息
    instance-id: config-server:8500
    #访问路径可以显示Ip地址
    prefer-ip-address: true

修改config-client

和config-server一样配置到注册中心,这里不再重复。

spring:
  application:
    name: config-client
  cloud:
    config:
#      uri: http://localhost:8500/ #配置服务地址
      label: master
      profile: dev
      discovery:
        enabled: true #从配置中心读取文件
        service-id: config-server #配置中心的servieId,即服务名。

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    # 自定义服务名称信息
    instance-id: config-client:8600
    #访问路径可以显示Ip地址
    prefer-ip-address: true

启动多个config-server

在这里插入图片描述

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一个用于构建分布式系统的开源框架,它基于Spring Boot提供了一套完整的微服务解决方案。下面是Spring Cloud的发展历程: 1. 2014年:Spring Cloud的前身是Netflix公司的一些开源项目,如Eureka、Hystrix、Ribbon等。这些项目提供了服务注册与发现、断路器、负载均衡等功能,为微服务架构提供了基础支持。 2. 2015年:Spring Cloud正式成为Spring官方项目的一部分,发布了第一个版本。这个版本主要包括了对Netflix开源项目的整合和封装,使得开发者可以更方便地使用这些功能。 3. 2016年:Spring Cloud发布了Dalston版本,引入了更多的组件和功能。其中包括了Zuul网关、Config配置中心、Feign声明式HTTP客户端等。这些组件进一步提升了微服务架构的可扩展性和灵活性。 4. 2017年:Spring Cloud发布了Edgware版本,引入了更多的功能和改进。其中包括了Sleuth分布式跟踪、Zipkin链路追踪、Spring Cloud Stream消息驱动等。这些功能使得微服务架构更加易于监控和调试。 5. 2018年:Spring Cloud发布了Finchley版本,继续引入了新的功能和改进。其中包括了Spring Cloud Gateway网关、Spring Cloud Circuit Breaker断路器等。这些功能进一步提升了微服务架构的性能和可靠性。 6. 2019年:Spring Cloud发布了Greenwich版本,继续完善了现有功能并引入了新的功能。其中包括了Spring Cloud LoadBalancer负载均衡、Spring Cloud Function函数式编程等。这些功能使得微服务架构更加灵活和高效。 7. 2020年:Spring Cloud发布了Hoxton版本,继续改进了现有功能并引入了新的功能。其中包括了Spring Cloud Alibaba组件、Spring Cloud Kubernetes支持等。这些功能进一步扩展了Spring Cloud在云原生领域的应用范围。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值