gateway 功能
?

?
创建gateway
pom 依赖
<?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">
<modelVersion>4.0.0</modelVersion>
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>spring-cloud-imooc-demo</artifactId>
<groupId>org.imooc</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.imooc</groupId>
<artifactId>geteway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>geteway</name>
<description>geteway - sample</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- geteway 不依赖 spring-boot-starter-web -->
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
spring.application.name=gateway-service
#表明gateway开启服务注册和发现的功能,并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务
spring.cloud.gateway.discovery.locator.enabled=true
server.port=65000
eureka.client.serviceUrl.defaultZone=http://eureka1:8860/eureka
## 开启所有actuator-endpoint
management.endpoint.health.show-details=always
## 全部放开使用*,或把需要开放的接口端点使用“,”隔开,如:env,health。
management.endpoints.web.exposure.include=*
?Application启动类
@SpringBootApplication
@EnableDiscoveryClient
?启动 gateway 以及 两个 eureka-client (服务提供者)

gateway endpoints端点
?访问?http://localhost:65000/actuator?查看所有的 endpoints? 端点。可以看到一个新的gateway端点
"gateway": {
"href": "http://localhost:65000/actuator/gateway",
"templated": false
}

gateway路由规则
?访问这个新的端点? ?http://localhost:65000/actuator/gateway/routes
可以看到?spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务。
gateway 自动创建了 断言 predicates 、 过滤器 filters

gateway 负载均衡
通过 gateway 访问 EUREKA-CLIENT 微服务:?http://localhost:65000/EUREKA-CLIENT/hi? 调用两次可以看到 gateway 自动帮我们完成了负载均衡。


URL 路径中?EUREKA-CLIENT 是大写。因为 Eureka 注册中心的 Application 就是大写

|