SpringCloud 网关配置:spring-cloud-gateway
微服务网关
微服务网关的功能:
路由转发,接收一切外界请求,转发到后端的微服务上去;
请求过滤,在服务网关中可以完成一系列的横切功能,例如权限校验、限流以及监控等,这些都可以通过过滤器完成(其实路由转发也是通过过滤器实现的)
微服务网关种类
Zuul,spring-cloud-gateway,linkerd,Nignx,还有lyft的Envoy,Undertow等等备用方案。
主要的为前4个工具:
Zuul, zuul 1.x 是一个基于阻塞IO的API 网关。提供动态路由,监控,弹性,安全等的边缘服务。是Netflix出品的一个基于JVM路由和服务端的负载均衡器。zuul 2.x 基于Netty,实现了非阻塞IO,支持长连接的API网关。SpringCloud暂时还没有整合计划。
SpringCloud-gateway: 非阻塞的IO 的API网关。上手简单,功能强大。
Linkerd ,基于Scala实现的,目前是市面上仅有生产级别的Service Mesh。
Nginx,不说了。
本文主要介绍spring-cloud-starter-gateway的搭建。如果想看高级的教程的话:
http://www.iocoder.cn/categories/Spring-Cloud-Gateway/
简单的环境搭建(基于Spring-boot-starter-parent,2.0.0.RELEASE)
1.引用依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.isomorphism</groupId>
<artifactId>token-bucket</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<jvmTarget>1.8</jvmTarget>
</configuration>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>src/main/kotlin</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/java</source>
<source>src/test/kotlin</source>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is treated specially by maven -->
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>java-compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>java-test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
2.启动类编写
@SpringBootConfiguration
@EnableAutoConfiguration
public class GatewaySampleApplication {
public static void main(String[] args) {
SpringApplication.run(GatewaySampleApplication.class, args);
}
}
3.编写配置文件
spring:
cloud:
gateway:
default-filters:
- AddResponseHeader=X-Response-Default-Foo, Default-Bar
- AddRequestHeader=fangxiaobai,its sound good
- PrefixPath=/httpbin
routes:
- id: fangxiaobai_test
host: 127.0.0.1
# 除了ws,还有lb,不知道什么意思
uri: http://www.baidu.com/
predicates:
- Host=127.0.0.1
- Method=GET
- Path=/web
filters:
- AddResponseHeader=X-Response-Foo, Bar
- SetPath=/web
详细:http://www.iocoder.cn/Spring-Cloud-Gateway/route-definition-locator-properties/
https://github.com/YunaiV/spring-cloud-gateway/blob/382a4cd98fbb8ac53a83a5559bacb0f885838074/spring-cloud-gateway-core/src/test/resources/application.yml#L10
以上这种方式是使用配置文件的形式来配置路由。除了这种方式,还有供API的形式和kotlin的形式来配置,
对于路由的存储,还有从配置文件中读取,从存储器中读取,从注册中心中读取,也可以组成以上这三种形式的读取路由。
接下来记录一下,从注册中心读取。
非常简单,在配置文件中加入如下配置就可;
cloud:
gateway:
discovery:
locator:
enabled: true
当然你还得有一个注册服务中心,你可以手动搭建一个,也可以。。。。使用这个http://eureka.didispace.com/
今天用了一天的时间,学习了这些内容,其他内容后续补充,,,,,
学习内容:http://www.iocoder.cn/categories/Spring-Cloud-Gateway/
学习内容,,记录下。
最后
如果你觉得写的还不错,就关注下公众号呗,关注后,有点小礼物回赠给你。
你可以获得5000+电子书,java,springCloud,adroid,python等各种视频教程,IT类经典书籍,各种软件的安装及破解教程。
希望一块学习,一块进步!