springCloud Zuul网关
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网关的更多相关文章
- SpringCloud Zuul网关的简单理解
Zuul网关功能 请求路由.服务路由.请求过滤 请求路由 参数配置如下所示,所有能够配置path规则的请求,都会被zuul网关转发到对应的url上. zuul.routes.user-service. ...
- spring-cloud zuul网关
API Gateway 是随着微服务(Microservice)这个概念一起兴起的一种架构模式,它用于解决微服务过于分散,没有一个统一的出入口进行流量管理的问题. 使用 Zuul 实现 API Gat ...
- SpringCloud Zuul网关超时
最近在使用SpringCloudZuul网关时,报错"NUMBEROF_RETRIES_NEXTSERVER_EXCEEDED", 查询资料后,发现: ribbon.Connect ...
- SpringCloud zuul 网关限流分析
最近项目中 spring cloud zuul 运用到限流功能,打算配置一下就直接使用,不过在压测与调优过程中遇到一些没有预测到的问题,附上排查与解析结果 yml.pom配置 强烈推荐,按最新gith ...
- springCloud zuul网关服务
第一步:编写application.properties文件 spring.application.name=api-gateway server.port=5555 zuul.routes.user ...
- SpringCloud(7)---网关概念、Zuul项目搭建
SpringCloud(7)---网关概念.Zuul项目搭建 一.网关概念 1.什么是路由网关 网关是系统的唯一对外的入口,介于客户端和服务器端之间的中间层,处理非业务功能 提供路由请求.鉴权.监控. ...
- springcloud 实战 网关zuul使用中遇到的相关问题
springcloud 实战 网关zuul使用中遇到的相关问题 1.网关zuul使用时,跨域问题在网关中配置pre过滤器: response.setHeader("Access-Contr ...
- springcloud(七,多个服务消费者配置,以及zuul网关案例)
spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) spring cloud (三.服务提供者demo_provid ...
- SpringCloud之Zuul网关原理及其配置
Zuul是spring cloud中的微服务网关.网关: 是一个网络整体系统中的前置门户入口.请求首先通过网关,进行路径的路由,定位到具体的服务节点上. Zuul是一个微服务网关,首先是一个微服务.也 ...
随机推荐
- STL中list的erase()方法
http://www.cnblogs.com/gshlsh17/ rase()方法是删除iterator指定的节点 但是要注意的是在执行完此函数的时候iterator也被销毁了 这样的话关于it ...
- PAT 甲级 1036 Boys vs Girls(20)
https://pintia.cn/problem-sets/994805342720868352/problems/994805453203030016 This time you are aske ...
- Visual Source Safe的使用方法
VSS 的全称为 Visual Source Safe .作为 Microsoft Visual Studio 的一名成员,它主要任务就是负责项目文件的管理,几乎可以适用任何软件项目.管理软件开发中各 ...
- hdu 1053 Entropy (哈夫曼树)
Entropy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 计蒜客16495 Truefriend(fwt)
#include <iostream> #include <cstring> #include <cstdio> using namespace std; type ...
- 洛谷 P2218 [HAOI2007]覆盖问题 解题报告
P2218 [HAOI2007]覆盖问题 题目描述 某人在山上种了\(N\)棵小树苗.冬天来了,温度急速下降,小树苗脆弱得不堪一击,于是树主人想用一些塑料薄膜把这些小树遮盖起来,经过一番长久的思考,他 ...
- 洛谷 P1268 树的重量 解题报告
P1268 树的重量 题目描述 树可以用来表示物种之间的进化关系.一棵"进化树"是一个带边权的树,其叶节点表示一个物种,两个叶节点之间的距离表示两个物种的差异.现在,一个重要的问题 ...
- Kafka自我学习-报错篇
1. kafka启动出现:Unsupported major.minor version 52.0 错误, 具体的错误输出: Exception in thread "main" ...
- CSS样式实现溢出超出DIV边框宽度高度的内容自动隐藏方法
CSS样式实现溢出超出DIV边框宽度高度的内容自动隐藏方法 平时我们布局时候,有的文字内容多了会超过溢出我们限制的高度,有的图片会撑破DIV,让网页错位变乱. 这样我们就需要解决如何使用CSS来超出设 ...
- jquery.cookie.js 的使用指南
转自:http://www.cnblogs.com/yjzhu/p/4359420.html 介绍: jquery.cookie.js 是一款轻量级的 cookie 插件,可以读取,写入和删除 coo ...