table of Contents
1, add the Web, Actuator, Zuul module in the start.spring.io
2, application.properties Configuration
4, add @EnableZuulProxy start the service startup class, and register a custom Filter-bit Bean
5, start the service, see Routing Rules
Project address: https://github.com/kevin-lihongmin/spring-cloud-project-kevin/tree/master/zuul-demo
About a word in Spring Cloud Zuul official website: Zuul IS A Router and the JVM-based Server-Side from the Load Balancer Netflix.Zuul is a router-based memory is server load balancer. Key features include:
Authentication: security verification, based on filter realization
Stress Testing: Stress Test
Dynamic Routing: Dynamic Routing
Static Response handling: static resources to deal with similar Nginx
1, add the Web, Actuator, Zuul module in the start.spring.io
2, application.properties Configuration
In order to facilitate the understanding of the part should be placed bootstrap.properties also arranged into the configuration. requires attention:
Consumer-zuul.routes.user the User-Consumer are registered to the service name in Eureka Server
# 服务注册名称 spring.application.name=zuul-gateway # 服务端口 server.port=8800 # 注册中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/,\ http://localhost:8760/eureka/,http://localhost:8759/eureka/ # 将IP注册到Eureka Server上,如果不配置就是机器的主机名 eureka.instance.prefer-ip-address=true # 默认路由规则是使用Eureka注册的服务名称 http://ZUULHOST:ZUULPORT/serviceId/** # 配置路由规则 zuul.routes.ribbon-user-consumer-eureka = /kevin-user/** # 忽略的服务 zuul.ignored-services=ribbon-provider
3, Custom Filter
Speaking of the above can be used as Zuul (Authentication: secure authentication based filter to achieve) to achieve, so I wrote myself a Filter inherited from ZuulFilter , in order to achieve when the print log service requests, of course, can do here such as secure authentication services . The time required to implement multiple Filter, and can be sorted according to those behind filterOrder () method returns the value realized go in-depth study.
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import javax.servlet.http.HttpServletRequest;
public class PreRequestLogFilter extends ZuulFilter {
@Override
public String filterType() {
return "pre";
}
@Override
public int filterOrder() {
return 0;
}
@Override
public boolean shouldFilter() {
return true;
}
@Override
public Object run() {
RequestContext currentContext = RequestContext.getCurrentContext();
HttpServletRequest request = currentContext.getRequest();
System.out.print(String.format("send %s request to %s", request.getMethod(),
request.getRequestURL()));
return null;
}
}
4, add the startup class @EnableZuulProxy start the service, and register a custom Filter-bit Bean
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
/**
* 路由Api gateway
* 默认路由规则为: http://ZUUL-HOST:ZUUL-PORT/serviceId/**
*
* @author kevin
* @date 2019/5/30 9:15
* @since 1.0
*/
@SpringBootApplication
@EnableZuulProxy
public class ZuulDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulDemoApplication.class, args);
}
@Bean
public PreRequestLogFilter preRequestLogFilter(){
return new PreRequestLogFilter();
}
}
5, start the service, see Routing Rules
Since zuul just do agency services, it is necessary to request mapping to a specific registered to Eureka Server service registry, and selected before building over the ribbon-demo project: address load balancing implementation Spring Cloud-Ribbon startup ribbon -provider and ribbon-consumer-eureka two services.
Registration required: When adding zuul in start.spring.io in time, add a ribbon rely relevant packages, and zuul proxy service requires the introduction of ribbon, otherwise it will error. Just started building when he encountered a problem.
1), the service started, the service access address ribbon-consumer-rureka are: HTTP: // localhost: 8769 / getRemoteUser / 1
2), addzuul.routes.ribbon-user-consumer = /kevin-user/**
the configuration, visit the following address:
http://localhost:8800/kevin-user/getRemoteUser/1
3), removezuul.routes.ribbon-user-consumer = /kevin-user/**
the configuration默认路由规则是使用Eureka注册的服务名称 http://ZUULHOST:ZUULPORT/serviceId/**
, as we want to accessribbon-user-consumer-eureka服务
, access address is:
http://localhost:8800/ribbon-user-consumer-eureka/getRemoteUser/1
6, custom log filter
When requesting access to services, print log is as follows:
send GET request to http://localhost:8800/ribbon-user-consumer-eureka/getRemoteUser/1