网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- zuul路由 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
### 2、创建配置文件
server:
port: 8004
spring:
application:
name: zuul
eureka:
client:
service-url:
defaultZone: http://localhost:8001/eureka/
feign:
hystrix:
enabled: true
zuul:
routes:
app: #活动
path: /app/** #配置请求URL的请求规则
serviceId: app #指定Eureka注册中心中的服务id
dms: #活动
path: /dms/** #配置请求URL的请求规则
serviceId: dms #指定Eureka注册中心中的服务id
logging:
pattern:
console: ‘%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n’
### 3、创建Zuul过滤器
/**
* @Author:公众号:程序员965
* @create 2022-06-30
**/
@Component
public class MyZuulFilter extends ZuulFilter {
// pre :可以在请求被路由之前调用
// route :在路由请求时候被调用
// post :在route和error过滤器之后被调用
// error :处理请求时发生错误时被调用
@Override
public String filterType() {
return “pre”;// 前置过滤器
}
//通过int值来定义过滤器的执行顺序,优先级为0,数字越大,优先级越低
@Override
public int filterOrder() {
return 0;
}
//返回一个boolean类型来判断该过滤器是否要执行,所以通过此函数可实现过滤器的开关
@Override
public boolean shouldFilter() {
return true;//此处为true,说明需要过滤
}
//过滤器的具体逻辑。
@Override
public Object run() throws ZuulException {
// 获取上下文
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
String accessToken = request.getParameter("accessToken");
if (StringUtils.isEmpty(accessToken)) {
//setSendZuulResponse(false)令zuul过滤该请求,不进行路由
currentContext.setSendZuulResponse(false);
//设置返回的错误码
currentContext.setResponseStatusCode(401);
currentContext.setResponseBody("AccessToken is null");
return null;
}
System.out.println("获取到AccessToken为:"+accessToken);
// 否则正常执行业务逻辑.....
return null;
}
}
### 4、编写启动类
注意增加`@EnableZuulProxy`注解
/**
* @Author:公众号:程序员965
* @create 2022-06-30
**/
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
### 5、启动验证
这个时候,我们将直接请求app的接口地址,改成网关zuul的地址和端口:http://localhost:8004/app/index
![img](https://img-blog.csdnimg.cn/img_convert/91e535dc2d33469423cdb2817f726732.png)
![img](https://img-blog.csdnimg.cn/img_convert/2cb2471199ceb4f67ea2ca8d34c46935.png)
![img](https://img-blog.csdnimg.cn/img_convert/b873b57adf00054d95dc3a52cc846b03.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/forums/4f45ff00ff254613a03fab5e56a57acb)**