springCloud之路由网关gateway

简介: springCloud之路由网关gateway

前置:配置注册中心

https://yjtzfywh.blog.csdn.net/article/details/129220590

微服务网关是介于客户端和服务器端之间的中间层,所有的外部请求都会先经过微服务网关。

一、导入依赖

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

二、配置文件

server:
  port: 8500
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8801/eureka, http://localhost:8802/eureka
spring:
  application:
    name: gateway
 
  cloud:
    gateway:
      routes:
      - id: borrow-service
        uri: lb://borrowservice
        predicates:
          - Path=/borrow/**
      # 继续添加新的路由配置,这里就以书籍管理服务为例
      # 注意-要对齐routes:
      - id: book-service
        uri: lb://bookservice
        predicates:
          - Path=/book/**
        filters: # 添加过滤器
          - AddRequestHeader=Test, HelloWorld!
        # AddRequestHeader 就是添加请求头信息,其他工厂请查阅官网

三、启动类

package com.minos;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
 
@SpringBootApplication
public class GateWayServerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(GateWayServerApplication.class, args);
    }
}

四、自定义全局路由过滤

1.import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
 
import java.util.List;
 
@Component   //需要注册为Bean
public class TestFilter implements GlobalFilter, Ordered {//指定过滤器之间的顺序
 
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {   //只需要实现此方法
        //先获取ServerHttpRequest对象,注意不是HttpServletRequest
        ServerHttpRequest request = exchange.getRequest();
        //打印一下所有的请求参数
        System.out.println(request.getQueryParams());
        //判断是否包含test参数,且参数值为1 ?test=1
        List<String> value = request.getQueryParams().get("test");
        if (value != null && value.contains("1")) {
            //将ServerWebExchange向过滤链的下一级传递(跟JavaWeb中介绍的过滤器其实是差不多的)
            return chain.filter(exchange);
        } else {
            //直接在这里不再向下传递,然后返回响应
            return exchange.getResponse().setComplete();
        }
    }
 
    /**
     * 注意Order的值越小优先级越高,
     * 并且无论是在配置文件中编写的单个路由过滤器还是全局路由过滤器,都会受到Order值影响
     *
     * @return
     */
    @Override
    public int getOrder() {
        return 0;
    }
}

五、测试

 

启动服务,注册中心注册正常。

通过注册中心访问

http://localhost:8500/borrow/1?test=1
{
"user": {
"uid": 1,
"name": "小明",
"sex": "男"
},
"bookList": [
{
"bid": 1,
"title": "深入理解Java虚拟机",
"desc": "讲解JVM底层原理"
},
{
"bid": 2,
"title": "Java并发编程的艺术",
"desc": "讲解并发编程的各种技术"
}
]
}

直接访问

http://localhost:8301/borrow/1
{
"user": {
"uid": 1,
"name": "小明",
"sex": "男"
},
"bookList": [
{
"bid": 1,
"title": "深入理解Java虚拟机",
"desc": "讲解JVM底层原理"
},
{
"bid": 2,
"title": "Java并发编程的艺术",
"desc": "讲解并发编程的各种技术"
}
]
}
相关文章
|
8天前
|
负载均衡 Java Nacos
SpringCloud基础2——Nacos配置、Feign、Gateway
nacos配置管理、Feign远程调用、Gateway服务网关
SpringCloud基础2——Nacos配置、Feign、Gateway
|
19天前
|
安全 Java 开发者
强大!Spring Cloud Gateway新特性及高级开发技巧
在微服务架构日益盛行的今天,网关作为微服务架构中的关键组件,承担着路由、安全、监控、限流等多重职责。Spring Cloud Gateway作为新一代的微服务网关,凭借其基于Spring Framework 5、Project Reactor和Spring Boot 2.0的强大技术栈,正逐步成为业界的主流选择。本文将深入探讨Spring Cloud Gateway的新特性及高级开发技巧,助力开发者更好地掌握这一强大的网关工具。
72 6
|
1月前
|
Java API 微服务
服务网关Gateway
该博客文章详细介绍了Spring Cloud Gateway的使用方法和概念。文章首先阐述了API网关在微服务架构中的重要性,解释了客户端直接与微服务通信可能带来的问题。接着,文章通过具体的示例代码,展示了如何在Spring Cloud Gateway中添加依赖、编写路由规则,并对路由规则中的基本概念如Route、Predicate和Filter进行了详细解释。最后,文章还提供了路由规则的测试方法。
服务网关Gateway
|
1月前
|
负载均衡 Java 数据安全/隐私保护
网关路由-路由属性
网关路由-路由属性
41 1
|
27天前
|
安全 API
【Azure API 管理】APIM Self-Host Gateway 自建本地环境中的网关数量超过10个且它们的出口IP为同一个时出现的429错误
【Azure API 管理】APIM Self-Host Gateway 自建本地环境中的网关数量超过10个且它们的出口IP为同一个时出现的429错误
|
28天前
|
负载均衡 网络架构
|
28天前
|
存储 容器
【Azure 事件中心】为应用程序网关(Application Gateway with WAF) 配置诊断日志,发送到事件中心
【Azure 事件中心】为应用程序网关(Application Gateway with WAF) 配置诊断日志,发送到事件中心
|
2月前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
61 3
|
2月前
|
Java 微服务 Spring
SpringCloud gateway自定义请求的 httpClient
SpringCloud gateway自定义请求的 httpClient
112 3
|
1月前
|
负载均衡 Java 应用服务中间件
Gateway服务网关
本节针对微服务中另一重要组件:网关 进行了实战性演练,网关作为分布式架构中的重要中间件,不仅承担着路由分发(重点关注Path规则配置),同时可根据自身负载均衡策略,对多个注册服务实例进行均衡调用。本节我们借助GateWay实现的网关只是技术实现的方案之一,后续大家可能会接触像:Zuul、Kong等,其实现细节或有差异,但整体目标是一致的。

热门文章

最新文章