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. Java课程设计--学生成绩管理系统

    一.团队名称: 团队成员 林艺薇 201721123032 网络1712 黄毓颖 201721123033 网络1712 唐川 201721123034 网络1712 梁才玉 201721123038 ...

  2. 详细解析@Resource和@Autowired的区别 , 以及@Qualifier的作用

    (41)  (0) 首先 . @Resource是javax.annotation 包中的注解类 , 是jdk中封装的 . @AutoWired是spring的中注解,依赖于spring上下文. 相同 ...

  3. [Leetcode] count and say 计数和说

    The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...

  4. ldconfig用法小记

    By francis_hao    Aug 4,2017   ldconfig:配置运行时动态链接库 概述 /sbin/ldconfig [ -nNvXV ] [ -f conf ] [ -C cac ...

  5. Web自适应

    随着移动设备的普及,移动web在前端工程师们的工作中占有越来越重要的位置.移动设备更新速度频繁,手机厂商繁多,导致的问题是每一台机器的屏幕宽度和分辨率不一样.这给我们在编写前端界面时增加了困难,适配问 ...

  6. jquery学习之事件委派

    一.定义 事件委派的定义就是,把原来加给子元素身上的事件绑定在父元素身上,就是把事件委派给父元素. 二.版本 从jQuery1.7开始,jQuery引入了全新的事件绑定机制,on()和off()两个函 ...

  7. [洛谷P2124] 奶牛美容

    洛谷题目链接:奶牛美容 题目描述 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 6 16 ................ ..XXXX....XXX... ...XXXX... ...

  8. Java并发编程--ThreadPoolExecutor

    概述 为什么要使用线程池? 合理利用线程池能够带来三个好处.第一:降低资源消耗.通过重复利用已创建的线程降低线程创建和销毁造成的消耗.第二:提高响应速度.当任务到达时,任务可以不需要等到线程创建就能立 ...

  9. request.getParameterValues与request.getParameter的区别

    一. 简单的对比 request.getParameter用的比较多,相对熟悉 request.getParameterValues(String   name)是获得如checkbox类(名字相同, ...

  10. 【Foreign】置换 [数论][置换]

    置换 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 4 2 1 4 3 Sample O ...