首页 技术 正文
技术 2022年11月6日
0 收藏 937 点赞 999 浏览 4169 个字

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

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

1.搭一个简单的网关

idea中file-new-project

spring cloud网关gateway

spring cloud网关gateway

spring cloud网关gateway

spring cloud网关gateway

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);
}}

目录结构

spring cloud网关gateway

2.启动测试

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

spring cloud网关gateway

测试

spring cloud网关gateway

返回

spring cloud网关gateway


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;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,484
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,899
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,732
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,485
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,125
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,286