Spring Cloud Gateway - gateway basic function API exposure

API gateway

API gateway is a kind of design pattern, a kind of classic component in microservice system. For the latest API gateway pattern, please refer to Ao Xiaojian Written In depth discussion on the relationship between Service Mesh and Api Gateway

In the early stage of SOA, there are also API gateways. For example, the open platform interface contains a series of functions. For example, Taobao provides many capabilities, such as user authorization, e-commerce, payment, express delivery, invoice, commodity management, etc., which must be provided by multiple SOA services, all from a unified gateway service https://eco.taobao.com/router/rest (this is Taobao API The latest interface, not the earlier one, is exposed.

In the microservice scenario, API gateways are more widely used, and some small business interfaces are often scattered on multiple back-end services. In order to achieve unified interface management, authority verification and general processing, API gateways are needed. Some designs also rely heavily on API gateway for business isolation, subscription generation, set routing, etc.

Common open source implementations of API gateway include: nginx spring cloud zuul,spring cloud gateway , kong and other cloud service providers also have API gateway services, such as Alicloud,Aws,Azure There are API gateway products.

config file mode configuration

Route requests to http://httpbin.org:80's yml configuration:

      - id: default_path_to_httpbin
        uri: http://httpbin.org:80
        order: 10000
        - Path=/**

java code mode configuration

Routing to via java code configuration http://httpbin.org 80

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

	public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
		// String uri = "http://httpbin.org:80";
		// String uri = "http://localhost:9080";
		return builder.routes()
				.route(r -> r.path("/**").uri("http://httpbin.org:80"))

Dynamic service discovery mode configuration

Add Netflix eureka client dependency:


Add @ EnableDiscoveryClient

public class SpringCloudGatewayExamplesLbApplication {

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


Add eureka service discovery configuration

# use eureka discovery, so add eureka discovery server config
      defaultZone: http://localhost:8761/eureka/
    enabled: true 
    prefer-ip-address: true

# open gateway discovery locator and close non-flux loadbalancer
    name: spring-cloud-gateway
          enabled: true
        enabled: false

All http://localhost : a request in the form of 8080 / EXAMPLE-SERVICE / Hello will automatically discover the service provider with the service name of EXAMPLE-SERVICE and send http://localhost:890/hello request.

Tags: Spring Java REST Nginx

Posted on Sat, 13 Jun 2020 05:38:10 -0400 by msarefin