这篇文章讲述了如何简单地使用Spring Cloud Gateway,来源于Spring Cloud官方案例,
地址:https://spring.io/projects/spring-cloud-gateway#overview
文档:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/
简介
Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关。网关作为流量的,在微服务系统中有着非常作用,网关常见的功能有路由转发、权限校验、限流控制等作用。
This project provides an API Gateway built on top of the Spring
Ecosystem, including: Spring 5, Spring Boot 2 and Project Reactor.
Spring Cloud Gateway aims to provide a simple, yet effective way to
route to APIs and provide cross cutting concerns to them such as:
security, monitoring/metrics, and resiliency.本项目提供了一个构建在 Spring 生态系统之上的 API 网关,包括:Spring 5、Spring Boot 2 和 Project
Reactor。Spring Cloud Gateway 旨在提供一种简单而有效的方式来路由到 API
并为它们提供交叉关注点,例如:安全性、监控/指标和弹性。
gateway可以路由到的三种目标地址:
1、websocket
2、http、https
3、微服务
在gateway中配置uri配置有三种方式,包括
第一种:ws(websocket)方式: uri: ws://localhost:9000
第二种:http方式: uri: http://localhost:8130/
第三种:lb(注册中心中服务名字)方式: uri: lb://brilliance-consumer
创建工程
nacos-provider
nacos-ribbon
nacos-feign
nacos-gateway
需要注意的是:nacos-ribbon、nacos-feign请求接口需要加上命令空间
nacos-ribbon
package com.nacosribbon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RequestMapping("/ribbon")
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@GetMapping(value = "/hi-rest")
public String hi(@RequestParam String name) {
return restTemplate.getForObject("http://nacos-provider/hi?name="+name,String.class);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RequestMapping("/goods")
@RestController
public class GoodsController {
@Autowired
RestTemplate restTemplate;
@GetMapping(value = "/goodsName")
public String goodsName(@RequestParam String name) {
return restTemplate.getForObject("http://nacos-provider/hi?name="+name,String.class);
}
}
nacos-feign
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/feign")
@RestController
public class HiController {
//编译器报错,无视。 因为这个Bean是在程序启动的时候注入的,编译器感知不到,所以报错。
@Autowired
ConsumerHi consumerHi;
@GetMapping(value = "/hi-feign")
public String sayHi(@RequestParam String name) {
return consumerHi.sayHiFromConsumer( name );
}
}
nacos-gateway配置
1、引入依赖
<properties>
<java.version>1.8</java.version>
<spring.cloud-version>2020.0.4</spring.cloud-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2021.1</version>
</dependency>
<!--fegin组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- Feign Client for loadBalancing -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2、nacos-gateway配置文件
在gateway中配置uri配置有三种方式,包括
第一种:ws(websocket)方式: uri: ws://localhost:9000
第二种:http方式: uri: http://localhost:8130/
第三种:lb(注册中心中服务名字)方式: uri: lb://brilliance-consumer
server.port=8764
spring.application.name=nacos-gateway
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.routes[0].id=NACOS-RIBBON
spring.cloud.gateway.routes[0].uri=lb://nacos-ribbon
spring.cloud.gateway.routes[0].predicates[0]= Path=/ribbon/**,/goods/**
spring.cloud.gateway.routes[1].id=NACOS-FEIGN
spring.cloud.gateway.routes[1].uri=lb://nacos-feign
spring.cloud.gateway.routes[1].predicates[0]= Path=/feign/**
3、启动类加上@EnableDiscoveryClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class NacosGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(NacosGatewayApplication.class, args);
}
}
访问gateway
请求ribbon的服务:
http://localhost:8764/ribbon/hi-rest?name=小火锅
服务端【8761】提供消息,客户端传过来的name= 小火锅
http://localhost:8764/goods/goodsName?name=小辣条
服务端【8761】提供消息,客户端传过来的name= 小辣条
请求feign的服务:
http://localhost:8764/feign/hi-feign?name=小蜘蛛
服务端【8761】提供消息,客户端传过来的name= 小蜘蛛
上面8761为nacos-provider的服务