SpringCloud(第 018 篇)Zuul 服务 API 网关微服务之代理与反向代理

SpringCloud(第 018 篇)Zuul 服务 API 网关微服务之代理与反向代理

一、大致介绍

11API 服务网关顾名思义就是统一入口,类似 nginx、F5 等功能一样,统一代理控制请求入口,弱化各个微服务被客户端记忆功能; 22、本章节主要讲解了使用 zuul 的代理功能与反向代理功能,当然 zuul 还有很多属性设置,我就没一一列举所有的测试方法了; 33、http://localhost:8150/routes 地址可以查看该zuul微服务网关代理了多少微服务的serviceId;

二、实现步骤

2.1 添加 maven 引用包

1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <artifactId>springms-gateway-zuul</artifactId> 8 <version>1.0-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <parent> 12 <groupId>com.springms.cloud</groupId> 13 <artifactId>springms-spring-cloud</artifactId> 14 <version>1.0-SNAPSHOT</version> 15 </parent> 16 17 <dependencies> 18 <!-- API网关模块 --> 19 <dependency> 20 <groupId>org.springframework.cloud</groupId> 21 <artifactId>spring-cloud-starter-zuul</artifactId> 22 </dependency> 23 24 <!-- 客户端发现模块,由于文档说 Zuul 的依赖里面不包括 eureka 客户端发现模块,所以这里还得单独添加进来 --> 25 <dependency> 26 <groupId>org.springframework.cloud</groupId> 27 <artifactId>spring-cloud-starter-eureka</artifactId> 28 </dependency> 29 </dependencies> 30 31</project>

2.2 添加应用配置文件(springms-gateway-zuul\src\main\resources\application.yml)

1spring: 2 application: 3 name: springms-gateway-zuul 4server: 5 port: 8150 6eureka: 7 datacenter: SpringCloud # 修改 http://localhost:8761 地址 Eureka 首页上面 System StatusData center 显示信息 8 environment: Test # 修改 http://localhost:8761 地址 Eureka 首页上面 System StatusEnvironment 显示信息 9 client: 10 service-url: 11 defaultZone: http://admin:admin@localhost:8761/eureka 12# healthcheck: # 健康检查 13# enabled: true 14 instance: 15 prefer-ip-address: true 16 instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} 17 18 19##################################################################################################### 20# 测试二,自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务 21#zuul: 22# routes: 23# springms-provider-user: /user/** 24 25 26 27## 测试三,自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务,其它代理路径一律失效 28#zuul: 29# ignoredServices: '*' 30# routes: 31# springms-provider-user: /user/** 32 33 34 35# 测试四,自定义路径配置,给 springms-provider-user 微服务添加前缀地址,代理、反向代理用户微服务,忽略禁用 springms-consumer-movie 代理、反向代理路径 36#zuul: 37# ignoredServices: springms-consumer-movie 38# routes: 39# springms-provider-user: /user/** 40##################################################################################################### 41 42##################################################################################################### 43# 打印日志 44logging: 45 level: 46 root: INFO 47 com.springms: DEBUG 48##################################################################################################### 49 50##################################################################################################### 51ribbon: 52 ConnectTimeout: 3000 53 ReadTimeout: 60000 54##################################################################################################### 55 56##################################################################################################### 57# 解决第一次请求报超时异常的方案,因为 hystrix 的默认超时时间是 1 秒,因此请求超过该时间后,就会出现页面超时显示 : 58# 59# 这里就介绍大概三种方式来解决超时的问题,解决方案如下: 60# 61# 第一种方式:将 hystrix 的超时时间设置成 5000 毫秒 62hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000 63# 64# 或者: 65# 第二种方式:将 hystrix 的超时时间直接禁用掉,这样就没有超时的一说了,因为永远也不会超时了 66# hystrix.command.default.execution.timeout.enabled: false 67# 68# 或者: 69# 第三种方式:索性禁用feign的hystrix支持 70# feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持 71 72# 超时的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768 73# 超时的解决方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available 74# hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds 75#####################################################################################################

2.3 添加zuul服务网关微服务启动类(springms-gateway-zuul\src\main\java\com\springms\cloud\MsGatewayZuulApplication.java)

1package com.springms.cloud; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 6 7/** 8 * Zuul 服务 API 网关微服务之代理与反向代理。 9 * 10 * 注意 EnableZuulProxy 注解能注册到 eureka 服务上,是因为该注解包含了 eureka 客户端的注解,该 EnableZuulProxy 是一个复合注解。 11 * 12 * @EnableZuulProxy --> { @EnableCircuitBreaker、@EnableDiscoveryClient } 包含了 eureka 客户端注解,同时也包含了 Hystrix 断路器模块注解。 13 * 14 * http://localhost:8150/routes 地址可以查看该zuul微服务网关代理了多少微服务的serviceId。 15 * 16 * @author hmilyylimh 17 * 18 * @version 0.0.1 19 * 20 * @date 2017/9/24 21 * 22 */ 23@SpringBootApplication 24@EnableZuulProxy 25public class MsGatewayZuulApplication { 26 27 public static void main(String[] args) { 28 SpringApplication.run(MsGatewayZuulApplication.class, args); 29 System.out.println("【【【【【【 GatewayZuul微服务 】】】】】】已启动."); 30 } 31}

三、测试

1/**************************************************************************************** 2 一、Zuul 服务 API 网关微服务之代理与反向代理(正常情况测试): 3 4 1、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置; 5 2、启动 springms-discovery-eureka 模块服务,启动1个端口; 6 3、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是无法测试通过的); 7 4、启动 springms-consumer-movie 模块服务,启动1个端口; 8 5、启动 springms-gateway-zuul 模块服务; 9 10 6、新起网页页签,输入 http://localhost:7900/simple/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来; 11 7、新起网页页签,输入 http://localhost:7901/movie/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来; 12 13 总结一:第6、7步正常,说明 springms-provider-user、springms-consumer-movie 两个服务目前正常; 14 15 8、新起网页页签,然后输入 http://localhost:8150/springms-provider-user/simple/3,正常情况下是能看到 ID != 0 一堆用户信息被打印出来; 16 9、新起网页页签,然后输入 http://localhost:8150/springms-consumer-movie/movie/4,正常情况下是能看到 ID != 0 一堆用户信息被打印出来; 17 18 总结二:第8、9步也能正常打印用户信息,说明 API 网关已经生效了,可以通过API服务器地址链接各个微服务的 http://localhost:8150/serviceId/path 这样的路径来访问了; 19 ****************************************************************************************/ 20 21/**************************************************************************************** 22 二、Zuul 服务 API 网关微服务之代理与反向代理(自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务): 23 24 1、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置; 25 # 测试二,自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务 26 zuul: 27 routes: 28 springms-provider-user: /user/** 29 2、启动 springms-discovery-eureka 模块服务,启动1个端口; 30 3、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是无法测试通过的); 31 4、启动 springms-consumer-movie 模块服务,启动1个端口; 32 5、启动 springms-gateway-zuul 模块服务; 33 34 6、新起网页页签,输入 http://localhost:7900/simple/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来; 35 7、新起网页页签,输入 http://localhost:7901/movie/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来; 36 37 总结一:第6、7步正常,说明 springms-provider-user、springms-consumer-movie 两个服务目前正常; 38 39 8、新起网页页签,然后输入 http://localhost:8150/springms-provider-user/simple/3,正常情况下是能看到 ID != 0 一堆用户信息被打印出来; 40 9、新起网页页签,然后输入 http://localhost:8150/springms-consumer-movie/movie/4,正常情况下是能看到 ID != 0 一堆用户信息被打印出来; 41 42 总结二:第8、9步也能正常打印用户信息,说明 API 网关已经生效了,可以通过API服务器地址链接各个微服务的 http://localhost:8150/serviceId/path 这样的路径来访问了; 43 44 10、新起网页页签,然后输入 http://localhost:8150/user/simple/3,正常情况下是能看到 ID != 0 一堆用户信息被打印出来,可见【用户微服务】的地址被改变生效了,同时被 API 网关反向代理了,也就是说 http 的请求 /user 将被发送到【用户微服务】; 45 11、新起网页页签,然后输入 http://localhost:8150/user/movie/4,正常情况下访问不通,理应访问不通的; 46 47 总结三:zuul.routes 属性仅仅只是为了给 springms-provider-user 微服务添加了 user 前缀,所以电影微服务加 user 前缀当然访问不通的; 48 ****************************************************************************************/ 49 50/**************************************************************************************** 51 三、Zuul 服务 API 网关微服务之代理与反向代理(自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务,其它代理路径一律失效): 52 53 1、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置; 54 # 测试三,自定义路径配置,给User微服务添加前缀地址,反向代理User微服务,不反向代理电影微服务 55 zuul: 56 ignoredServices: '*' 57 routes: 58 springms-provider-user: /user/** 59 2、启动 springms-discovery-eureka 模块服务,启动1个端口; 60 3、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是无法测试通过的); 61 4、启动 springms-consumer-movie 模块服务,启动1个端口; 62 5、启动 springms-gateway-zuul 模块服务; 63 64 6、新起网页页签,输入 http://localhost:7900/simple/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来; 65 7、新起网页页签,输入 http://localhost:7901/movie/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来; 66 67 总结一:第6、7步正常,说明 springms-provider-user、springms-consumer-movie 两个服务目前正常; 68 69 8、新起网页页签,然后输入 http://localhost:8150/springms-provider-user/simple/3,正常情况下不能被代理了,访问页面不存在,出现404错误码; 70 9、新起网页页签,然后输入 http://localhost:8150/springms-consumer-movie/movie/4,正常情况下不能被代理了,访问页面不存在,出现404错误码; 71 72 总结二:第8、9步访问出现404错误码,说明通过 http://localhost:8150/serviceId/path 代理路径访问 API 网关已经失效了; 73 74 10、新起网页页签,然后输入 http://localhost:8150/user/simple/3,正常情况下是能看到 ID != 0 一堆用户信息被打印出来,可见【用户微服务】的地址被改变生效了,同时被 API 网关反向代理了,也就是说 http 的请求 /user 将被发送到【用户微服务】; 75 11、新起网页页签,然后输入 http://localhost:8150/user/movie/4,正常情况下访问不通,理应访问不通的; 76 77 总结三:zuul.routes ignoredServices 忽略禁用了所有代理路径,但仅仅只是为了给 springms-provider-user 微服务添加了 user 前缀供反向代理路径访问,所以电影微服务加 user 前缀当然访问不通的; 78 ****************************************************************************************/ 79 80/**************************************************************************************** 81 四、Zuul 服务 API 网关微服务之代理与反向代理(自定义路径配置,给 springms-provider-user 微服务添加前缀地址,代理、反向代理用户微服务,忽略禁用 springms-consumer-movie 代理、反向代理路径): 82 83 1、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置; 84 # 测试四,自定义路径配置,给 springms-provider-user 微服务添加前缀地址,代理、反向代理用户微服务,忽略禁用 springms-consumer-movie 代理、反向代理路径 85 zuul: 86 ignoredServices: springms-consumer-movie 87 routes: 88 springms-provider-user: /user/** 89 2、启动 springms-discovery-eureka 模块服务,启动1个端口; 90 3、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是无法测试通过的); 91 4、启动 springms-consumer-movie 模块服务,启动1个端口; 92 5、启动 springms-gateway-zuul 模块服务; 93 94 6、新起网页页签,输入 http://localhost:7900/simple/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来; 95 7、新起网页页签,输入 http://localhost:7901/movie/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来; 96 97 总结一:第6、7步正常,说明 springms-provider-user、springms-consumer-movie 两个服务目前正常; 98 99 8、新起网页页签,然后输入 http://localhost:8150/springms-provider-user/simple/3,正常情况下是能看到 ID != 0 一堆用户信息被打印出来,可见【用户微服务】的地址被改变生效了,同时被 API 网关反向代理了,也就是说 http 的请求 /user 将被发送到【用户微服务】; 100 9、新起网页页签,然后输入 http://localhost:8150/springms-consumer-movie/movie/4,正常情况下不能被代理了,访问页面不存在,出现404错误码; 101 102 总结二:zuul.routes ignoredServices 忽略禁用了 springms-consumer-movie 【电影微服务】的代理路径,所以电影微服务的代理路径当然访问不通的; 103 104 10、新起网页页签,然后输入 http://localhost:8150/user/simple/3,正常情况下是能看到 ID != 0 一堆用户信息被打印出来,可见【用户微服务】的地址被改变生效了,同时被 API 网关反向代理了,也就是说 http 的请求 /user 将被发送到【用户微服务】; 105 11、新起网页页签,然后输入 http://localhost:8150/user/movie/4,正常情况下访问不通,理应访问不通的; 106 107 总结三:zuul.routes ignoredServices 忽略禁用了 springms-consumer-movie 【电影微服务】的代理路径,所以电影微服务的代理路径当然访问不通的; 108 109 注意:测试三、测试四的区别在于,ignoredServices 属性的设置,影响的是 springms-consumer-movie 微服务的代理路径是否可以访问; 110 ****************************************************************************************/

四、下载地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接

欢迎关注,您的肯定是对我最大的支持!!!

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )