Day 25:Spring Cloud Config 和 Spring Cloud Gateway

目标:学习 Spring Cloud 配置管理和 API 网关的基础应用。

理论知识

1. Spring Cloud Config:配置管理
  • 配置中心的作用

    • 集中式配置管理:Spring Cloud Config 提供了一种集中管理应用配置的方式,可以将所有应用的配置信息存储在一个中央服务器中,避免在每个服务中重复配置。
    • 动态配置:支持动态加载配置,即当配置文件发生变化时,应用可以实时感知并加载新的配置,减少重启次数。
    • 多环境支持:可以为不同的环境(如开发环境、生产环境)提供不同的配置文件。
  • 使用 Git 或本地文件作为配置源

    • Git:Spring Cloud Config 可以从 Git 仓库加载配置文件,便于版本控制和管理。
    • 本地文件:除了 Git,Spring Cloud Config 还可以使用本地文件系统作为配置源。
2. Spring Cloud Gateway:API 网关
  • API 网关的作用

    • 路由:将外部请求路由到合适的微服务。
    • 负载均衡:通过集成 Ribbon 或其他负载均衡机制,平衡请求分发。
    • 权限控制:通过过滤器对请求进行权限验证或其他拦截操作。
    • 服务聚合:将多个微服务的响应结果合并,返回给客户端。
  • 路由规则配置和过滤器

    • 路由规则:使用 application.yml 配置不同的路由规则,将外部请求转发到相应的微服务。
    • 过滤器:可以通过定义过滤器对请求进行预处理或后处理,例如认证、日志记录、限流等。

实践操作

1. 配置 Spring Cloud Config
1.1 创建 Spring Cloud Config Server
  1. 创建 Config Server 项目: 使用 Spring Initializr 创建一个 Spring Boot 项目,添加依赖:

    • Config Server
    • Spring Web
  2. 启用 Config Server: 在 @SpringBootApplication 注解的类上,添加 @EnableConfigServer 注解以启用配置服务器。

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    }
    
  3. 配置 application.yml: 在 application.yml 中配置 Git 仓库作为配置源(如果使用 Git 作为配置源):

    server:
      port: 8888
    
    spring:
      cloud:
        config:
          server:
            git:
              uri: https://github.com/your-username/config-repo
              searchPaths: '{application}'
    

    如果使用本地文件系统:

    spring:
      cloud:
        config:
          server:
            native:
              searchLocations: file:///path/to/your/config-files/
    
  4. 启动 Config Server: 启动应用后,访问 http://localhost:8888/{application}/{profile},查看配置文件。

1.2 配置客户端获取配置
  1. 创建客户端应用: 在 Spring Boot 应用中,添加 Config Client 依赖。

  2. 配置 bootstrap.yml: 在 bootstrap.yml 中配置配置中心的地址,Spring Cloud Config Client 会在应用启动时从该地址获取配置。

    spring:
      application:
        name: my-app
      cloud:
        config:
          uri: http://localhost:8888
    
  3. 使用配置: 在代码中使用 @Value@ConfigurationProperties 获取配置。

    @Value("${my-config.property}")
    private String myConfigProperty;
    
  4. 启动客户端应用: 启动后,客户端会从配置中心获取配置并应用

2. 配置 Spring Cloud Gateway
2.1 创建 Spring Cloud Gateway 项目
  1. 创建 Gateway 项目: 使用 Spring Initializr 创建一个 Spring Boot 项目,添加依赖:

    • Spring Web
    • Spring Cloud Gateway
  2. 配置 application.yml: 配置 Spring Cloud Gateway 的路由规则。

    spring:
      cloud:
        gateway:
          routes:
            - id: service1_route
              uri: http://localhost:8081
              predicates:
                - Path=/service1/**
            - id: service2_route
              uri: http://localhost:8082
              predicates:
                - Path=/service2/**
    
  3. 启动 Spring Cloud Gateway: 启动后,Spring Cloud Gateway 会监听并根据路由规则将请求转发到相应的微服务。

2.2 配置过滤器
  1. 定义过滤器: 你可以定义自定义过滤器来执行一些操作,例如身份验证、日志记录、限流等。

    import org.springframework.web.server.ServerWebExchange;
    import org.springframework.web.server.WebFilter;
    import org.springframework.web.server.WebFilterChain;
    import reactor.core.publisher.Mono;
    
    @Component
    public class CustomFilter implements WebFilter {
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
            // Perform custom logic before passing the request to the gateway
            return chain.filter(exchange);
        }
    }
    
  2. application.yml 配置过滤器: 你也可以在 application.yml 中定义内置过滤器,如日志记录过滤器:

    spring:
      cloud:
        gateway:
          default-filters:
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 10
                redis-rate-limiter.burstCapacity: 20
    

总结

  • Spring Cloud Config:提供了一个集中的配置管理系统,支持从 Git 仓库或本地文件加载配置。客户端应用可以在启动时自动从配置服务器获取配置信息,支持动态配置更新。
  • Spring Cloud Gateway:是一个 API 网关,主要用于路由请求、负载均衡和权限控制等功能。通过配置文件可以定义路由规则,并且可以通过过滤器进行请求的预处理和后处理。
  • 实践操作
    • 配置 Spring Cloud Config Server,使用 Git 或本地文件作为配置源,客户端应用自动获取配置。
    • 使用 Spring Cloud Gateway 进行 API 路由和配置简单的过滤器,实现微服务之间的请求路由。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值