Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How SpringCloud-Gateway integrates Eureka routing forwarding

2025-04-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how SpringCloud-Gateway integrates Eureka routing and forwarding. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Create a Gateway project

Create a Maven project for Spring Boot and increase the dependency on Spring Cloud Gateway, as shown below.

Org.springframework.bootspring-boot-starter-parent2.0.6.RELEASEorg.springframework.cloudspring-cloud-dependenciesFinchley.SR2pomimportorg.springframework.cloudspring-cloud-starter-gateway

Just start the class in the Spring Boot way, without adding additional comments. The code is as follows.

@ SpringBootApplicationpublic class App {public static void main (String [] args) {SpringApplication.run (App.class, args);} routing forwarding example

Let's implement one of the simplest forwarding functions-matching forwarding based on Path.

The routing configuration of Gateway supports yml files better. We create an application.yml file under resources, as follows:

Server: port: 2001spring: cloud:gateway: routes:- id: path_routeuri: http://c.biancheng.netpredicates:-Path=/spring_cloud

When you visit http://localhost:2001/spring_cloud, it will be forwarded to http://c.biancheng.net/spring_cloud.

If we want to support multi-level Path, configure it in the same way as in Zuul, and add two * signs after it, for example:

-id: path_route2uri: http://c.biancheng.netpredicates:-Path=/spring_cloud/**

In this way, the above configuration can support multi-level Path, such as forwarding to http://c.biancheng.net/spring_cloud/view/1 when accessing http://localhost:2001/spring_cloud/view/1.

Integrate Eureka routin

Add a dependency for Eureka Client, as shown in the following code.

Org.springframework.cloudspring-cloud-starter-netflix-eureka-client

Configure Eureka-based routing:

-id: user-serviceuri: lb://user-servicepredicates:-Path=/user-service/**

Uri starts with lb:// (lb means to obtain a service from the registry), 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. The error code is as follows:

Org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for user-service1 integrates the default route of Eureka

Zuul forwards all services by default. We only need to specify the service to be accessed on the access path. In this way, there is no need to configure forwarding rules for each service. When new services are added, there is no need to configure routing rules and restart the gateway.

This feature is also available in Spring Cloud Gateway, which can be enabled through configuration. The configuration is as follows:

Spring: cloud:gateway: discovery:locator: enabled: true

After enabling it, we can access the service through the address, in the following format:

Http:// gateway address / service name (uppercase) / * * http://localhost:2001/USER-SERVICE/user/get?id=1

This uppercase name still has a great impact. If we upgrade from Zuul to Spring Cloud Gateway, it means that the request address is changed, or the routing address of each service is reconfigured. Through the source code, the author finds that it can be compatible, and then add another configuration:

Spring: cloud:gateway: discovery:locator: lowerCaseServiceId: true

After the configuration is complete, we can access it through a lowercase service name, as shown below:

Http:// gateway address / service name (lowercase) / * * http://localhost:2001/user-service/user/get?id=1

Note: uppercase service names cannot be used when lowercase service names are enabled, and you can only choose between the two.

The configuration source code is in the org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties class, as shown in the code.

@ ConfigurationProperties ("spring.cloud.gateway.discovery.locator") public class DiscoveryLocatorProperties {/ * service name lowercase configuration. Default is false * * / private boolean lowerCaseServiceId = false;}. This is the end of how SpringCloud-Gateway integrates Eureka routing and forwarding. I hope the above content can be helpful to you and you can learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report