本文共 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/