Through the introduction of the previous articles, we can easily build up a few of the more important infrastructure services in the microservices system. So, in this blog post, we'll focus on how to design API Gateway concepts by agreeing to expose all microservices APIs.
This series of tutorials
"MicroServices" easy to fix springcloud Micro Service catalog This series is a serial article, before reading this article we strongly recommend that you read the first few.
Gateway Name Interpretation
Gateways (Gateway) are also known as inter-network connectors, protocol converters. The gateway realizes network interconnection above the network layer, is the most complex network interconnection device, only for two high-level protocol different network interconnection. Gateways can be used for both WAN interconnection and LAN interconnection. A gateway is a computer system or device that acts as a transformation task. A gateway is a translator that uses a different communication protocol, data format, or language, or even between two systems that have a completely different architecture.
API Gateway noun Explanation
API Gateway (API GW/API Gateway), as its name implies, is an API-oriented, serial centralized, strong-control service that appears on the system boundary, where boundaries are the boundaries of enterprise IT systems. Prior to the popularity of microservices, API GW entities have been born, the main application scenario is OPENAPI, that is, open platform, for the enterprise external partners, for this application scenario, I believe that contacts will be more. When the concept of microservices became popular, API gateways seemed to be the standard component for integration in the upper application layer.
About Spring Cloud Zuul
Zuul is an open source component of Netflix that is dedicated to providing a framework for dynamic routing, monitoring, resiliency, security and other edge services on cloud platforms. There are also many companies that use it as an important part of the gateway. The module, which is included in the Spring Cloud system, is designed to provide dynamic routing, monitoring, security control, current limit quotas, and so on, to allow internal microservices APIs to be exposed.
Blog System Architecture
Based on an architectural design of our microservices as a whole, this series of blogs is primarily a demonstration of the entire microservices architecture. The concept of a gateway can be more deeply understood through a set of schema comparisons.
Traditional Internet architecture
In the architecture without microservices, we generally use nginx as the load distribution, reverse proxy, to form a building of an API gateway. The schema diagram looks like this:
Gateway mode under micro-service
In the spring cloud system, we have all the internal services hidden, there is only one external exposure mechanism, this is the spring cloud Zuul gateway. The schema diagram looks like this:
If we go into some, to be compatible with some previous company old non-microservices system can also be like this:
Start takeoff
Also, before taking off, it is recommended to read the previous articles to facilitate code understanding and use.
Creating Gateway Sub-projects
We create a subproject under the original parent class project. (You can refer to my github code structure)
Configuring the Pom file
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency></dependencies><build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins></build>
Creating a configuration file Application.yml
The resources folder creates a file Application.yml, with the following configuration list:
#服务注册中心配置eureka: client: service-url: defaultZone: http://localhost:8081/eureka/ instance: appname: api-gateway#设置网关端口号server: port: 8080spring: application: name: api-getway
Create a program entry Application.java
For convenience, the name is Gatewayapplication
/** * Created by Administrator on 2017/12/17. * 网关启动入口 */@SpringBootApplication@EnableZuulProxypublic class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); }}
Note @EnableZuulProxy Note: @EnableZuulProxy simple to understand as an enhanced version of @enablezuulserver, when Zuul and Eureka, ribbon and other components used in conjunction, We use @enablezuulproxy.
Start and test
From the above configuration, we have completed the basic gateway configuration, then we will do a simple test. This test selects my microservices program with three services:
Sub-project name |
Service Name |
Port number |
Source Code |
Cloud-hyh-service-1 |
Cloud-service |
8071 |
GitHub |
Cloud-hyh-discovery-eureka |
Discovery-service |
8081 |
GitHub |
Cloud-hyh-api-gateway-zuul |
Api-gateway |
8080 |
GitHub |
Start Child Service
Start the registry, test services, API Gateway three services respectively. We can observe that the service registry can already see that the other two services are registered.
Accessing API Tests
We have already done a test API in the Cloud-service sub-project,
Sub-Micro services |
Address |
Path |
Request Method |
Cloud-service |
http://localhost:8071 |
/ribbon/name |
GET |
Before we have a gateway, we can access the service's own address if we want to invoke the flowers of the services. But with the API Gateway, we can expose our service address to the outside.
Sub-Micro services |
Address |
Path |
Request Method |
Cloud-service |
http://localhost:8080 |
/ribbon/name |
GET |
Access Address: Gateway address/Service Name/access address/http://localhost:8080/cloud-service/ribbon/name
Test successfully returned in browser: Tens of thousands of roads just started-www.hanyahong.com-beijing the server port 8071
Description: There are many parameters that need to be configured for API gateways, such as persistence, load distribution, and so on. For this piece we will also carry out a detailed in-depth topic.
Source
This article source: Http://www.hanyahong.com/Github Source: Https://github.com/hanyahong/spring-cloud-microservice forwarding Please indicate the source!
"MicroServices" Six: Easy to fix springcloud microservices-API Gateway Zuul