SpringBoot中使用Zuul
Zuul提供了服务网关的功能,可以实现负载均衡、反向代理、动态路由、请求转发等功能。
Zuul大部分功能是通过过滤器实现的,除了标准的四种过滤器类型,还支持自定义过滤器。
使用@EnableZuulProxy注解,Spring容器初始化时,会将Zuul的相关配置初始化,其中包含一个Spring Boot的Bean:ServletRegistrationBean,该类主要用于注册Servlet。在Servlet的service方法中,执行各种Zuul过滤器。下图为HTTP请求在ZuulServlet中的生命周期。
Spring Boot Web项目中整合Zuul:
一、创建hello源服务项目
1、创建项目
开发工具:IntelliJ IDEA 2019.2.3
IDEA中创建一个新的SpringBoot项目,名称为“hello-server”,SpringBoot版本选择2.1.10,在选择Dependencies(依赖)的界面勾选Web -> Spring Web。
2、修改启动类代码
添加一个hello服务
- package com.example.helloserver;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RestController;
- @SpringBootApplication
- @RestController
- public class HelloServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(HelloServerApplication.class, args);
- }
- @GetMapping("/hello/{name}")
- public String hello(@PathVariable String name){
- return "hello " + name;
- }
- }
3、修改配置application.yml,指定端口号8090
- server:
- port: 8090
二、测试路由功能
1、创建项目
IDEA中创建一个新的SpringBoot项目,名称为“zuul-router”,SpringBoot版本选择2.1.10,在选择Dependencies(依赖)的界面勾选Web -> Spring Web,Spring Cloud Routing -> Zuul。
主要添加了spring-boot-starter-web和spring-cloud-starter-netflix-zuul两个依赖项。
pom.xml完整内容如下:
- <?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 https://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.10.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example</groupId>
- <artifactId>zuul-router</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>zuul-router</name>
- <description>Demo project for Spring Boot</description>
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.SR4</spring-cloud.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </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>
2、修改启动类代码
增加注解@EnableZuulProxy
- package com.example.zuulrouter;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
- @SpringBootApplication
- @EnableZuulProxy
- public class ZuulRouterApplication {
- public static void main(String[] args) {
- SpringApplication.run(ZuulRouterApplication.class, args);
- }
- }
3、修改配置application.yml
- zuul:
- routes:
- test:
- url: http://localhost:8090
加入以上配置后,发送给http://localhost:8080/test的所有请求会被转发到8090端口。
在浏览器访问http://localhost:8080/test/hello/lc,页面输出:hello lc
上面路由配置省略了path,默认情况下用routeId“test”作为path。
修改为:
- zuul:
- routes:
- test:
- path: /a/**
- url: http://localhost:8090
- b:
- url: https://www.cnblogs.com/gdjlc
现在浏览器访问http://localhost:8080/a/hello/lc,页面输出:hello lc
访问http://localhost:8080/b,页面显示https://www.cnblogs.com/gdjlc的内容
SpringBoot中使用Zuul的更多相关文章
- SpringBoot中yaml配置对象
转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...
- 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧
做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...
- springboot中swaggerUI的使用
demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...
- 由浅入深学习springboot中使用redis
很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...
- Springboot中使用AOP统一处理Web请求日志
title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...
- SpringBoot 中常用注解
本篇博文将介绍几种SpringBoot 中常用注解 其中,各注解的作用为: @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注 ...
- SpringBoot中关于Mybatis使用的三个问题
SpringBoot中关于Mybatis使用的三个问题 转载请注明源地址:http://www.cnblogs.com/funnyzpc/p/8495453.html 原本是要讲讲PostgreSQL ...
- 在SpringBoot中配置aop
前言 aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现.这里就以打印日志作为例子,在SpringBoot中配置aop 已经加入我 ...
随机推荐
- Android多图选择
多图选择是Android中一个常用的功能,用户可以拍照或者批量选择图片上传,还是国际惯例,先看下效果图,demo地址我会放到文章末尾. 经过对比,这里我选择了一个第三方开源库PictureSelect ...
- Unity3d 与 Objective-C 数据交互,持续更新中
Unity 3D是用于3D游戏编程的语言,它是一个用C\C++编写的强大的库.而在游戏中经常要接入用OC编写的SDK,这就涉及到了Unity3d 和 OC之间的数据交互.XCode是完成兼容C语言的. ...
- [TimLinux] TCP全连接队列满
0. TCP三次握手 该图来自:TCP SOCKET中backlog参数的用途是什么? syns queue: 半连接队列 accept queue: 全连接队列 控制参数存放在文件:/proc/sy ...
- cmd 窗口中运行 Java 程序
1.CMD 命令提示符(Command Processor)(CMD) CMD命令:开始->运行->键入 cmd(在命令行里可以看到系统版本.文件系统版本) 2.对文件夹操作的部分命令 启 ...
- Python3 常用的几个内置方法
目录 max()/min() filter() 过滤 map() 映射 sorted筛选 reduce()减少 max()/min() 传入一个参数 (可迭代对象), 返回这个可迭代对象中最大的元素 ...
- 使用FileReader在浏览器读取预览文件(image和txt)
如标题,之前在某个地方看到因为有Blob的存在,理论上可以在浏览器上查看所有格式的文件.自己想着试试现在暂时只能够查看图片和预览txt文件.其他的比如doc,docx格式的文件查看的时候是乱码 如图: ...
- python学习-class封装
# 封装 类=属性+行为 抽像 -class StudentV2: # 类属性 所有的实例可以共享 .不属于任何实例的特性. is_people = True # 类方法 1.装饰器.2.参数是cls ...
- 分享一个mysql服务启动与关闭的bat文件
有时候打开数据库可视化工具(sqlyog.navicat)连接数据库时,会出现以下报错信息. 大家都知道是数据库的服务没有启动. 所以我想给大家分享一个bat文件可供快速启动mysql的数据库的服务, ...
- HPS端如何通过AXI Bridge控制FPGA端口的GPIO
该笔记主要记录HPS端如何通过AXI Bridge控制FPGA端口的GPIO,主要是如何操作FPGA侧的Led 1.AXI Bridge AXIB主要包括H2FB.F2HB.LWH2F ...
- Linux Bash之通配符
通配符是我们在shell环境中不知不觉中都会用到的,有时甚至都不会考虑到去探究其实现过程,因为使用得太普遍了.而清晰地理解每一个过程,将有助于我们的分析和调试. 说白了,通配符就是在 shell 环境 ...