zuul application side
Add module in spring-cloud-demo project: zuul-app
added to rely on pom.xml file:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
Increase application.properties file in the resource directory, add the following:
server.port=8080
spring.application.name=zuul-app
eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eureka/
zuul.routes.zuul-use.path=/api/**
zuul.routes.zuul-use.serviceId=zuul-use
Create a SpringBoot project startup class in java catalog: ZuulApplication, reads as follows:
/**
* @CalssName ZuulApplication
* @Description TODO
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
zuul end use
Add module in spring-cloud-demo project: zuul-use
join rely on pom.xml file:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
Increase application.properties file in the resource directory, add the following:
server.port=8903
spring.application.name=zuul-use
eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eureka/
Create a SpringBoot project startup class in java catalog: ZuulUseApplication, reads as follows:
/**
* @CalssName ZuulUseApplication
* @Description TODO
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ZuulUseApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulUseApplication.class,args);
}
}
Creating a Controller: HelloController, reads as follows:
/**
* @CalssName controller
* @Description TODO
*/
@RestController
public class HelloController {
@RequestMapping(value = "/hello")
public String hello(){
return "hello,I'm from zuul-use...";
}
}
Eureka in order to start the server, zuul-app and zuul-use, browser access: http: // localhost: 8080 / api / hello, output is as follows:
hello,I'm from zuul-use...
Description zuul successful application
Zuul Advanced Use
Example of use Zuul filter to filter token, for example, in the filter construction TokenFilter zuul-app, as follows:
/**
* @CalssName TokenFilter
* @Description Token Zuul过滤器
* @Author Alyshen
*/
public class TokenFilter extends ZuulFilter {
private final Logger logger = LoggerFactory.getLogger(TokenFilter.class);
@Override
public String filterType() {
return "pre"; // 可以在请求被路由之前调用
}
@Override
public int filterOrder() {
return 0; // filter执行顺序,通过数字指定 ,优先级为0,数字越大,优先级越低
}
@Override
public boolean shouldFilter() {
return true;// 是否执行该过滤器,此处为true,说明需要过滤
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
logger.info("--->>> TokenFilter {},{}", request.getMethod(), request.getRequestURL().toString());
String token = request.getParameter("token");// 获取请求的参数
if (StringUtils.isNotBlank(token)) {
ctx.setSendZuulResponse(true); //对请求进行路由
ctx.setResponseStatusCode(200);
ctx.set("isSuccess", true);
return null;
} else {
ctx.setSendZuulResponse(false); //不对其进行路由
ctx.setResponseStatusCode(400);
ctx.setResponseBody("token is empty");
ctx.set("isSuccess", false);
return null;
}
}
}
In the startup class zuul-app added as follows:
@Bean
public TokenFilter tokenFilter() {
return new TokenFilter();
}