irpas技术客

五、SpringCloud之Gateway网关整合Eureka_泥耳_eureka整合gateway

大大的周 5396

一、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/服务名/接口


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #eureka整合gateway #一SpringCloud #Spring #Webflux #之上构建 #API #网关的库