(Ix) Service Gateway Zuul (routing)

I. Introduction

Before introducing several Spring Cloud by several core components, we can already build a simple micro-service architecture, and this might look like the following figure:

 

We use Spring Cloud Netflix in Eureka achieve a service registry and service registration and discovery; and implement services between service through Ribbon or Feign consumption and load balancing; by Spring Cloud Config achieve external application multi-environment configuration and version management . In order to make the service more robust cluster failure caused by the use of financial mechanisms Hystrix off to avoid abnormal individual services in the micro-services architecture spread. It seems to be a micro-services framework has been completed.

We still consider a few issues, external application of how to access a variety of internal micro-services? In the micro-service architecture, back-end services are often not directly open to the calling end, but to the appropriate service based on the URL request is routed through an API gateway. When adding API Gateway, third-party service providers and end calls between the parties to create a wall, this wall permissions control directly communicate with the caller, after requesting a balanced distribution to the background server.

 

 

Second, why need API Gateway?

1, to simplify the client calls the complexity of the
number of instances in the micro-services architecture model back-end services in general is dynamic, in terms of hard to find the client access address service instances dynamically changing information. Thus based on the micro-program services in order to simplify the front end of the call logic, which usually involves a gateway API Gateway As a lightweight, while API Gateway will also implementation-dependent authentication logic to simplify the complexity of the cross call between the inner and services.

 

2, data aggregation and cutting
general different client demand for display data is inconsistent, such as mobile phones or the Web side and end or low-latency network environment or high-latency network environment.

Therefore, in order to optimize the client experience, API Gateway can be tailored to the versatility of the response data to suit the needs of different clients. But also can be a plurality of API call logic polymerization, thereby reducing the number of request from the client, the client to optimize the user experience.

 

3, multi-channel support
, of course we can also provide different API Gateway for different channels and clients, using this mode is called by another well-known way Backend for front-end, among Backend for front-end mode, we can create their BFF respectively, for different clients, to learn more about BFF can refer to this article: Pattern: Backends the for frontends

 

4, micro-services transformation of legacy systems
micro service reform for the system is usually due to a problem more or less the original system, such as technical debt, code quality, maintainability, scalability and so on. API Gateway mode also applies to this type of transformation of legacy systems, the progressive realization of the repair of problems in the original system through the transformation of micro-services, so as to enhance existing business to enhance the response force. By introducing an abstraction layer, to achieve gradually replace the old with the new implementation.

In Spring Cloud system, Spring Cloud Zuul is to provide load balancing, reverse proxy, a certification authority API Gateway.

We say this is the Zuul Zuul 1, in fact, Netflix has released Zuul 2, but Spring does not seem to be integrated into the Zuul 2 Spring Cloud ecology meaning, because it had become one Spring Cloud Gateway (presumably because before Zuul 2 has been bouncing leads can not wait for it). About Spring Cloud Gateway This high-performance gateway us later.

 

 

三、Spring Cloud Zuul

Spring Cloud Zuul routing is an integral part of the micro-service architecture, providing dynamic routing, monitoring, edge flexibility, security and other services. Zuul Netflix is ​​produced based on a JVM routing and server load balancer.

 

We adopted the following code to understand how it works Zuul

 

 

Fourth, real

1. Preparations

Before building the services gateway, we are ready to look at the inside of the first micro gateway services, you can use the direct content written before several, such as:

  • eureka
  • producer
  • consumer

After starting an instance eureka, producer and consumer, all the preparatory work to be ready, let's try to use Spring Cloud Zuul to achieve functional services gateway.

 

2. Create a project api-gateway

 

3.POM file as follows 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>api-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>api-gateway</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    </properties>

    <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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

 

4. application.yml profile as follows:

spring:
  application:
    name: api-gateway
server:
  port: 14000
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/

 

5. Start the class

Use  @EnableZuulProxy annotations to open Zuul function

package com.example.apigateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableZuulProxy
@SpringBootApplication
public class ApiGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiGatewayApplication.class, args);
    }

}

 

Here, a service gateway based on Spring Cloud Zuul had finished building. Start the application, a default service gateway to finished building, and at the same time be able to see this service in Eureka.

 

Five test

Since the Spring Cloud Zuul after the integration of Eureka, with the default service routing functions, namely: When we built here  api-gateway after the application is up and register to Eureka, the service gateway will find the above we launched the two services  producer and  consumer, at this time will Zuul create two routing rules. Each routing rule consists of two parts, a matching rule external requests, the other is the route service ID. For the current example, Zuul will create the following two routing rules:

  • Forwarded to the  producer requested service rules:/producer/**
  • Forwarded to the  consumer requested service rules:/consumer/**

Finally, we can access  14000 to verify the correctness of the above routing service gateway ports:

Note that the consumer is the service name, if your name is not this service, you need to change your service name.

 

 

Learn from https://windmt.com/2018/04/23/spring-cloud-10-zuul-router/

Published 111 original articles · won praise 1 · views 5440

Guess you like

Origin blog.csdn.net/daziyuanazhen/article/details/105006242