-java
一、API 服务网关顾名思义就是统一入口,相似 nginx、F5 等功能同样,统一代理控制请求入口,弱化各个微服务被客户端记忆功能; 二、本章节主要讲解了使用 zuul 的代理功能与反向代理功能,固然 zuul 还有不少属性设置,我就没一一列举全部的测试方法了; 三、http://localhost:8150/routes 地址能够查看该zuul微服务网关代理了多少微服务的serviceId;
<?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>
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 #####################################################################################################
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微服务 】】】】】】已启动."); } }
/**************************************************************************************** 1、Zuul 服务 API 网关微服务之代理与反向代理(正常状况测试): 一、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置; 二、启动 springms-discovery-eureka 模块服务,启动1个端口; 三、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是没法测试经过的); 四、启动 springms-consumer-movie 模块服务,启动1个端口; 五、启动 springms-gateway-zuul 模块服务; 六、新起网页页签,输入 http://localhost:7900/simple/3 正常状况下是能看到 ID != 0 一堆用户信息被打印出来; 七、新起网页页签,输入 http://localhost:7901/movie/3 正常状况下是能看到 ID != 0 一堆用户信息被打印出来; 总结一:第六、7步正常,说明 springms-provider-user、springms-consumer-movie 两个服务目前正常; 八、新起网页页签,而后输入 http://localhost:8150/springms-provider-user/simple/3,正常状况下是能看到 ID != 0 一堆用户信息被打印出来; 九、新起网页页签,而后输入 http://localhost:8150/springms-consumer-movie/movie/4,正常状况下是能看到 ID != 0 一堆用户信息被打印出来; 总结二:第八、9步也能正常打印用户信息,说明 API 网关已经生效了,能够经过API服务器地址连接各个微服务的 http://localhost:8150/serviceId/path 这样的路径来访问了; ****************************************************************************************/ /**************************************************************************************** 2、Zuul 服务 API 网关微服务之代理与反向代理(自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务): 一、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置; # 测试二,自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务 zuul: routes: springms-provider-user: /user/** 二、启动 springms-discovery-eureka 模块服务,启动1个端口; 三、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是没法测试经过的); 四、启动 springms-consumer-movie 模块服务,启动1个端口; 五、启动 springms-gateway-zuul 模块服务; 六、新起网页页签,输入 http://localhost:7900/simple/3 正常状况下是能看到 ID != 0 一堆用户信息被打印出来; 七、新起网页页签,输入 http://localhost:7901/movie/3 正常状况下是能看到 ID != 0 一堆用户信息被打印出来; 总结一:第六、7步正常,说明 springms-provider-user、springms-consumer-movie 两个服务目前正常; 八、新起网页页签,而后输入 http://localhost:8150/springms-provider-user/simple/3,正常状况下是能看到 ID != 0 一堆用户信息被打印出来; 九、新起网页页签,而后输入 http://localhost:8150/springms-consumer-movie/movie/4,正常状况下是能看到 ID != 0 一堆用户信息被打印出来; 总结二:第八、9步也能正常打印用户信息,说明 API 网关已经生效了,能够经过API服务器地址连接各个微服务的 http://localhost:8150/serviceId/path 这样的路径来访问了; 十、新起网页页签,而后输入 http://localhost:8150/user/simple/3,正常状况下是能看到 ID != 0 一堆用户信息被打印出来,可见【用户微服务】的地址被改变生效了,同时被 API 网关反向代理了,也就是说 http 的请求 /user 将被发送到【用户微服务】; 十一、新起网页页签,而后输入 http://localhost:8150/user/movie/4,正常状况下访问不通,理应访问不通的; 总结三:zuul.routes 属性仅仅只是为了给 springms-provider-user 微服务添加了 user 前缀,因此电影微服务加 user 前缀固然访问不通的; ****************************************************************************************/ /**************************************************************************************** 3、Zuul 服务 API 网关微服务之代理与反向代理(自定义路径配置,给 springms-provider-user 微服务添加前缀地址,反向代理用户微服务,其它代理路径一概失效): 一、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置; # 测试三,自定义路径配置,给User微服务添加前缀地址,反向代理User微服务,不反向代理电影微服务 zuul: ignoredServices: '*' routes: springms-provider-user: /user/** 二、启动 springms-discovery-eureka 模块服务,启动1个端口; 三、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是没法测试经过的); 四、启动 springms-consumer-movie 模块服务,启动1个端口; 五、启动 springms-gateway-zuul 模块服务; 六、新起网页页签,输入 http://localhost:7900/simple/3 正常状况下是能看到 ID != 0 一堆用户信息被打印出来; 七、新起网页页签,输入 http://localhost:7901/movie/3 正常状况下是能看到 ID != 0 一堆用户信息被打印出来; 总结一:第六、7步正常,说明 springms-provider-user、springms-consumer-movie 两个服务目前正常; 八、新起网页页签,而后输入 http://localhost:8150/springms-provider-user/simple/3,正常状况下不能被代理了,访问页面不存在,出现404错误码; 九、新起网页页签,而后输入 http://localhost:8150/springms-consumer-movie/movie/4,正常状况下不能被代理了,访问页面不存在,出现404错误码; 总结二:第八、9步访问出现404错误码,说明经过 http://localhost:8150/serviceId/path 代理路径访问 API 网关已经失效了; 十、新起网页页签,而后输入 http://localhost:8150/user/simple/3,正常状况下是能看到 ID != 0 一堆用户信息被打印出来,可见【用户微服务】的地址被改变生效了,同时被 API 网关反向代理了,也就是说 http 的请求 /user 将被发送到【用户微服务】; 十一、新起网页页签,而后输入 http://localhost:8150/user/movie/4,正常状况下访问不通,理应访问不通的; 总结三:zuul.routes ignoredServices 忽略禁用了全部代理路径,但仅仅只是为了给 springms-provider-user 微服务添加了 user 前缀供反向代理路径访问,因此电影微服务加 user 前缀固然访问不通的; ****************************************************************************************/ /**************************************************************************************** 4、Zuul 服务 API 网关微服务之代理与反向代理(自定义路径配置,给 springms-provider-user 微服务添加前缀地址,代理、反向代理用户微服务,忽略禁用 springms-consumer-movie 代理、反向代理路径): 一、编写 application.yml 文件,添加应用程序的注解 EnableZuulProxy 配置; # 测试四,自定义路径配置,给 springms-provider-user 微服务添加前缀地址,代理、反向代理用户微服务,忽略禁用 springms-consumer-movie 代理、反向代理路径 zuul: ignoredServices: springms-consumer-movie routes: springms-provider-user: /user/** 二、启动 springms-discovery-eureka 模块服务,启动1个端口; 三、启动 springms-provider-user 模块服务,启动1个端口(application.yml 文件中的 appname 属性不去掉的话,测试一是没法测试经过的); 四、启动 springms-consumer-movie 模块服务,启动1个端口; 五、启动 springms-gateway-zuul 模块服务; 六、新起网页页签,输入 http://localhost:7900/simple/3 正常状况下是能看到 ID != 0 一堆用户信息被打印出来; 七、新起网页页签,输入 http://localhost:7901/movie/3 正常状况下是能看到 ID != 0 一堆用户信息被打印出来; 总结一:第六、7步正常,说明 springms-provider-user、springms-consumer-movie 两个服务目前正常; 八、新起网页页签,而后输入 http://localhost:8150/springms-provider-user/simple/3,正常状况下是能看到 ID != 0 一堆用户信息被打印出来,可见【用户微服务】的地址被改变生效了,同时被 API 网关反向代理了,也就是说 http 的请求 /user 将被发送到【用户微服务】; 九、新起网页页签,而后输入 http://localhost:8150/springms-consumer-movie/movie/4,正常状况下不能被代理了,访问页面不存在,出现404错误码; 总结二:zuul.routes ignoredServices 忽略禁用了 springms-consumer-movie 【电影微服务】的代理路径,因此电影微服务的代理路径固然访问不通的; 十、新起网页页签,而后输入 http://localhost:8150/user/simple/3,正常状况下是能看到 ID != 0 一堆用户信息被打印出来,可见【用户微服务】的地址被改变生效了,同时被 API 网关反向代理了,也就是说 http 的请求 /user 将被发送到【用户微服务】; 十一、新起网页页签,而后输入 http://localhost:8150/user/movie/4,正常状况下访问不通,理应访问不通的; 总结三:zuul.routes ignoredServices 忽略禁用了 springms-consumer-movie 【电影微服务】的代理路径,因此电影微服务的代理路径固然访问不通的; 注意:测试3、测试四的区别在于,ignoredServices 属性的设置,影响的是 springms-consumer-movie 微服务的代理路径是否能够访问; ****************************************************************************************/
https://gitee.com/ylimhhmily/SpringCloudTutorial.gitnginx
SpringCloudTutorial交流QQ群: 235322432git
SpringCloudTutorial交流微信群: 微信沟通群二维码图片连接github
欢迎关注,您的确定是对我最大的支持!!!spring