在之前一篇博客搭建的springcloud聚合项目基础上,https://blog.csdn.net/qq_41890624/article/details/103663817
创建一个maven项目,名称为springcloud-zuul
引入依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> </parent> <!-- 管理依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- SpringBoot整合eureka客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <!-- 注意: 这里必须要添加, 否者各种依赖有问题 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> application.yml ###注册 中心 eureka: client: serviceUrl: defaultZone: http://localhost:8100/eureka/ server: port: 80 ###网关名称 spring: application: name: service-zuul ### 配置网关反向代理 zuul: routes: api-a: ### 以 /api-member/访问转发到会员服务 path: /api-member/** ###要转发服务的别名 serviceId: app-vhukze-member api-b: ### 以 /api-order/访问转发到订单服务 path: /api-order/** serviceId: app-vhukze-order
创建一个启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication @EnableEurekaClient @EnableZuulProxy public class ZuulApp { public static void main(String[] args) { SpringApplication.run(ZuulApp.class, args); } }
创建一个过滤器
import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import com.netflix.zuul.ZuulFilter; import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.exception.ZuulException; @Component public class TokenFilter extends ZuulFilter{ //编写过滤器拦截业务代码 public Object run() throws ZuulException { // TODO Auto-generated method stub //案例:拦截所有服务接口,判断服务接口上是否传有UserToken类型参数 RequestContext currentContext = RequestContext.getCurrentContext(); HttpServletRequest request = currentContext.getRequest(); String token = request.getParameter("userToken"); if(StringUtils.isEmpty(token)) { //不会继续执行,直接响应给客户端 currentContext.setSendZuulResponse(false); //响应的内容 currentContext.setResponseBody("userToken is null"); } return null; } //过滤器是否执行,可以添加一些逻辑,当满族条件时执行过滤器 public boolean shouldFilter() { // TODO Auto-generated method stub return true; } //过滤器执行顺序,当一个请求在同一个阶段有过个过滤器时,多个过滤器的执行顺序 @Override public int filterOrder() { // TODO Auto-generated method stub return 0; } //过滤器类型 pre是请求前执行 // pre:在请求被转发之前,执行过滤器 // **route:**过滤器可以处理实际的请求转发 // post:在请求被转发之后,执行过滤器 // error:在请求被转发时发生错误,执行过滤器 @Override public String filterType() { // TODO Auto-generated method stub return "pre"; } }
启动Eureka、订单服务项目、会员服务项目、zuul网关项目
80端口号可以省略的
访问http://localhost/api-order/toMember?name=22
访问http://localhost/api-order/toMember?name=22&userToken=111