4、微服務系列-服務閘道器Gateway
阿新 • • 發佈:2021-05-28
目錄 Spring Cloud Alibaba 微服務系列文章
- 0、Spring Cloud Alibaba微服務系列-準備工作
- 1、Spring Cloud Alibaba微服務系列-服務註冊中心Nacos
- 2、Spring Cloud Alibaba微服務系列-配置中心Nacos
- 3、Spring Cloud Alibaba微服務系列-服務呼叫Open Feign
- 4、Spring Cloud Alibaba微服務系列-服務閘道器Gateway(當前)
- 5、Spring Cloud Alibaba微服務系列-分散式鎖Redisson
- 6、Spring Cloud Alibaba微服務系列-分散式事務Seata
- 7、Spring Cloud Alibaba微服務系列-訊息佇列RocketMQ未完成
- 8、Spring Cloud Alibaba微服務系列-分散式id(雪花演算法)未完成
- 9、Spring Cloud Alibaba微服務系列-服務限流、熔斷、降級Sentinel未完成
- 10、Spring Cloud Alibaba微服務系列-服務鏈路追蹤Skywalking未完成
- 11、Spring Cloud Alibaba微服務系列-分庫分表ShardingSphere未完成
- 12、Spring Cloud Alibaba微服務系列-分散式任務排程xxl-job未完成
- 13、Spring Cloud Alibaba微服務系列-分散式檔案系統Minio
一、前言
生產環境中所有的請求入口都是統一的,一般用Nginx->Gateway。微服務之間相互呼叫是走內網。Gateway是所有服務對外的入口。在Gateway上可以做許可權的認證和攔截。
二、Maven依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.llh</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gateway</artifactId>
<version>1.0.0</version>
<name>gateway</name>
<description>gateway description</description>
<packaging>jar</packaging>
<dependencies>
<!--服務註冊與發現-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--閘道器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--負載均衡器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>
三、配置檔案 application.properties
server.port=20000
# 應用名稱
spring.application.name=gateway
# nacos服務註冊相關
spring.cloud.nacos.discovery.server-addr=http://localhost:8848
spring.cloud.nacos.discovery.namespace=83e9d384-d49e-4f40-84bf-c25612883dcc
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
# 閘道器相關
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
spring.cloud.gateway.routes[0].id=consumer
spring.cloud.gateway.routes[0].uri=lb://consumer
spring.cloud.gateway.routes[0].predicates[0]=Path=/order/**
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1
- 這裡做了路由更改,所有/order開頭的介面會路由到
consumer
服務上的介面。
四、自定義Filter
MyFilter
類
package com.llh.gateway;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
/**
* @author 小虎哥的技術部落格
*/
@Component
public class MyFilter implements WebFilter{
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
String path = request.getPath().toString();
// 列印請求路徑,生產環境可以在此做一些許可權認證等
System.out.println(path);
return chain.filter(exchange);
}
}
- 所有的請求都會通過這個攔截器,這裡只是列印了一下請求路徑,生產環境可以在此做一些許可權認證等
五、執行
請求閘道器的介面都會被轉發到具體的服務中
請求介面: 閘道器ip:閘道器port/服務名/服務內部介面
如 http://localhost:20000/consumer/consumer/buy/1/1
因為做了路由的轉發
http://localhost:20000/order/consumer/buy/1/1
就等於
http://localhost:20000/consumer/consumer/buy/1/1
六、結語
原始碼地址:https://github.com/tigerleeli/xiaohuge-blog/tree/master/spring-cloud-alibaba-gateway
同步微信公眾號:小虎哥的技術部落格