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

Easter79
• 阅读 500

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

一、大致介绍

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

二、实现步骤

2.1 添加 maven 引用包

<?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>

    <artifactId>springms-gateway-zuul</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>com.springms.cloud</groupId>
        <artifactId>springms-spring-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <dependencies>
        <!-- API网关模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>

        <!-- 客户端发现模块,由于文档说 Zuul 的依赖里面不包括 eureka 客户端发现模块,所以这里还得单独添加进来 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
    </dependencies>

</project>

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

spring:
  application:
    name: springms-gateway-zuul
server:
  port: 8150
eureka:
  datacenter: SpringCloud   # 修改 http://localhost:8761 地址 Eureka 首页上面 System Status 的 Data center 显示信息
  environment: Test         # 修改 http://localhost:8761 地址 Eureka 首页上面 System Status 的 Environment 显示信息
  client:
    service-url:
      defaultZone: http://admin:admin@localhost:8761/eureka
#    healthcheck:  # 健康检查
#      enabled: true
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}


#####################################################################################################
# 测试二,自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务
#zuul:
#  routes:
#    springms-provider-user: /user/**



## 测试三,自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务,其它代理路径一律失效
#zuul:
#  ignoredServices: '*'
#  routes:
#    springms-provider-user: /user/**



# 测试四,自定义路径配置,给 springms-provider-user 微服务添加前缀地址,代理、反向代理用户微服务,忽略禁用 springms-consumer-movie 代理、反向代理路径
#zuul:
#  ignoredServices: springms-consumer-movie
#  routes:
#    springms-provider-user: /user/**
#####################################################################################################

#####################################################################################################
# 打印日志
logging:
  level:
    root: INFO
    com.springms: DEBUG
#####################################################################################################

#####################################################################################################
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000
#####################################################################################################

#####################################################################################################
# 解决第一次请求报超时异常的方案,因为 hystrix 的默认超时时间是 1 秒,因此请求超过该时间后,就会出现页面超时显示 :
#
# 这里就介绍大概三种方式来解决超时的问题,解决方案如下:
#
# 第一种方式:将 hystrix 的超时时间设置成 5000 毫秒
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
#
# 或者:
# 第二种方式:将 hystrix 的超时时间直接禁用掉,这样就没有超时的一说了,因为永远也不会超时了
# hystrix.command.default.execution.timeout.enabled: false
#
# 或者:
# 第三种方式:索性禁用feign的hystrix支持
# feign.hystrix.enabled: false ## 索性禁用feign的hystrix支持

# 超时的issue:https://github.com/spring-cloud/spring-cloud-netflix/issues/768
# 超时的解决方案: http://stackoverflow.com/questions/27375557/hystrix-command-fails-with-timed-out-and-no-fallback-available
# hystrix配置: https://github.com/Netflix/Hystrix/wiki/Configuration#execution.isolation.thread.timeoutInMilliseconds
#####################################################################################################

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

package com.springms.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * Zuul 服务 API 网关微服务之代理与反向代理。
 *
 * 注意 EnableZuulProxy 注解能注册到 eureka 服务上,是因为该注解包含了 eureka 客户端的注解,该 EnableZuulProxy 是一个复合注解。
 *
 * @EnableZuulProxy --> { @EnableCircuitBreaker、@EnableDiscoveryClient } 包含了 eureka 客户端注解,同时也包含了 Hystrix 断路器模块注解。
 *
 * http://localhost:8150/routes 地址可以查看该zuul微服务网关代理了多少微服务的serviceId。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/24
 *
 */
@SpringBootApplication
@EnableZuulProxy
public class MsGatewayZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsGatewayZuulApplication.class, args);
        System.out.println("【【【【【【 GatewayZuul微服务 】】】】】】已启动.");
    }
}

三、测试

/****************************************************************************************
 一、Zuul 服务 API 网关微服务之代理与反向代理(正常情况测试):

 1、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置;
 2、启动 springms-discovery-eureka 模块服务,启动1个端口;
 3、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是无法测试通过的);
 4、启动 springms-consumer-movie 模块服务,启动1个端口;
 5、启动 springms-gateway-zuul 模块服务;

 6、新起网页页签,输入 http://localhost:7900/simple/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来;
 7、新起网页页签,输入 http://localhost:7901/movie/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来;

 总结一:第6、7步正常,说明 springms-provider-user、springms-consumer-movie 两个服务目前正常;

 8、新起网页页签,然后输入 http://localhost:8150/springms-provider-user/simple/3,正常情况下是能看到 ID != 0 一堆用户信息被打印出来;
 9、新起网页页签,然后输入 http://localhost:8150/springms-consumer-movie/movie/4,正常情况下是能看到 ID != 0 一堆用户信息被打印出来;

 总结二:第8、9步也能正常打印用户信息,说明 API 网关已经生效了,可以通过API服务器地址链接各个微服务的 http://localhost:8150/serviceId/path 这样的路径来访问了;
 ****************************************************************************************/

/****************************************************************************************
 二、Zuul 服务 API 网关微服务之代理与反向代理(自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务):

 1、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置;
     # 测试二,自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务
     zuul:
        routes:
            springms-provider-user: /user/**
 2、启动 springms-discovery-eureka 模块服务,启动1个端口;
 3、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是无法测试通过的);
 4、启动 springms-consumer-movie 模块服务,启动1个端口;
 5、启动 springms-gateway-zuul 模块服务;

 6、新起网页页签,输入 http://localhost:7900/simple/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来;
 7、新起网页页签,输入 http://localhost:7901/movie/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来;

 总结一:第6、7步正常,说明 springms-provider-user、springms-consumer-movie 两个服务目前正常;

 8、新起网页页签,然后输入 http://localhost:8150/springms-provider-user/simple/3,正常情况下是能看到 ID != 0 一堆用户信息被打印出来;
 9、新起网页页签,然后输入 http://localhost:8150/springms-consumer-movie/movie/4,正常情况下是能看到 ID != 0 一堆用户信息被打印出来;

 总结二:第8、9步也能正常打印用户信息,说明 API 网关已经生效了,可以通过API服务器地址链接各个微服务的 http://localhost:8150/serviceId/path 这样的路径来访问了;

 10、新起网页页签,然后输入 http://localhost:8150/user/simple/3,正常情况下是能看到 ID != 0 一堆用户信息被打印出来,可见【用户微服务】的地址被改变生效了,同时被 API 网关反向代理了,也就是说 http 的请求 /user 将被发送到【用户微服务】;
 11、新起网页页签,然后输入 http://localhost:8150/user/movie/4,正常情况下访问不通,理应访问不通的;

 总结三:zuul.routes 属性仅仅只是为了给 springms-provider-user 微服务添加了 user 前缀,所以电影微服务加 user 前缀当然访问不通的;
 ****************************************************************************************/

/****************************************************************************************
 三、Zuul 服务 API 网关微服务之代理与反向代理(自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务,其它代理路径一律失效):

 1、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置;
     # 测试三,自定义路径配置,给User微服务添加前缀地址,反向代理User微服务,不反向代理电影微服务
     zuul:
        ignoredServices: '*'
        routes:
            springms-provider-user: /user/**
 2、启动 springms-discovery-eureka 模块服务,启动1个端口;
 3、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是无法测试通过的);
 4、启动 springms-consumer-movie 模块服务,启动1个端口;
 5、启动 springms-gateway-zuul 模块服务;

 6、新起网页页签,输入 http://localhost:7900/simple/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来;
 7、新起网页页签,输入 http://localhost:7901/movie/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来;

 总结一:第6、7步正常,说明 springms-provider-user、springms-consumer-movie 两个服务目前正常;

 8、新起网页页签,然后输入 http://localhost:8150/springms-provider-user/simple/3,正常情况下不能被代理了,访问页面不存在,出现404错误码;
 9、新起网页页签,然后输入 http://localhost:8150/springms-consumer-movie/movie/4,正常情况下不能被代理了,访问页面不存在,出现404错误码;

 总结二:第8、9步访问出现404错误码,说明通过 http://localhost:8150/serviceId/path 代理路径访问 API 网关已经失效了;

 10、新起网页页签,然后输入 http://localhost:8150/user/simple/3,正常情况下是能看到 ID != 0 一堆用户信息被打印出来,可见【用户微服务】的地址被改变生效了,同时被 API 网关反向代理了,也就是说 http 的请求 /user 将被发送到【用户微服务】;
 11、新起网页页签,然后输入 http://localhost:8150/user/movie/4,正常情况下访问不通,理应访问不通的;

 总结三:zuul.routes ignoredServices 忽略禁用了所有代理路径,但仅仅只是为了给 springms-provider-user 微服务添加了 user 前缀供反向代理路径访问,所以电影微服务加 user 前缀当然访问不通的;
 ****************************************************************************************/

/****************************************************************************************
 四、Zuul 服务 API 网关微服务之代理与反向代理(自定义路径配置,给 springms-provider-user 微服务添加前缀地址,代理、反向代理用户微服务,忽略禁用 springms-consumer-movie 代理、反向代理路径):

 1、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置;
     # 测试四,自定义路径配置,给 springms-provider-user 微服务添加前缀地址,代理、反向代理用户微服务,忽略禁用 springms-consumer-movie 代理、反向代理路径
     zuul:
         ignoredServices: springms-consumer-movie
         routes:
            springms-provider-user: /user/**
 2、启动 springms-discovery-eureka 模块服务,启动1个端口;
 3、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是无法测试通过的);
 4、启动 springms-consumer-movie 模块服务,启动1个端口;
 5、启动 springms-gateway-zuul 模块服务;

 6、新起网页页签,输入 http://localhost:7900/simple/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来;
 7、新起网页页签,输入 http://localhost:7901/movie/3 正常情况下是能看到 ID != 0 一堆用户信息被打印出来;

 总结一:第6、7步正常,说明 springms-provider-user、springms-consumer-movie 两个服务目前正常;

 8、新起网页页签,然后输入 http://localhost:8150/springms-provider-user/simple/3,正常情况下是能看到 ID != 0 一堆用户信息被打印出来,可见【用户微服务】的地址被改变生效了,同时被 API 网关反向代理了,也就是说 http 的请求 /user 将被发送到【用户微服务】;
 9、新起网页页签,然后输入 http://localhost:8150/springms-consumer-movie/movie/4,正常情况下不能被代理了,访问页面不存在,出现404错误码;

 总结二:zuul.routes ignoredServices 忽略禁用了 springms-consumer-movie 【电影微服务】的代理路径,所以电影微服务的代理路径当然访问不通的;

 10、新起网页页签,然后输入 http://localhost:8150/user/simple/3,正常情况下是能看到 ID != 0 一堆用户信息被打印出来,可见【用户微服务】的地址被改变生效了,同时被 API 网关反向代理了,也就是说 http 的请求 /user 将被发送到【用户微服务】;
 11、新起网页页签,然后输入 http://localhost:8150/user/movie/4,正常情况下访问不通,理应访问不通的;

 总结三:zuul.routes ignoredServices 忽略禁用了 springms-consumer-movie 【电影微服务】的代理路径,所以电影微服务的代理路径当然访问不通的;

 注意:测试三、测试四的区别在于,ignoredServices 属性的设置,影响的是 springms-consumer-movie 微服务的代理路径是否可以访问;
 ****************************************************************************************/

四、下载地址

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

SpringCloudTutorial交流QQ群: 235322432

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

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

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Easter79 Easter79
2年前
swap空间的增减方法
(1)增大swap空间去激活swap交换区:swapoff v /dev/vg00/lvswap扩展交换lv:lvextend L 10G /dev/vg00/lvswap重新生成swap交换区:mkswap /dev/vg00/lvswap激活新生成的交换区:swapon v /dev/vg00/lvswap
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
Java获得今日零时零分零秒的时间(Date型)
publicDatezeroTime()throwsParseException{    DatetimenewDate();    SimpleDateFormatsimpnewSimpleDateFormat("yyyyMMdd00:00:00");    SimpleDateFormatsimp2newS
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k