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

2.新建Module eureka-zuul-client

3.导入依赖

<?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>
<parent>
<groupId>com.hc</groupId>
<artifactId>demoparent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<artifactId>eureka-zuul-client</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入关于 eureka-server的依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!--引入网关 zuul依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!--运行状态监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--mybatis配置 start-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<!--mybatis配置 end-->
</dependencies>
</project>

4.启动类加上注解

@EnableEurekaClient
@EnableZuulProxy

5.增加配置文件application.yml

server:
port: 8767
spring:
main:
allow-bean-definition-overriding: true
datasource:
url: jdbc:mysql://192.168.9.1:3306/test1?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
application:
name: eureka-zuul-client
eureka:
client:
#改变eureka server的检查方式,使用actuator 的health进行检查,可能会检查到数据库
healthcheck:
enabled: true
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
instanceId: ${spring.application.name}:${server.port}
# 每隔10s发送一次心跳
lease-renewal-interval-in-seconds: 10
# 告知服务端30秒还未收到心跳的话,就将该服务移除列表
lease-expiration-duration-in-seconds: 30
#改变eureka 服务端的 status 状态跳转查看页面
status-page-url-path: /actuator/health
management:
endpoints:
web:
exposure:
include: "*"
server:
port: 10116
servlet:
context-path: /
ssl:
enabled: false
endpoint:
health:
show-details: always
zuul:
routes:
zuulHello:
path: /zuulHello/**
serviceId: eureka-client
ribbonHello:
path: /ribbonHello/**
serviceId: eureka-ribbon-client
feignHello:
path: /feignHello/**
serviceId: eureka-feign-client
prefix: /v1

6.增加zuul熔断器

代码:

package com.example.eurekazuulclient.hystrixList;

import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component; import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream; @Component
public class MyFallbackProvider implements FallbackProvider {
@Override
public String getRoute() {
//return "eureka-client";
return "*";
} @Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
} @Override
public int getRawStatusCode() throws IOException {
return 200;
} @Override
public String getStatusText() throws IOException {
return "OK";
} @Override
public void close() { } @Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("fallback".getBytes());
} @Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
}
}//end

7.增加zuul 过滤器

pre

package com.example.eurekazuulclient.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @Component
public class MyFilter_pre extends ZuulFilter {
@Override
public String filterType() {
/**
* pre 代表请求被路由前执行
*/
return "pre";
} @Override
public int filterOrder() {
/**
* 过滤器执行的顺序 越小越靠前
*/
return 0;
} @Override
public boolean shouldFilter() {
/**
* 判断过滤器是否执行
*/
return true;
} @Override
public Object run() throws ZuulException {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
System.out.println("pre send " + request.getMethod() + " Request to " + request.getRequestURL().toString());
//int i = 1 / 0;
String token = request.getParameter("token");
if (StringUtils.isBlank(token)) {
System.out.println("access token is empty");
currentContext.setSendZuulResponse(false);
currentContext.setResponseStatusCode(401);
try {
HttpServletResponse response = currentContext.getResponse();
response.setCharacterEncoding("utf-8"); //设置字符集
response.setContentType("text/html; charset=utf-8"); //设置相应格式
response.getWriter().write("token 验证失败");
} catch (IOException e) {
System.out.println("response io异常");
e.printStackTrace();
}
return null;
}
return null;
}
}//end

post

package com.example.eurekazuulclient.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @Component
public class MyFilter_post extends ZuulFilter {
@Override
public String filterType() {
/**
* post 它是在请求己被路由到微服务后执行的 般情况下,用作收集统计
* 信息、指标,以及将响应传输到客户端
*/
return "post";
} @Override
public int filterOrder() {
/**
* 过滤器执行的顺序 越小越靠前
*/
return 0;
} @Override
public boolean shouldFilter() {
/**
* 判断过滤器是否执行
*/
return true;
} @Override
public Object run() throws ZuulException {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
System.out.println("post send " + request.getMethod() + " Request to " + request.getRequestURL().toString());
return null;
}
}//end

error

package com.example.eurekazuulclient.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException; @Component
public class MyFilter_error extends ZuulFilter {
@Override
public String filterType() {
/**
* error 在其他过滤器发生错误时执行
*/
return "error";
} @Override
public int filterOrder() {
/**
* 过滤器执行的顺序 越小越靠前
*/
return 0;
} @Override
public boolean shouldFilter() {
/**
* 判断过滤器是否执行
*/
return true;
} @Override
public Object run() throws ZuulException {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
System.out.println("error send " + request.getMethod() + " Request to " + request.getRequestURL().toString());
try {
HttpServletResponse response = currentContext.getResponse();
response.setCharacterEncoding("utf-8"); //设置字符集
response.setContentType("text/html; charset=utf-8"); //设置相应格式
response.getWriter().write("error 验证失败");
} catch (IOException e) {
System.out.println("response io异常");
e.printStackTrace();
}
return null;
}
}//end

routing

package com.example.eurekazuulclient.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; @Component
public class MyFilter_routing extends ZuulFilter {
@Override
public String filterType() {
/**
* routing 它用于将请求路由到具体的微服务实例。在默认情况下,它使用
* Http Client 进行网络请求。
*/
return "routing";
} @Override
public int filterOrder() {
/**
* 过滤器执行的顺序 越小越靠前
*/
return 0;
} @Override
public boolean shouldFilter() {
/**
* 判断过滤器是否执行
*/
return true;
} @Override
public Object run() throws ZuulException {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
System.out.println("routing send " + request.getMethod() + " Request to " + request.getRequestURL().toString());
return null;
}
}//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. HashSet如何判定两个元素相同

    在介绍java的集合时,我们提到,set是一个"罐子".我们可以向其中放入各式各样的元素,这些元素没有顺序,但不能相同.其中,HashSet是最常用的一个实现类. 首先,我们看下H ...

  2. 如何在MyEclipse下查看JDK源代码

    在MyEclipse中查看JDK类库的源代码~ 设置: 1.点 "window"-> "Preferences" -> "Java&quo ...

  3. [剑指Offer] 22.从上往下打印二叉树

    [思路]广度优先遍历,队列实现 class Solution { public: vector<int> PrintFromTopToBottom(TreeNode* root) { qu ...

  4. 【Python】Linux crontab定时任务配置方法(详解)

    CRONTAB概念/介绍 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行. cron 系统调度进程. 可以使用它在 ...

  5. Java学习全攻略-->阅读官方文档

    一直感觉Java的官方文档有些杂乱,最近特意整理了一下,仅供参考. 入口 Oracle官方文档入口:http://docs.oracle.com/.下级页面这边只整理了JavaEE跟JavaSE的文档 ...

  6. Expect使用小记

    By francis_hao    May 31,2017   本文翻译了部分Expect的man手册,只选取了个人常用的功能,因此并不完善.   Expect是一个可以和交互式程序对话的程序 概述 ...

  7. 安卓recyclerview的基本使用

    1.先在布局文件中写 <android.support.v7.widget.RecyclerView android:id="@+id/my_recycler_view" a ...

  8. idea 的http client的使用

    1.打开idea的http client的工具步骤如下图所示: 然后在http client 的工具里写:请求头,请求参数,请求体即可.

  9. 播放video

    <html> <head> <title> four in one vedio</title> <style type="text/cs ...

  10. 解决设置了background-size: cover; 但是图片在ios下显示不完整的问题

    设置 background-size: % 99.9%: