Earlier learned Eureka, Feign, Hystrix, Config, Benpian to learn under the API Gateway zuul. In the micro-service architecture, the backend services are often not directly open to the calling terminal, but according to the appropriate service request url, routed through a gateway API. 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.
Why do I need API Gateway
1, to simplify the complexity of client calls
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, cutting data and polymeric
Generally speaking a different client demand for the 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 user experience optimized
3, multi-channel support
Of course, we can also provide for different channels and client different API Gateway, for the use of this mode is called by another well-known way Backend for front-end, among Backend for front-end mode, we can target different clients are creating their BFF, BFF can refer to learn more about this article: Pattern: Backends the For frontends
4, micro-services transformation of legacy systems
Micro systems for service reform 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.
zuul simple to use
First, the introduction of dependence
The introduction of spring-cloud-starter-netflix-zuul, note the version number springboot and spring-cloud version number, here on the error caused due to inconsistent versions appear in the course of implementation. Xml directly following the introduction of spring-cloud-starter-netflix-eureka-client later use.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
<?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.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>SpringColudZuulSimple</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>SpringColudZuulSimple</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </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>
Second, the use @EnableZuulProxy
Add @EnableZuulProxy comment in the main method.
Third, set up a profile
Increase follows the application.properties.
spring.application.name=gateway-service-zuul
server.port=8890
zuul.routes.baidu.path=/it/**
zuul.routes.baidu.url=https://www.cnblogs.com/
Fourth, the test
Enter the browser HTTP: // localhost: 8890 / IT / 5ishare , the browser will jump to the following page.
Service of
In fact when implementing micro-service architecture, the relationship between the service name and address of the service instance in eureka server already exists, so only you need to register to eureka server Zuul go find other services, you can achieve the mapping serviceId.
First, the introduction of dependence
Add spring-cloud-starter-netflix-eureka-client in dependence SpringColudZuulSimple above.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
Second, add the configuration
spring.application.name=gateway-service-zuul
server.port=8890
zuul.routes.baidu.path=/it/**
zuul.routes.baidu.url=https://www.cnblogs.com/
zuul.routes.api-a.path=/producer/**
zuul.routes.api-a.serviceId=spring-cloud-producer
eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/
Third, the test
In turn starts EurekaServer, EurekaClient, SpringColudZuul. Enter the browser HTTP: // localhost:? 8890 / Producer / = cuiyw the Hello name .
Default gateway routing rules
By default, all agents will Zuul registered to the micro-service Eureka Server, and Zuul routing rules are as follows: http: // ZUUL_HOST: ZUUL_PORT / micro serviceId service on the Eureka / ** will be forwarded to the corresponding micro-services serviceId . Written off gateway-service-zuul-eureka project configuration on the route:
#zuul.routes.api-a.path=/producer/**
#zuul.routes.api-a.serviceId=spring-cloud-producer
Reset input HTTP: // localhost: 8890 / the Spring-Cloud-Producer / name = cuiyw the Hello? .
Reference: http: //www.ityouknow.com/springcloud/2017/06/01/gateway-service-zuul.html