博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第五篇: 路由网关(zuul)(Greenwich版本)
阅读量:4181 次
发布时间:2019-05-26

本文共 5397 字,大约阅读时间需要 17 分钟。

一、在前面的基础上新建个module,service-zuul。

 1.1、在父工程下添加该module

  

eureka-server
eureka-client
service-ribbon
service-feign
service-zuul

 1.2、在service.zuul工程下设置与父工程的关系,该pom.xml如下所示:

4.0.0
service-zuul
0.0.1-SNAPSHOT
jar
service-zuul
zuul for Spring Boot
com.example
chapter2
0.0.1-SNAPSHOT
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-devtools
true
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false

1.3、启动类ServiceZuulApplication配置如下:

package com.example;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;/** * @author xiaobu * @EnableZuulProxy 开启zuul功能 * 

* 是如果选用的注册中心是eureka,那么就推荐@EnableEurekaClient, * 如果是其他的注册中心,那么推荐使用@EnableDiscoveryClient。 */@EnableEurekaClient@EnableDiscoveryClient@EnableZuulProxy@SpringBootApplicationpublic class ServiceZuulApplication { public static void main(String[] args) { SpringApplication.run(ServiceZuulApplication.class, args); }}

1.4、配置文件application.properties内容如下

eureka.client.service-url.defaultZone=http://localhost:8001/eureka/server.port=8006#请求路径和服务名zuul.routes.xiaobu.path=/xiaobu/**zuul.routes.xiaobu.service-id=service-ribbon#请求路径和服务名zuul.routes.admin.path=/admin/**zuul.routes.admin.service-id=service-feignspring.application.name=service-zuul#热部署生效spring.devtools.restart.enabled=true

1.5访问出现如下结果:

1.6访问出现结果如下:

这样路由就起到效果了。


二、zuul还可以起到拦截的作用。

2.1、定义个MyFilter实现ZuulFilter.

package com.example.filter;import com.netflix.zuul.ZuulFilter;import com.netflix.zuul.context.RequestContext;import com.netflix.zuul.exception.ZuulException;import io.micrometer.core.instrument.util.StringUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest;import java.io.IOException;/** * @author xiaobu * @version JDK1.8.0_171 * @date on  2018/11/7 14:28 * @description V1.0 zuul过滤器 */@Componentpublic class MyFilter extends ZuulFilter {    private static Logger logger = LoggerFactory.getLogger(MyFilter.class);    /**     * @author xiaobu     * @date 2018/11/7 14:57     * @return java.lang.String     * @descprition pre:路由之前  routing:路由之时 post: 路由之后 error:发送错误调用     * @version 1.0     */    @Override    public String filterType() {        return "pre";    }    /**     * @author xiaobu     * @date 2018/11/7 15:01     * @return int     * @descprition   过滤的顺序     * @version 1.0     */    @Override    public int filterOrder() {        return 0;    }    /**     * @author xiaobu     * @date 2018/11/7 14:59     * @return boolean     * @descprition  true表示为需要过滤     * @version 1.0     */    @Override    public boolean shouldFilter() {        return true;    }    @Override    public Object run() throws ZuulException {        RequestContext requestContext = RequestContext.getCurrentContext();        HttpServletRequest request=requestContext.getRequest();        String token=request.getParameter("token");        //字符串替换替换        logger.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString()));        if(StringUtils.isBlank(token)){            requestContext.setSendZuulResponse(false);            requestContext.setResponseStatusCode(401);            try {                requestContext.getResponse().setContentType("text/html;charset=UTF-8");                requestContext.getResponse().getWriter().print("令牌是空的。");            } catch (IOException e) {                e.printStackTrace();            }        }        logger.info("ok");        return null;    }}

2.2、访问结果如下所示:


源码地址:

转载地址:http://eugai.baihongyu.com/

你可能感兴趣的文章
mysql排行榜并列与不并列
查看>>
SpringBoot | Mybatis申明为Mapper文件
查看>>
JPA主键生成策略
查看>>
byte数组和InputStream的相互转换
查看>>
InputStream,InputStreamReader和Reader之间的区别与关系
查看>>
Java中System.arraycopy方法的使用
查看>>
tk.mybatis的使用记录
查看>>
遍历获取目录下的所有文件
查看>>
从指定服务器路径下载文件
查看>>
EasyExcel读取和写入java model数据
查看>>
《C编译原理》共享库的动态加载和静态加载
查看>>
《Android系统学习》第二章:如何制作OTA U盘升级包
查看>>
《Android系统学习》第五章:编译Android的JDK环境
查看>>
《C++特性》之引用类型
查看>>
fflush(stdin)在gcc编译器中不起作用?
查看>>
《Android系统学习》第九章:Android模拟器编译
查看>>
《Android系统学习》第十章:Android消息处理、消息循环和消息队列
查看>>
《Android系统学习》第十一章:Android应用程序Activity组件分析
查看>>
Android4.2 Input子系统
查看>>
《C++面向对象》结构体继承
查看>>