Previously, we have carried on an introductory study of Spring Cloud Gateway. For specific articles, you can see “Spring Cloud Gateway Early Taste” to learn.

The Gateway is responsible for forwarding, so it needs to know the service information of the back-end. Today, we will learn the operation of integrating Eureka with Spring Cloud Gateway to realize the service forwarding function.

Add the eureka-client dependency to the previous one:

<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
Copy the code

The following is the configuration of specific forwarding rules, which need to pay attention to the configuration of uri:

server:
  port: 8084
spring:
  cloud:
    gateway:
      routes:
      - id: fsh-house
        uri: lb://fsh-house
        predicates:
        - Path=/house/**
        
  application:
    name: fangjia-gateway

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://yinjihuan:123456@master:8761/eureka/
Copy the code

The uri starts with lb:// (lb stands for service from registry) and is followed by the name of the service you need to forward to. This service name must correspond to that in Eureka, otherwise the service will not be found.

org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for fsh-house1
Copy the code

If the spring-cloud-starter-Netflix-Eureka-client package is introduced, but you don’t want to integrate Eureka, you can also disable it with the following configuration:

eureka.client.enabled=false
Copy the code

Direct configuration of routing, and we said no configuration mode can also be forwarded, used Zuul students must know that Zuul will default for all services for forwarding operation, only need to access the services specified in the access path, in this way, don’t have to go forward configuration rules, every service when the new added services, There is no need to configure routing rules and restart the gateway.

Spring Cloud Gateway also has this function, which can be enabled only by configuring it as follows:

spring.cloud.gateway.discovery.locator.enabled=true
Copy the code

After this function is enabled, we can access the service through the address. The format is as follows:

http:// gateway address/service name (upper) / * * http://localhost:8084/FSH-HOUSE/house/1Copy the code

If we upgrade from Zull to Spring Cloud Gateway, it means that the request address has been changed, or the routing address of each service has been reconfigured.

spring.cloud.gateway.discovery.locator.lowerCaseServiceId=true
Copy the code

After the configuration is complete, we can access the service with the lowercase name as follows:

http:// gateway address/service name (lowercase) / * * http://localhost:8084/fsh-house/house/1Copy the code

Configuration source: org. Springframework. Cloud. Gateway. Discovery. DiscoveryLocatorProperties

@ConfigurationProperties("spring.cloud.gateway.discovery.locator") public class DiscoveryLocatorProperties { /** Flag that enables DiscoveryClient gateway integration */ private boolean  enabled =false;

	/**
	 * The prefix for the routeId, defaults to discoveryClient.getClass().getSimpleName() + "_".
	 * Service Id will be appended to create the routeId.
	 */
	private String routeIdPrefix;

	/**
	 * SpEL expression that will evaluate whether to include a service in gateway integration or not,
	 * defaults to: true
	 */
	private String includeExpression = "true";

	/** SpEL expression that create the uri for each route, defaults to: 'lb://'+serviceId */
	private String urlExpression = "'lb://'+serviceId";

	/**
	 * Option to lower case serviceId in predicates and filters, defaults to false.
	 * Useful with eureka when it automatically uppercases serviceId.
	 * so MYSERIVCE, would match /myservice/**
	 */
	private boolean lowerCaseServiceId = false;

	private List<PredicateDefinition> predicates = new ArrayList<>();

	private List<FilterDefinition> filters = new ArrayList<>();
}
Copy the code

The article source reference address: https://github.com/yinjihuan/spring-cloud/tree/master/fangjia-gateway