spring cloud网关gateway
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的更多相关文章
- spring cloud 网关服务
微服务 网关服务 网关服务是微服务体系里面重要的一环. 微服务体系内,各个服务之间都会有通用的功能比如说:鉴权.安全.监控.日志.服务调度转发.这些都是可以单独抽象出来做一个服务来处理.所以微服务网关 ...
- Spring Cloud 网关服务 zuul 二
有一点上篇文章忘了 讲述,nacos的加载优先级别最高.服务启动优先拉去配置信息.所以上一篇服务搭建我没有讲述在nacos 中心创建的配置文件 可以看到服务端口和注册中心都在配置文件中配置化 属性信息 ...
- Spring Cloud 网关服务 zuul 三 动态路由
zuul动态路由 网关服务是流量的唯一入口.不能随便停服务.所以动态路由就显得尤为必要. 数据库动态路由基于事件刷新机制热修改zuul的路由属性. DiscoveryClientRouteLocato ...
- Spring Cloud 组件 —— gateway
Spring Cloud 网关主要有三大模块:route.predicates.filters 其中 filter 最为关键,是功能增强的核心组件. 列举出一些功能组件: 5.6 CircuitBre ...
- spring Cloud网关之Spring Cloud Gateway
Spring Cloud Gateway是什么?(官网地址:https://cloud.spring.io/spring-cloud-gateway/reference/html/) Spring C ...
- Spring Cloud 之 Gateway 知识点:网关
Spring Cloud Gateway 是使用 netty+webflux 实现因此不需要再引入 web 模块. Spring Cloud Gateway 提供了一种默认转发的能力,只要将 Spri ...
- Spring Cloud 之 Gateway.
一.Gateway 和 Zuul 的区别 Zuul 基于servlet 2.5 (works with 3.x),使用阻塞API.它不支持任何长期的连接,如websocket. Gateway建立在S ...
- Spring Cloud Alibaba - Gateway
Gateway Gateway简介 底层使用Netty框架,性能大于Zuul 配置gateway模块,一般使用yaml格式: server: port: 80 #spring boot actuato ...
- spring cloud网关通过Zuul RateLimit 限流配置
目录 引入依赖 配置信息 RateLimit源码简单分析 RateLimit详细的配置信息解读 在平常项目中为了防止一些没有token访问的API被大量无限的调用,需要对一些服务进行API限流.就好比 ...
随机推荐
- MongoDB查询系统
首先,我们先向集合(collections)中添加测试文档(documents).如下: > for(i=1;i<=5;i++) db.test.insert({x:i,y:i*i,z:6 ...
- Oracle-第一篇一些调优技巧
1.查询 1>通过提示,使用索引. 2>使用/*+parallel*/并行查询 3>查看执行计划,调整sql语句或者优化表结构 4>避免使用“*”号 2.表设计:partiti ...
- python之下载每日必应壁纸
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'jiangwenwen' from bs4 import BeautifulS ...
- C++ 14 新特性总结
转载自: http://www.codeceo.com/article/cpp-14-new-features.html C++14 这一继C++11 之后的新的 C++ 标准已经被正式批准,正在向 ...
- Java学习day9面向对象编程2-方法的可变个数的参数和方法的参数传递
一.方法的可变个数的参数. 问题:我们能够打印的信息都是来源于方法的参数,也就是形参的传递.那如何要给方法传递不同的参数? .1.采用数组形参来定义方法 public static void test ...
- 获取用户真实IP:(模拟:客户端--F5--nginx--tomcat 后端获取用户真实IP)
模拟:客户端--F5--nginx--tomcat 后端获取用户真实IP 192.168.109.137 :nginx01(充当第一层代理==F5)192.168.109.138 :nginx02(二 ...
- 信号量计算问题--n个进程, 共享3个资源, 当前信号量为-1, 其他进程继续执行P操作, 那么信号量应该继续减
选B
- mysql 5.6 datetime default now()
CREATE TABLE `test` ( id int, `gmt_create` datetime DEFAULT NOW() not NULL )ENGINE=InnoDB; mysq ...
- centos 6.5 安装 maven
从nexus官网下载Nexus Repository Manager OSS 2.x的安装包:nexus-2.14.1-01-bundle.tar.gz,3.x版本需要jdk8及以上 解压 tar x ...
- 超低功耗研发-STM32L151C8T6芯片(一)时钟系统概述
前言: 由于之前对STM32Fxx系列相对熟悉,所以涉及到超低功耗设备时,自然就选用STM32家族的STM32Lxx系列产品. STM32L151C8T6 功能特点: (1)Flash:64k (2) ...