Spring Cloud GateWay
目录
- Spring Cloud GateWay
- 依赖
- 路由配置
- 测试网关
- REFERENCE
- 更多
手机用户请
横屏
获取最佳阅读体验,REFERENCES
中是本文参考的链接,如需要链接和更多资源,可以关注其他博客发布地址。
平台 | 地址 |
简书 | https://www.jianshu.com/u/3032cc862300 |
个人博客 | https://yiyuery.github.io/NoteBooks/ |
官网简介
Spring Cloud Gateway
该项目提供了一个用于在Spring MVC之上构建API网关的库。 Spring Cloud Gateway旨在提供一种简单而有效的方式来路由API并为他们提供横切关注点,例如:安全性,监控/指标和弹性。
Spring Cloud Gateway 官网特性
- 基于Spring Framework 5,Project Reactor和Spring Boot 2.0构建
- 能够匹配任何请求属性上的路由。
- 指定路由配置属性和过滤器。
- Hystrix Circuit Breaker集成。
- Spring Cloud DiscoveryClient集成
- 易于编写属性和过滤器
- 限流
- 路径重写
依赖
buildscript {
repositories {
maven { url = "https://plugins.gradle.org/m2/" }
maven { url = "http://maven.aliyun.com/nexus/content/groups/public/" }
jcenter()
}
dependencies {
classpath libs["spring-boot-gradle-plugin"]
}
}
apply plugin: "io.spring.dependency-management"
apply plugin: "org.springframework.boot"
bootJar {
baseName = 'capsule-gateway'
version = '1.0.0'
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Greenwich.SR1"
}
}
dependencies {
//compile project (":capsule-web-simple-dependency")
//implementation "org.springframework.boot:spring-boot-starter-web"
testImplementation "org.springframework.boot:spring-boot-starter-test"
//集成依赖
compile("org.springframework.cloud:spring-cloud-starter-gateway")
compile("org.springframework.cloud:spring-cloud-starter-netflix-hystrix")
compile("org.springframework.cloud:spring-cloud-starter-contract-stub-runner"){
exclude group: "org.springframework.boot", module: "spring-boot-starter-web"
}
}
注意:需要去掉
spring-boot-starter-web
的依赖
路由配置
CapsuleGatewayApplication
package com.example.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.example")
public class CapsuleGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(CapsuleGatewayApplication.class, args);
}
}
RouterConfiguration
/*
* @ProjectName: 编程学习
* @Copyright: 2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.
* @address: http://xiazhaoyang.tech
* @date: 2019/3/26 07:42
* @email: xiazhaoyang@live.com
* @description: 本内容仅限于编程技术学习使用,转发请注明出处.
*/
package com.example.gateway.router;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* <p>
*
* </p>
*
* @author xiazhaoyang
* @version v1.0.0
* @date 2019/3/26 07:42
* @modificationHistory=========================逻辑或功能性重大变更记录
* @modify By: {修改人} 2019/3/26
* @modify reason: {方法名}:{原因}
* ...
*/
@Configuration
public class RouterConfiguration {
@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route(p->p.path("/get")
.filters(f->f.addRequestHeader("Hello","World"))
.uri("http://httpbin.org:80")
)
.build();
}
}
测试网关
访问curl http://localhost:8080/get
返回:
{
"args": {},
"headers": {
"Accept": "*/*",
"Forwarded": "proto=http;host=\"localhost:8080\";for=\"0:0:0:0:0:0:0:1:55727\"",
//通过Filter添加的Header
"Hello": "World",
"Host": "httpbin.org",
"User-Agent": "curl/7.54.0",
"X-Forwarded-Host": "localhost:8080"
},
"origin": "0:0:0:0:0:0:0:1, 115.198.0.193, ::1",
"url": "https://localhost:8080/get"
}
设置网关跳转到自己开发的应用
@Bean
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
return builder.routes()
.route(p->p.path("/get")
//过滤路由并添加Header参数
.filters(f->f.addRequestHeader("Hello","World"))
.uri("http://httpbin.org:80"))
//跳转到自定义服务的路由网关
.route(p->p.path("/capsule-flowable/demo/hi")
.uri("http://localhost:8081"))
.build();
}
/*
* @ProjectName: 编程学习
* @Copyright: 2019 HangZhou xiazhaoyang Dev, Ltd. All Right Reserved.
* @address: http://xiazhaoyang.tech
* @date: 2019/3/26 06:59
* @email: xiazhaoyang@live.com
* @description: 本内容仅限于编程技术学习使用,转发请注明出处.
*/
package com.example.web;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* <p>
*
* </p>
*
* @author xiazhaoyang
* @version v1.0.0
* @date 2019/3/26 06:59
* @modificationHistory=========================逻辑或功能性重大变更记录
* @modify By: {修改人} 2019/3/26
* @modify reason: {方法名}:{原因}
* ...
*/
@RestController
@RequestMapping(value="/demo")
public class HelloController {
@RequestMapping("/hi")
public String hi(){
return "created by capsule-web-simply-dependency:" +
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"\n";
}
}
xiazhaoyang capsule-utils $ curl http://localhost:8080/capsule-flowable/demo/hi
created by capsule-web-simply-dependency:2019-03-27 07:53:20
REFERENCE
Spring官方开发指导文档Spring Cloud Gateway 入门Spring Cloud Gateway入门案例更多
扫码关注“架构探险之道”,获取更多源码和文章资源