SpringCloud|SpringCloud - zuul网关
POM
4.0.0 com.boolib.cloud
server_zuul
0.0.1-SNAPSHOT jarserver_zuul
Demo project for Spring Boot 【SpringCloud|SpringCloud - zuul网关】org.springframework.boot
spring-boot-starter-parent
2.0.4.RELEASE
UTF-8UTF-8
1.8
Finchley.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.boot
spring-boot-devtools
runtime
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
zuul启动项
@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ServerZuulApplication {public static void main(String[] args) {
SpringApplication.run(ServerZuulApplication.class, args);
}
}
参数配置
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/server.port=8763spring.application.name=service-zuulzuul.routes.api-a.path=/member/**zuul.routes.api-a.service-id=service-orderzuul.routes.api-b.path=/provide/**zuul.routes.api-b.service-id=service-providezuul.routes.api-c.path=/client/**zuul.routes.api-c.service-id=config-client
ps:api-c 这个是自己写的 service-id = 值 千万别写错!!!
filter
package com.boolib.cloud.server_zuul.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Component
@Slf4j
public class MyFilter extends ZuulFilter {@Override
public String filterType() {
return "pre";
}@Override
public int filterOrder() {
return 0;
}@Override
public boolean shouldFilter() {
return true;
}@Override
public Object run() throws ZuulException {RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
Object token = request.getParameter("token");
if(token!=null)
{
return null;
}ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
try {
ctx.getResponse().getWriter().write("token is empty");
}
catch(IOException ex)
{}
return null;
}
}
推荐阅读
- 【SpringCloud-Alibaba系列教程】8.一文学会使用sentinel
- 史上最简单的SpringCloud教程|史上最简单的SpringCloud教程 | 第十篇: 高可用的服务注册中心(Finchley版本)
- Java架构师必备(Springcloud微服务+Docker+Springboot+分布式架构+Nginx+SSM+MySQL)
- 【SpringCloud-Alibaba系列教程】5.负载均衡
- 【SpringCloud-Alibaba系列教程】4.服务管理
- 网关流控利器(结合 AHAS 实现 Ingress/Nginx 流量控制)
- 网关流控利器(结合|网关流控利器:结合 AHAS 实现 Ingress/Nginx 流量控制)
- 网关服务免登录、免检
- SpringCloud之WebSocket配置的最佳方式
- SpringCloud浅尝(六)——Bus