I. Introduction
In spring cloud, zuul is used to implement the gateway function. The client's requests first go through the load balancing Ngnix, then to the service gateway (zuul cluster), and then to the specific services. Zuul's main functions are route forwarding and filter. Routing function is a part of microservice, such as / api/server1 forwarding to server1 service. Zuul default and Ribbon combine to realize the function of load balancing.
Two, build
First POM file
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> Then in applicaton Class annotated@EnableZuulProxy,open zuul Function @SpringBootApplication @EnableZuulProxy @EnableEurekaClient @RefreshScope public class HfzZuulApplication { public static void main(String[] args) { SpringApplication.run(HfzZuulApplication.class, args); } }
yml is configured as follows.
eureka: client: serviceUrl: defaultZone: http://name:pass@IP/eureka/ instance: ip-address: Ip address prefer-ip-address: true server: port: 8769 spring: application: name: service-zuul sleuth: sampler: percentage: 1.0 cloud: config: discovery: enabled: true service-id: CONFIG-SERVER label: master profile: dev name: hfz-zuul username: name password: pass
the above is configured in the project. In order to make the project more flexible, the routing configuration is placed on github, which can be read dynamically.
zuul: routes: api-a: path: /api-a/** serviceId: service-ribbon api-b: path: /api-b/** serviceId: service-feign
Requests starting with / api-a / are forwarded to the service ribbon service; requests starting with / api-b / are forwarded to the service feign service.
III. service filtering
zuul can not only route, but also intercept some services through filtering, which can be used for security verification.
public class MyFilter extends ZuulFilter{ private static Logger log = LoggerFactory.getLogger(MyFilter.class); @Override public String filterType() { return "pre"; } @Override public int filterOrder() { return 0; } @Override public boolean shouldFilter() { return true; } @Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info(String.format("%s >>> %s", request.getMethod(), request.getRequestURL().toString())); Object accessToken = request.getParameter("token"); if(accessToken == null) { log.warn("token is empty"); ctx.setSendZuulResponse(false); ctx.setResponseStatusCode(401); try { ctx.getResponse().getWriter().write("token is empty"); }catch (Exception e){} return null; } log.info("ok"); return null; } }
filterType: returns a string representing the filter type. In zuul, four filter types with different life cycles are defined, as follows:
pre: before routing
Routing: routing time
post: after routing
Error: send error call
filterOrder: order of filtering
shouldFilter: logical judgment, whether to filter
run: concrete logic control of filter
Next you can test the access.