SpringCloud entry-5 --- Zuul (Serving Gateway)

SpringCloud Getting Started 1 Introduction ---

SpringCloud entry 2 --- Eureka (service discovery component)

SpringCloud Getting Started 3 --- Feign (service call component)

SpringCloud entry 4 --- Hystrix (fuse assembly)

SpringCloud entry-5 --- Zuul (Serving Gateway)

The fifth chapter, we do the right SpringCloud in Zuul (Serving Gateway) a brief description:

1. Why do we need micro gateway service

       Different micro-services in general have different network addresses, and external clients may need to call multiple service interfaces to complete a business needs. For example, a movie ticket collection APP, micro film classification may call back services, users micro-service, micro-payment services. If the client and micro service to communicate directly, there will be questions about:
# client repeatedly requested different micro-services, increase the complexity of the client;
# cross-domain requests, treated at a certain scene is relatively complex;
# authentication complex, each service requires a separate certification;                                                                                                                  # is difficult to reconstruct, with the iteration of the project may need to be re-divided micro service, if the client directly and micro-service communication, then the reconstruction will be difficult to implement;
# some services may use other micro- agreement, direct access will be difficult;
these problems, can be solved by means of micro-services gateway. Micro service gateway is interposed an intermediate layer between the client and the server, all requests will go through the external micro serving gateway.

2, Zull Profile

Zuul is open source Netflix micro-services gateway, and he could Eureka, Ribbon, Hystrix and other components used in conjunction. Zuul core component is a series of filters, these filters can perform the following functions:
# identity authentication and security: identification verification requirements for each resource, and to reject those requests inconsistent;
# review and monitoring : monitoring request service load;
# dynamic routing : dynamic route requests to different backend cluster;
# stress test: increasing traffic to the cluster, in order to understand the performance;
# load distribution: corresponds to the capacity allocated for each type of load, and the limit value is exceeded deprecated request;
# static response processing: edge position to respond, to avoid forwarded to the internal cluster;
# multi-regional flexibility: Cross-domain routing request AWS region designed to achieve ELB (ElasticLoad Balancing) the use of diverse;
the Spring of Zuul have been integrated Cloud and enhanced.
After using Zuul, architecture diagram the evolution of the form:

3, Zuul gateway integration

  (1) create a sub-module test_manager, pom.xml dependent eureka-client and the introduction of zuul

<dependencies>
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring‐cloud‐starter‐netflix‐eureka‐client</artifactId>
   </dependency>
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring‐cloud‐starter‐netflix‐zuul</artifactId>
   </dependency>
</dependencies>

  (2) create application.yml

server:
  port: 9011
spring:
   application:
     name: test‐manager #指定服务名
eureka:
  client:
    serviceUrl: #Eureka客户端与Eureka服务端进行交互的地址
      defaultZone: http://127.0.0.1:6868/eureka/
  instance:
    prefer‐ip‐address: true
zuul:
  routes:
    test‐gathering: #活动
      path: /gathering/** #配置请求URL的请求规则
      serviceId: tensquare‐gathering #指定Eureka注册中心中的服务id
     test‐article: #文章
      path: /article/** #配置请求URL的请求规则
      serviceId: tensquare‐article #指定Eureka注册中心中的服务id
     test‐user: #用户
      path: /user/** #配置请求URL的请求规则
      serviceId: tensquare‐user #指定Eureka注册中心中的服务id

(3) Start writing class 

@EnableZuulProxy
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

4, Zuul filter 

Create a simple filter zuul

@Component
public class WebFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return "pre";// 前置过滤器
    }
    @Override
    public int filterOrder() {
        return 0;// 优先级为0,数字越大,优先级越低
    }
    @Override
    public boolean shouldFilter() {
        return true;// 是否执行该过滤器,此处为true,说明需要过滤
    }
    @Override
    public Object run() throws ZuulException {
        System.out.println("zuul过滤器...");
        return null;
    }
}

Start Zuul service will find that the filter has been executed
filterType: Returns a string that represents the type of filter, filter type defines four different life cycle in zuul, as follows:

  • pre: it can be called before the request is routed
  • route: the route is called when the request
  • post: is called after the route and filter error
  • error: is called when an error occurred while processing the request

filterOrder : to define a filter by performing an int sequence
shouldFilter: returns a boolean determining whether the filter is to be performed, so this function can be achieved by switching the filter. In the above example, we directly returns true, so the filter is always active
run: the specified logical filter. 

Gateway service has been introduced so far Zuul completed, simply is a pre-service, distribution service can do, filtering, authentication, current limiting and not associated with the business in order to concentrate on the back of each micro services can do their business services.

 

 


 

Published 41 original articles · won praise 47 · views 30000 +

Guess you like

Origin blog.csdn.net/u014526891/article/details/87476236