table of Contents
A, Spring Cloud Gateway building
Second, build a common application scenarios
1, with the support of the registry Spring Cloud Integration
3), arranged to start class added @EnableEurekaClient
2, with the circuit breaker Hystrix integration
3, the request frequency limit
6, RouteDefinitionLocator coded manner
Project Address: https://github.com/kevin-lihongmin/spring-cloud-project-kevin/tree/master/gateway-demo
A, Spring Cloud Gateway building
Add start.spring.io the Actuator, Gateway module, the default will open Spring Cloud Gateway service (you can add in the configuration spring.cloud.gateway.enabled = false to shut down).
Application.properties added in the following configuration:
# Service port server.port = 8900 # Application Name spring.application.name=gateway-server
Second, build a common application scenarios
1, with the support of the registry Spring Cloud Integration
Spring Cloud Gateway can communicate with various registration centers (Eureka Server, Zookeeper, Consul) Spring Cloud support order, to the current Eureka Server service centers, which in fact is the same in other registries.
1) some advance preparation
Eureka Server registry see: the Spring Cloud Eureka Server cluster and client calls .
The service provider, please see written before ribbon-demo in the ribbon-provider module (Project address: https://github.com/kevin-lihongmin/spring-cloud-project-kevin/tree/master/ribbon-demo ) , started using Spring profiles of different configurations.
2), configuration files
Add bootstrap.properties configuration of gateway-demo:
# Configure Eureka Server registry address
eureka.client.service-url.defaultZone = http://127.0.0.1:8761/eureka/,\
http://127.0.0.1:8760/eureka/,http://127.0.0.1:8759/eureka/
# Gateway function to open service registration and discovery
spring.cloud.gateway.discovery.locator.enabled = true
# Get name registration service center will be operational Lower Case
spring.cloud.gateway.discovery.locator.lower-case-service-id = true
# Enable default after routing rules zuul consistent with, it will be registered to install the service name registry for load balancing call, the following is the default rule
spring.cloud.gateway.discovery.locator.url-expression='lb://'+serviceId
3), arranged to start class added @EnableEurekaClient
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
/**
* Spring Cloud Gateway 服务网关
*
* @author kevin
* @date 2019/6/4 10:33
* @since 1.0
*/
@EnableEurekaClient
@SpringBootApplication
public class GatewayDemoApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayDemoApplication.class, args);
}
}
4) to start the service
In the original service ribbon-provider, the start of the three-port node are: 8765,8766,8767. For example, visit http: // localhost: 8765 / user / 1
Now visit HTTP: // localhost: 8900 / Ribbon-the User-Provider / the User / 1 , defaults registered to the name registry access and load balancing .
2, with the circuit breaker Hystrix integration
pending upgrade
3, the request frequency limit
pending upgrade
4, Predicate integration
pending upgrade
5, filter integration
pending upgrade
6, RouteDefinitionLocator coded manner