Unconsciously, the article has written 100 articles, from 0 to 1, from 1 to 100, thanks to the support of the old iron people, different micro services generally have different network address, and external clients may need to call multiple service interfaces to complete a business demand. For example, a movie ticket collection APP may call movie classification micro-service, user micro-service, payment micro-service and so on.

Zuul Microservices Gateway (1)

  • ① Causes of the microservice gateway

The company’s internal consistent use of micro services, micro services are through doubo this call each other, now up a new project needs to call film classification micro service, user micro service, payment micro service, etc..

  1. The client may request different microservices multiple times, increasing the complexity of the client.
  2. Cross-domain requests exist and are complicated to process in certain scenarios. (Some companies provide micro services through internal domain names, such as www.idig8.com/type, www.idig8.com/user and www.idig8.com/pay, so that there is no cross-domain problem. However, most companies are classified micro-service domain name type.idig8.com, user micro-service user.idig8.com, user micro-service pay.idig8.com, mainstream companies are through the secondary domain name to distinguish micro-service things, if through Ajax call, This involves cross-domain issues.)
  3. Authentication is complex, and each service requires independent authentication.
  4. Difficult to refactor. As the project iterates, it may be necessary to repartition the microservices, which can be difficult to implement if the client communicates directly with the microservices. (The microservices themselves are divided into details. The finer the details are, the more convenient the reconstruction is. For the whole, it is complicated, but for the small modules, the less detail the business logic is, the easier the reconstruction is. BAT the best feature of this large Internet company is fast, three days two head demand side, demand may change several times a day, a week can release 5, 6, is a demand for fast, rapid response needs, when doing the new requirements need to write the place with bad, reconstruct the first began to design the system is not perfect, Is really a perfect system through refactoring, refactoring may be many times, such as the above illustration if split the commodity classification of micro service, split into services, commodity prices based information services, commodity classification of goods services, finished after break up, so the original client calls a service now call 3 or 4, it also need to change.
  5. Some microservices may use other protocols, making direct access difficult. (Some services are HTTP and some are RPC, which requires support for multiple protocols, which can be particularly troublesome.)

All can be solved by micro-service network management. Microservice network management is an intermediate layer between client and server. All external requests will first pass through the microservice gateway, and the architecture evolves into (in fact, the facade design mode, unified service, reach isolation).

  • ② Zuul microservices Gateway

Zuul is Netflix’s open source microservices gateway that works with Eureka,Ribbon,Hystrix, and more. Zuul

At the heart of the component is a series of filters.

Website github.com/Netflix/zuu…

  1. Dynamic routing: Requests are dynamically routed to different back-end clusters.
  2. Stress test: Incrementally increase traffic to the cluster to understand performance.
  3. Load allocation: Allocate capacity for each load type and discard requests that exceed the limit.
  4. Static response processing: The edge location responds to avoid forwarding to the internal cluster.
  5. Authentication and security: Identify authentication requirements for each resource and reject those that do not comply.

Spring Cloud integrates and enhances Zuul. Currently, Zuul uses Apache’s HTTP by default

Client, also can use the Rest Client, you can set the ribbon. The restclient. Enabled = true.

Source: 08 – ms – gateway – zuul

Add the dependent

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
Copy the code

application.yml

server:
  port: 8040
spring:
  application:
    name: microservice-gateway-zuul
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
management:
  security:
    enabled: false
Copy the code

Run the project (need to start two user microservices and one order microservice, Eureka-server, Zuul project

1.08 – ms – consumer – order – 2.08 – ms – eureka ribbon – server 3.08 – ms – gateway – zuul 4.08 – ms – the provider – user

Access attempts: http://127.0.0.1:8040/microservice-consumer-order/user/1

Will find zuul agent of all registered to eureka server services, and use the ribbon to do load balancing, zuul routing rules are as follows, can access the address: http://localhost:8040/routes

http://ZUUL_HOST:ZUUL_PORT/ Incognito serviceId/** on Eureka is forwarded to the microservice corresponding to serviceId: http://127.0.0.1:8040/microservice-consumer-order/user/1

Zuul also automatically integrates hystrix. http://localhost:8040/hystrix.stream

PS: Currently, only one service can be accessed through one API address of zuul. However, in actual production, accessing a gateway requires calling multiple micro-services at the back end, that is, the page where the client wants to access the details of the product. If it is an interface, I need to access three interfaces at the back end. Now with Zuul I need a client that only requests one API, but can call 3-4 interfaces on the back end instead of one request at a time. Let’s talk about converged microservices gateways next time.