spring gateway使用基于netty异步io,第二代网关;
zuul 1使用servlet 3,第一代网关,每个请求一个线程,同步Servlet,多线程阻塞模型。
而spring貌似不想在支持zuul 2了

API网关作为后端服务的统一入口,可提供请求路由、协议转换、安全认证、服务鉴权、流量控制、日志监控等服务。

1.搭一个简单的网关

idea中file-new-project

pom.xml文件(引入eureka)

<?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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>gatewaytest2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gatewaytest2</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</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>
</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> </project>

applicaton.yml文件配置

spring:
application:
name: gateway8710
cloud:
gateway:
default-filter:
routes:
- id: user-server
predicates:
- Path=/java/**
filters:
- StripPrefix=1
uri: lb://service-helloword
# uri: "http://192.168.111.133:8708/project/hello"
server:
port: 8710
eureka:
client:
serviceUrl:
#指向注册中心
defaultZone: http://192.168.111.133:8888/eureka/
instance:
# 每间隔1s,向服务端发送一次心跳,证明自己依然”存活“
lease-renewal-interval-in-seconds: 1
# 告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我踢出掉。
lease-expiration-duration-in-seconds: 2

注意:uri项中的lb第一个字母L的小写,配置这个表示要去eureka中查找相关的服务

StripPrefix=1表示去掉url中的前缀,如http://localhost:8710/java/project/hello,经过网关转后变成http://192.168.111.133:8708/project/hello,即去掉前缀/java,后面的不变,去注册中心找service-helloword服务的地址和端口

当配置uri使用绝对路径时写了项目路径(/project/hello)uri: "http://192.168.111.133:8708/project/hello",不管请求的是路径是什么(http://localhost:8710/java/xxx/xxx),都直接访问配置的地址http://192.168.111.133:8708/project/hello

当配置uri使用绝对路径时没写项目路径(/project/hello)如uri: "http://192.168.111.133:8708",请求http://localhost:8710/java/xxx/xxx转发后变为http://192.168.111.133:8708/xxx/xxx

启动类Gatewaytest2Application.java

package com.example.gatewaytest2;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient
@SpringBootApplication
public class Gatewaytest2Application { public static void main(String[] args) {
SpringApplication.run(Gatewaytest2Application.class, args);
} }

目录结构

2.启动测试

注册中心已经有我们的网关服务了

测试

返回


3.附service-helloword

controller

package com.pu.helloworld;

import com.pu.common.ExcelUtil;
import com.pu.ioctest.IocTest;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder; @RestController
@RequestMapping("/project")
public class helloController {
private static Logger log = LoggerFactory.getLogger(helloController.class); @RequestMapping(value = "/hello")
//required=false 表示url中可以不传入id参数,此时就使用默认参数
public String say(@RequestParam(value = "id", required = false, defaultValue = "hello world") String input) {
log.debug("your input :" + input);
return "your input :" + input;
}
}

spring cloud网关gateway的更多相关文章

  1. spring cloud 网关服务

    微服务 网关服务 网关服务是微服务体系里面重要的一环. 微服务体系内,各个服务之间都会有通用的功能比如说:鉴权.安全.监控.日志.服务调度转发.这些都是可以单独抽象出来做一个服务来处理.所以微服务网关 ...

  2. Spring Cloud 网关服务 zuul 二

    有一点上篇文章忘了 讲述,nacos的加载优先级别最高.服务启动优先拉去配置信息.所以上一篇服务搭建我没有讲述在nacos 中心创建的配置文件 可以看到服务端口和注册中心都在配置文件中配置化 属性信息 ...

  3. Spring Cloud 网关服务 zuul 三 动态路由

    zuul动态路由 网关服务是流量的唯一入口.不能随便停服务.所以动态路由就显得尤为必要. 数据库动态路由基于事件刷新机制热修改zuul的路由属性. DiscoveryClientRouteLocato ...

  4. Spring Cloud 组件 —— gateway

    Spring Cloud 网关主要有三大模块:route.predicates.filters 其中 filter 最为关键,是功能增强的核心组件. 列举出一些功能组件: 5.6 CircuitBre ...

  5. spring Cloud网关之Spring Cloud Gateway

    Spring Cloud Gateway是什么?(官网地址:https://cloud.spring.io/spring-cloud-gateway/reference/html/) Spring C ...

  6. Spring Cloud 之 Gateway 知识点:网关

    Spring Cloud Gateway 是使用 netty+webflux 实现因此不需要再引入 web 模块. Spring Cloud Gateway 提供了一种默认转发的能力,只要将 Spri ...

  7. Spring Cloud 之 Gateway.

    一.Gateway 和 Zuul 的区别 Zuul 基于servlet 2.5 (works with 3.x),使用阻塞API.它不支持任何长期的连接,如websocket. Gateway建立在S ...

  8. Spring Cloud Alibaba - Gateway

    Gateway Gateway简介 底层使用Netty框架,性能大于Zuul 配置gateway模块,一般使用yaml格式: server: port: 80 #spring boot actuato ...

  9. spring cloud网关通过Zuul RateLimit 限流配置

    目录 引入依赖 配置信息 RateLimit源码简单分析 RateLimit详细的配置信息解读 在平常项目中为了防止一些没有token访问的API被大量无限的调用,需要对一些服务进行API限流.就好比 ...

随机推荐

  1. [Python3 填坑] 002 isdecimal() 与 isdigit() 的区别 + isnumeric() 的补充

    目录 1. print( 坑的信息 ) 2. isdecimal() 官方文档 3. isdigit() 官方文档 4. 举例 (1) 先说结论 (2) 示例 5. 补充 isnumeric() (1 ...

  2. Java 和操作系统交互,你猜会发生什么?

    作者:lonelysnow https://www.jianshu.com/p/7f6832d61880 结合 CPU 理解一行 Java 代码是怎么执行的 根据冯·诺依曼思想,计算机采用二进制作为数 ...

  3. Lost Cows POJ 2182 思维+巧法

    Lost Cows POJ 2182 思维 题意 是说有n头牛,它们身高不一但是排成了一队,从左到右编号为1到n,现在告诉你从第二号开始前面的那些牛中身高小于它的个数,一共有n-1个数.然后求出它们按 ...

  4. AtCoder Beginner Contest 133 B - Good Distance

    地址:https://atcoder.jp/contests/abc133/tasks/abc133_b 核心问题:判断一个浮点数开方是否为整数 ; double ans1=sqrt(ans); if ...

  5. vue中如何实时修改输入的值

    vue中如何实时修改输入的值 经常看到需要对用户输入的值进行实时修改,有时是需要修改为指定的展示内容,有时候是用来校验,禁止用户输入非法数据,总之是一个常见的需求吧,只是自己一直没有特意去关注.思来想 ...

  6. 关于.net的精彩对话(转)

    [序言]我想很多爱好软件编程的网友都像我一样,对微软推出的.net平台充满了好奇,但是看了相关的文档也是一头雾水,还好,Purple很幸运在QQ上遇到了一位.net高手,经过高手的一番教导,Purpl ...

  7. C++11常用特性总结

    非原创,转载出处 http://www.cnblogs.com/feng-sc C++11已经出来很久了,网上也早有很多优秀的C++11新特性的总结文章,在编写本博客之前,博主在工作和学习中学到的关于 ...

  8. nodejs npm资料

    安装淘宝的 cnpm : npm install --global cnpm 不想安装 cnpm  又想使用淘宝的服务器来下载 : npm install jquery --registry=http ...

  9. yum软件包管理

    一.yum常用命令 #yum -y install [软件包名] //安装 #yum erase [软件包名] //卸载 #yum clean all //清除缓存 #yum makecache // ...

  10. DispatcherServlet的工作原理

    下面是DispatcherServlet的工作原理图,图片来源于网络. 下面是我从DispatcherServlet源码层面来分析其工作流程: 1.请求到达后,调用HandlerMapping来查找对 ...