大家好,又见面了,我是你们的朋友全栈君。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
说明:注册中心使用Eureka,若使用其他注册中心,则导入对应的注册中心依赖。
<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-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
以服务名称为goods示例:
zuul:
routes:
goods:
path: /goods/**
sensitiveHeaders: Authorization
url: http://localhost:8081
prefix: /api
addProxyHeaders: false
更多配置请阅读官网文档:https://docs.spring.io/spring-cloud-netflix/docs/2.2.9.RELEASE/reference/html/#router-and-filter-zuul
@RestController
public class GoodsController {
@GetMapping("/hello")
public String hello() throws Exception{
return "hello world";
}
}
http://网关IP:网关端口/api/hello
@Component //必须放入Spring容器
public class AuthorizationFilter extends ZuulFilter {
@Override
public String filterType() {
//过滤器类型,取值:pre、route、post、error
return "pre";
}
@Override
public int filterOrder() {
//过滤器执行顺序,越小越优先执行
return 0;
}
@Override
public boolean shouldFilter() {
//过滤器执行条件
return true;
}
@Override
public Object run() throws ZuulException {
//过滤器执行逻辑
return "test";
}
}
深入可阅读:Zuul网关源码解析
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/170630.html原文链接:https://javaforall.cn