Java后端爱上SpringCloud 第四节:路由网关Zuul和Gateway
Java后端爱上SpringCloud第四节:路由网关Zuul和Spring-Cloud-Gateway一些链接Zuul和Spring-Cloud-Gateway区别Zuul应用Spring-Cloud-Gateway应用一些链接Spring Cloud Gateway替代zuul作为API网关spring cloud-构建微服务架构的网关(API GateWay)Zuul和Sprin...
·
Java后端爱上SpringCloud 第四节:路由网关Zuul和Spring-Cloud-Gateway
一些链接
Spring Cloud Gateway替代zuul作为API网关
spring cloud-构建微服务架构的网关(API GateWay)
Zuul和Spring-Cloud-Gateway区别
- Zuul 1.X是一种异步阻塞的server,不支持长连接,不支持WebSocket,但是2.0是异步非阻塞的,性能有很大的提高,支持长连接,也支持WebSocket。但是2.1.1的SpringCloud默认集成的是1.3.1。
- Spring-Cloud-Gateway是Spring-Cloud无缝的一个组件。
- 两者都是做路由网关的
Zuul应用
新建一个My-Spring-Zuul:
完成的pom.xml如下:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hyn</groupId>
<artifactId>My-Spring-Zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>My-Spring-Zuul</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-netflix-eureka-client</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
启动类:
package com.hyn.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@SpringBootApplication
@EnableEurekaClient
public class MySpringZuulApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringZuulApplication.class, args);
}
}
yml文件及其解释如下:
spring:
application:
name: spring-boot-zuul
server:
port: 8767
# tag::configuration-eureka[]
management:
endpoints:
web:
exposure:
include: "*" #<2>
endpoint:
health:
show-details: ALWAYS
# end::configuration-eureka[]
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
zuul:
routes:
ribbon-api:
#可以指定 URL,但是需要知道每个服务的url,很是不方便
# url: http://localhost:8765/
#sericeId 指定了Eureka中注册的服务
serviceId: spring-boot-ribbon
#过滤以ribbon-api 开头的所有的请求给 spring-boot-ribbon 服务
path: /ribbon-api/**
feign-api:
path: /feign-api/**
serviceId: spring-boot-feign
# url: http://localhost:8763/
Spring-Cloud-Gateway应用
新建一个My-Spring-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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.hyn</groupId>
<artifactId>My-Spring-Gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>My-Spring-Gateway</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</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-netflix-eureka-client</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.security.oauth/spring-security-oauth2 -->
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
yml文件及其解释如下:
spring:
application:
name: spring-boot-gateway
cloud:
gateway:
routes:
#id
- id: ribbon-api
#要发送的url
uri: http://127.0.0.1:8765
#配置要过滤的path,所有ribbon-api 都将路由到http://127.0.0.1:8765
predicates:
- Path=/ribbon-api/**
#配置过滤规则,1位全部过滤
filters:
- StripPrefix=1
- id: feign-api
uri: http://127.0.0.1:8763
predicates:
- Path=/feign-api/**
filters:
- StripPrefix=1
server:
port: 8766
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
更多推荐
已为社区贡献1条内容
所有评论(0)