1.springboot 仅2.0.x 支持,在此选择 2.0.7

2.新建Module eureka-zuul-client

3.导入依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>com.hc</groupId>
  7. <artifactId>demoparent</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <artifactId>eureka-zuul-client</artifactId>
  12. <dependencies>
  13. <dependency>
  14. <groupId>org.springframework.boot</groupId>
  15. <artifactId>spring-boot-starter-web</artifactId>
  16. </dependency>
  17. <!-- 引入关于 eureka-server的依赖 -->
  18. <dependency>
  19. <groupId>org.springframework.cloud</groupId>
  20. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  21. <version>2.0.2.RELEASE</version>
  22. </dependency>
  23. <!--引入网关 zuul依赖-->
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  27. <version>2.0.2.RELEASE</version>
  28. </dependency>
  29. <!--运行状态监控-->
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-actuator</artifactId>
  33. </dependency>
  34. <!--mybatis配置 start-->
  35. <dependency>
  36. <groupId>org.mybatis.spring.boot</groupId>
  37. <artifactId>mybatis-spring-boot-starter</artifactId>
  38. <version>1.2.0</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>mysql</groupId>
  42. <artifactId>mysql-connector-java</artifactId>
  43. <version>5.1.39</version>
  44. </dependency>
  45. <!--mybatis配置 end-->
  46. </dependencies>
  47. </project>

4.启动类加上注解

@EnableEurekaClient
@EnableZuulProxy

5.增加配置文件application.yml

  1. server:
  2. port: 8767
  3. spring:
  4. main:
  5. allow-bean-definition-overriding: true
  6. datasource:
  7. url: jdbc:mysql://192.168.9.1:3306/test1?useUnicode=true&characterEncoding=utf8
  8. username: root
  9. password: 123456
  10. driver-class-name: com.mysql.jdbc.Driver
  11. application:
  12. name: eureka-zuul-client
  13. eureka:
  14. client:
  15. #改变eureka server的检查方式,使用actuator 的health进行检查,可能会检查到数据库
  16. healthcheck:
  17. enabled: true
  18. service-url:
  19. defaultZone: http://localhost:8761/eureka/
  20. instance:
  21. prefer-ip-address: true
  22. instanceId: ${spring.application.name}:${server.port}
  23. # 每隔10s发送一次心跳
  24. lease-renewal-interval-in-seconds: 10
  25. # 告知服务端30秒还未收到心跳的话,就将该服务移除列表
  26. lease-expiration-duration-in-seconds: 30
  27. #改变eureka 服务端的 status 状态跳转查看页面
  28. status-page-url-path: /actuator/health
  29. management:
  30. endpoints:
  31. web:
  32. exposure:
  33. include: "*"
  34. server:
  35. port: 10116
  36. servlet:
  37. context-path: /
  38. ssl:
  39. enabled: false
  40. endpoint:
  41. health:
  42. show-details: always
  43. zuul:
  44. routes:
  45. zuulHello:
  46. path: /zuulHello/**
  47. serviceId: eureka-client
  48. ribbonHello:
  49. path: /ribbonHello/**
  50. serviceId: eureka-ribbon-client
  51. feignHello:
  52. path: /feignHello/**
  53. serviceId: eureka-feign-client
  54. prefix: /v1

6.增加zuul熔断器

代码:

  1. package com.example.eurekazuulclient.hystrixList;
  2.  
  3. import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
  4. import org.springframework.http.HttpHeaders;
  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.http.MediaType;
  7. import org.springframework.http.client.ClientHttpResponse;
  8. import org.springframework.stereotype.Component;
  9.  
  10. import java.io.ByteArrayInputStream;
  11. import java.io.IOException;
  12. import java.io.InputStream;
  13.  
  14. @Component
  15. public class MyFallbackProvider implements FallbackProvider {
  16. @Override
  17. public String getRoute() {
  18. //return "eureka-client";
  19. return "*";
  20. }
  21.  
  22. @Override
  23. public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
  24. return new ClientHttpResponse() {
  25. @Override
  26. public HttpStatus getStatusCode() throws IOException {
  27. return HttpStatus.OK;
  28. }
  29.  
  30. @Override
  31. public int getRawStatusCode() throws IOException {
  32. return 200;
  33. }
  34.  
  35. @Override
  36. public String getStatusText() throws IOException {
  37. return "OK";
  38. }
  39.  
  40. @Override
  41. public void close() {
  42.  
  43. }
  44.  
  45. @Override
  46. public InputStream getBody() throws IOException {
  47. return new ByteArrayInputStream("fallback".getBytes());
  48. }
  49.  
  50. @Override
  51. public HttpHeaders getHeaders() {
  52. HttpHeaders headers = new HttpHeaders();
  53. headers.setContentType(MediaType.APPLICATION_JSON);
  54. return headers;
  55. }
  56. };
  57. }
  58. }//end

7.增加zuul 过滤器

pre

  1. package com.example.eurekazuulclient.filter;
  2.  
  3. import com.netflix.zuul.ZuulFilter;
  4. import com.netflix.zuul.context.RequestContext;
  5. import com.netflix.zuul.exception.ZuulException;
  6. import org.apache.commons.lang.StringUtils;
  7. import org.springframework.stereotype.Component;
  8.  
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import java.io.IOException;
  12.  
  13. @Component
  14. public class MyFilter_pre extends ZuulFilter {
  15. @Override
  16. public String filterType() {
  17. /**
  18. * pre 代表请求被路由前执行
  19. */
  20. return "pre";
  21. }
  22.  
  23. @Override
  24. public int filterOrder() {
  25. /**
  26. * 过滤器执行的顺序 越小越靠前
  27. */
  28. return 0;
  29. }
  30.  
  31. @Override
  32. public boolean shouldFilter() {
  33. /**
  34. * 判断过滤器是否执行
  35. */
  36. return true;
  37. }
  38.  
  39. @Override
  40. public Object run() throws ZuulException {
  41. RequestContext currentContext = RequestContext.getCurrentContext();
  42. HttpServletRequest request = currentContext.getRequest();
  43. System.out.println("pre send " + request.getMethod() + " Request to " + request.getRequestURL().toString());
  44. //int i = 1 / 0;
  45. String token = request.getParameter("token");
  46. if (StringUtils.isBlank(token)) {
  47. System.out.println("access token is empty");
  48. currentContext.setSendZuulResponse(false);
  49. currentContext.setResponseStatusCode(401);
  50. try {
  51. HttpServletResponse response = currentContext.getResponse();
  52. response.setCharacterEncoding("utf-8"); //设置字符集
  53. response.setContentType("text/html; charset=utf-8"); //设置相应格式
  54. response.getWriter().write("token 验证失败");
  55. } catch (IOException e) {
  56. System.out.println("response io异常");
  57. e.printStackTrace();
  58. }
  59. return null;
  60. }
  61. return null;
  62. }
  63. }//end

post

  1. package com.example.eurekazuulclient.filter;
  2.  
  3. import com.netflix.zuul.ZuulFilter;
  4. import com.netflix.zuul.context.RequestContext;
  5. import com.netflix.zuul.exception.ZuulException;
  6. import org.springframework.stereotype.Component;
  7.  
  8. import javax.servlet.http.HttpServletRequest;
  9.  
  10. @Component
  11. public class MyFilter_post extends ZuulFilter {
  12. @Override
  13. public String filterType() {
  14. /**
  15. * post 它是在请求己被路由到微服务后执行的 般情况下,用作收集统计
  16. * 信息、指标,以及将响应传输到客户端
  17. */
  18. return "post";
  19. }
  20.  
  21. @Override
  22. public int filterOrder() {
  23. /**
  24. * 过滤器执行的顺序 越小越靠前
  25. */
  26. return 0;
  27. }
  28.  
  29. @Override
  30. public boolean shouldFilter() {
  31. /**
  32. * 判断过滤器是否执行
  33. */
  34. return true;
  35. }
  36.  
  37. @Override
  38. public Object run() throws ZuulException {
  39. RequestContext currentContext = RequestContext.getCurrentContext();
  40. HttpServletRequest request = currentContext.getRequest();
  41. System.out.println("post send " + request.getMethod() + " Request to " + request.getRequestURL().toString());
  42. return null;
  43. }
  44. }//end

error

  1. package com.example.eurekazuulclient.filter;
  2.  
  3. import com.netflix.zuul.ZuulFilter;
  4. import com.netflix.zuul.context.RequestContext;
  5. import com.netflix.zuul.exception.ZuulException;
  6. import org.springframework.stereotype.Component;
  7.  
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11.  
  12. @Component
  13. public class MyFilter_error extends ZuulFilter {
  14. @Override
  15. public String filterType() {
  16. /**
  17. * error 在其他过滤器发生错误时执行
  18. */
  19. return "error";
  20. }
  21.  
  22. @Override
  23. public int filterOrder() {
  24. /**
  25. * 过滤器执行的顺序 越小越靠前
  26. */
  27. return 0;
  28. }
  29.  
  30. @Override
  31. public boolean shouldFilter() {
  32. /**
  33. * 判断过滤器是否执行
  34. */
  35. return true;
  36. }
  37.  
  38. @Override
  39. public Object run() throws ZuulException {
  40. RequestContext currentContext = RequestContext.getCurrentContext();
  41. HttpServletRequest request = currentContext.getRequest();
  42. System.out.println("error send " + request.getMethod() + " Request to " + request.getRequestURL().toString());
  43. try {
  44. HttpServletResponse response = currentContext.getResponse();
  45. response.setCharacterEncoding("utf-8"); //设置字符集
  46. response.setContentType("text/html; charset=utf-8"); //设置相应格式
  47. response.getWriter().write("error 验证失败");
  48. } catch (IOException e) {
  49. System.out.println("response io异常");
  50. e.printStackTrace();
  51. }
  52. return null;
  53. }
  54. }//end

routing

  1. package com.example.eurekazuulclient.filter;
  2.  
  3. import com.netflix.zuul.ZuulFilter;
  4. import com.netflix.zuul.context.RequestContext;
  5. import com.netflix.zuul.exception.ZuulException;
  6. import org.springframework.stereotype.Component;
  7.  
  8. import javax.servlet.http.HttpServletRequest;
  9.  
  10. @Component
  11. public class MyFilter_routing extends ZuulFilter {
  12. @Override
  13. public String filterType() {
  14. /**
  15. * routing 它用于将请求路由到具体的微服务实例。在默认情况下,它使用
  16. * Http Client 进行网络请求。
  17. */
  18. return "routing";
  19. }
  20.  
  21. @Override
  22. public int filterOrder() {
  23. /**
  24. * 过滤器执行的顺序 越小越靠前
  25. */
  26. return 0;
  27. }
  28.  
  29. @Override
  30. public boolean shouldFilter() {
  31. /**
  32. * 判断过滤器是否执行
  33. */
  34. return true;
  35. }
  36.  
  37. @Override
  38. public Object run() throws ZuulException {
  39. RequestContext currentContext = RequestContext.getCurrentContext();
  40. HttpServletRequest request = currentContext.getRequest();
  41. System.out.println("routing send " + request.getMethod() + " Request to " + request.getRequestURL().toString());
  42. return null;
  43. }
  44. }//end

7.依次启动,eureka-server  eureka-client-01  eureka-client  eureka-feign-client  eureka-ribbon-client  eureka-zuul-client

访问 http://192.168.9.6:8767/v1/zuulHello/hello?token=123 显示

访问 http://192.168.9.6:8767/v1/ribbonHello/hello?token=123 显示

访问 http://192.168.9.6:8767/v1/feignHello/hello 显示

springCloud Zuul网关的更多相关文章

  1. SpringCloud Zuul网关的简单理解

    Zuul网关功能 请求路由.服务路由.请求过滤 请求路由 参数配置如下所示,所有能够配置path规则的请求,都会被zuul网关转发到对应的url上. zuul.routes.user-service. ...

  2. spring-cloud zuul网关

    API Gateway 是随着微服务(Microservice)这个概念一起兴起的一种架构模式,它用于解决微服务过于分散,没有一个统一的出入口进行流量管理的问题. 使用 Zuul 实现 API Gat ...

  3. SpringCloud Zuul网关超时

    最近在使用SpringCloudZuul网关时,报错"NUMBEROF_RETRIES_NEXTSERVER_EXCEEDED", 查询资料后,发现: ribbon.Connect ...

  4. SpringCloud zuul 网关限流分析

    最近项目中 spring cloud zuul 运用到限流功能,打算配置一下就直接使用,不过在压测与调优过程中遇到一些没有预测到的问题,附上排查与解析结果 yml.pom配置 强烈推荐,按最新gith ...

  5. springCloud zuul网关服务

    第一步:编写application.properties文件 spring.application.name=api-gateway server.port=5555 zuul.routes.user ...

  6. SpringCloud(7)---网关概念、Zuul项目搭建

    SpringCloud(7)---网关概念.Zuul项目搭建 一.网关概念 1.什么是路由网关 网关是系统的唯一对外的入口,介于客户端和服务器端之间的中间层,处理非业务功能 提供路由请求.鉴权.监控. ...

  7. springcloud 实战 网关zuul使用中遇到的相关问题

    springcloud 实战  网关zuul使用中遇到的相关问题 1.网关zuul使用时,跨域问题在网关中配置pre过滤器: response.setHeader("Access-Contr ...

  8. springcloud(七,多个服务消费者配置,以及zuul网关案例)

    spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) spring cloud (三.服务提供者demo_provid ...

  9. SpringCloud之Zuul网关原理及其配置

    Zuul是spring cloud中的微服务网关.网关: 是一个网络整体系统中的前置门户入口.请求首先通过网关,进行路径的路由,定位到具体的服务节点上. Zuul是一个微服务网关,首先是一个微服务.也 ...

随机推荐

  1. LTE 中基于X2的切换

    LTE 中基于X2的切换 (36.300, 23.401)SGW  保持不变 http://blog.sina.com.cn/s/blog_673b30dd0100j4pe.html   1:eNod ...

  2. HDU 2175 汉诺塔IX

    http://acm.hdu.edu.cn/showproblem.php?pid=2175 Problem Description 1,2,...,n表示n个盘子.数字大盘子就大.n个盘子放在第1根 ...

  3. 简述jq中attr()和prop()的区别

    attr,prop都是属性的意思,那他们有什么区别呢?我们先来看一下jquery的部分源码: attr部分: attr: function( elem, name, value, pass ) { v ...

  4. Java中的输入输出流

    FileInputStream和FileOutputStream 创建含磁盘文件的输入 输出流对象. FileInputStream继承自InputStream,用于读取本地文件中的字节数据,由于所有 ...

  5. group by 分组后 返回的是一个同属性的集合

    group by 分组后 返回的是一个同属性的集合  我们可以遍历该集合

  6. [BZOJ4212]神牛的养成计划

    [BZOJ4212]神牛的养成计划 试题描述 Hzwer 成功培育出神牛细胞,可最终培育出的生物体却让他大失所望...... 后来,他从某同校女神 牛处知道,原来他培育的细胞发生了基因突变,原先决定神 ...

  7. day10_plus

    刚才发奖哈哈哈 想不到被惨虐的我还能混个牌子哈哈哈好开心

  8. 在xml文件中引入带有@Configuration的Java类

    在xml引入带有@Configuration的Java类,就是把这个带有@Configuration的Java类,当做一个普通的的类用<bean>标签引入: 核心代码如下: @Config ...

  9. centos安装net-speeder

    以前介绍过VPS上安装锐速对VPS的加速效果,但是这货对 Linux 内核有要求,一般就只能在XEN或者KVM的机子上安装.不过还好锐速有一个免费的代替品:net-speeder,所以这里介绍一下 D ...

  10. API教程

    www.yuanjiaocheng.net http://www.yuanjiaocheng.net/webapi/test-webapi.html