五、SpringCloud之Gateway网关整合Eureka

一、SpringCloud Gateway介绍

该项目提供了一个用于在 Spring WebFlux 之上构建 API 网关的库。Spring Cloud Gateway 旨在提供一种简单而有效的方式来路由到 API 并为它们提供交叉关注点,例如:安全性、监控/指标和弹性。

特征

Spring Cloud Gateway 特性:

基于 Spring Framework 5、Project Reactor 和 Spring Boot 2.0

能够匹配任何请求属性的路由。

谓词和过滤器特定于路由。

断路器集成。

Spring Cloud DiscoveryClient 集成

易于编写谓词和过滤器

请求速率限制

路径重写

二、SpringCloud Gateway搭建

首先我们需要建2个服务,Eureka server、Gateway。

2.1、Eureka Server搭建

新建springboot项目neil-eureka-server。

在pom.xml里面添加Eureka和web的依赖

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

配置文件application.yml

server:
  port: 8989

spring:
  application:
    name: neil-eureka-server

eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8989/eureka/
    fetch-registry: true
    register-with-eureka: true
  instance:
    prefer-ip-address: true

springboot启动类里面添加@EnableEurekaServer注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableEurekaServer
@SpringBootApplication
@RestController
public class NeilEurekaServerApplication {

	@RequestMapping("/get")
	public String get(){
		return "hello world";
	}

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

启动neil-eureka-server。

在浏览器地址栏输入机器ip加端口访问Eureka页面
http://127.0.0.1:8989/

请添加图片描述

2.2、gateway搭建

新建springboot项目neil-gateway-server。

在pom.xml里面添加Eureka和gateway的依赖

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

配置文件application.yml

server:
  port: 8990
  servlet:
    context-path: /
spring:
  application:
    name: neil-gateway-server
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true   #开启Eureka服务发现
          lower-case-service-id: true
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8989/eureka/ #Eureka Server地址
  instance:
    prefer-ip-address: true


springboot启动类里面添加@EnableEurekaServer注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@EnableEurekaClient
@SpringBootApplication
public class NeilGatewayServerApplication {

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

}

启动neil-gateway-server

请添加图片描述

2.3、测试

调用eureka服务上写的接口

http://localhost:8989/get

这个是直接调用eureka服务接口

请添加图片描述

然后通过gateway调用eureka接口

http://localhost:8990/neil-eureka-server/get

请添加图片描述

gateway调用其他服务的方式是

http://localhost:8990/服务名/接口

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
### 回答1: Spring Cloud GatewaySpring Cloud生态系统中的一个全新项目,它基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术,旨在为微服务架构提供一种简单而有效的统一的API路由管理方式。Spring Cloud Gateway作为Spring Cloud网关,提供了一种简单而有效的方式来管理API请求的路由、过滤和转换。它可以与Eureka、Consul、Zookeeper等服务注册中心集成,支持动态路由、限流、熔断等功能,可以帮助我们快速构建高可用、高性能的微服务架构。 ### 回答2: Spring Cloud GatewaySpring Cloud 的一个全新项目,它基于 Spring 5.0,是一个独立的路由和过滤器网关,领域涵盖协议转换、流量整形、安全、监控/指标、日志等。它可以使用 Java 开发,也可以使用 Groovy 开发。它旨在为微服务架构提供一种简单而有效的方式,以实现路由、监控和安全等诸多目标功能。 Spring Cloud Gateway 的一个关键特性是能够动态路由,它可以根据业务实现和运行情况以及使用者的需求,对请求进行处理和转发。同时,Spring Cloud Gateway 不仅支持各种协议的路由和服务转发,还提供了强大的过滤器机制,可以在请求和响应阶段进行访问控制、操作转换等操作。 使用Spring Cloud Gateway整合Gateway可以快速构建高性能的微服务应用,并实现各种路由和过滤器功能。 首先安装 Spring Cloud Gateway 依赖,同时在pom.xml文件中添加如下代码: ``` <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> </dependencies> ``` 接下来,需要添加路由规则。Spring Cloud Gateway 支持两种方式添加路由规则,一种是采用配置文件方式,另外一种是采用编程方式。对于较为简单的场景,采用配置文件方式会更为便捷和方便。配置文件需要写在 application.properties(或者 application.yml)或自定义的配置文件中,在其中配置路由规则。可以通过在配置文件 application.yml 中,指定 routes 来定义路由: ``` spring: cloud: gateway: routes: - id: demo uri: https://example.com/ predicates: - Path=/demo ``` 上述配置的作用是当访问 http://localhost:8080/demo 时,转发到 https://example.com/。 需要注意的是,Spring Cloud Gateway 的路由规则是可以动态修改的,只需要将新增、修改、删除的路由规则写入相应的配置文件,无需重启服务即可生效。 最后,需要添加过滤器,Spring Cloud Gateway 提供了两种过滤器,一种是全局过滤器,另外一种是基于路由的过滤器。全局过滤器能够匹配所有路由,而基于路由的过滤器仅匹配特定的路由。在使用过滤器时,需要注意过滤器的执行顺序和配置顺序有关,过滤器的执行顺序范围为0-10000。 Spring Cloud Gateway 实现了网关的基本功能,可以轻松地实现反向代理、路由转发、日志监控等功能,为 Spring Cloud 应用带来了更高效的边缘服务体系。通过使用 Spring Cloud Gateway 整合 Gateway,可以更迅速地进行微服务架构的开发和部署。 ### 回答3: 随着微服务的广泛应用,微服务架构下的网关服务也逐渐变得重要起来。Spring Cloud Gateway作为一种轻量级、高效、易扩展的网关服务,已成为业界热门的选择。在使用Spring Cloud Gateway时,需要将其与Spring Cloud的其它组件进行整合,以达到更好的服务支持和扩展性。 一、整合方式 1.在pom.xml文件中加入以下依赖: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> ``` 2.配置路由规则,可以在application.yml或application.properties文件中进行配置,在以下例子中,我们定义了一个简单的路由规则: ``` spring: cloud: gateway: routes: - id: route1 uri: http://example.org predicates: - Path=/foo/** - id: route2 uri: http://localhost:8080 predicates: - Path=/bar/** ``` 这里我们定义了两个路由规则,route1和route2,分别匹配以/foo和/bar开头的请求。其中uri属性指定了路由的目标地址,predicates属性指定了路由规则。 3.自定义过滤器,Spring Cloud Gateway提供了Filter接口,可以实现基于请求和响应的过滤器。以下是一个自定义的LoggingFilter,用来记录请求和响应的日志: ``` @Component public class LoggingFilter implements GlobalFilter, Ordered { private static final Logger log = LoggerFactory.getLogger(LoggingFilter.class); private static final String START_TIME = "startTime"; @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { exchange.getAttributes().put(START_TIME, System.currentTimeMillis()); return chain.filter(exchange).then( Mono.fromRunnable(() -> { Long startTime = exchange.getAttribute(START_TIME); if (startTime != null) { log.info(exchange.getRequest().getURI().getRawPath() + ": " + (System.currentTimeMillis() - startTime) + " ms"); } }) ); } @Override public int getOrder() { return -1; } } ``` 二、总结 Spring Cloud Gateway整合Spring Cloud的组件和功能,提供了一种轻量级、高效、易扩展的网关服务。通过配置路由规则和自定义过滤器,可以实现网关的基本功能,并且可以方便地扩展和定制化。同时,Spring Cloud Gateway还提供了丰富的插件,支持更多的业务场景和功能需求。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值