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 to build Spring Cloud Gateway

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to build Spring Cloud Gateway". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to build Spring Cloud Gateway" can help you solve the problem.

Building a server

Use Spring Boot to build a simple Web application. After the outside world sends a request to the gateway, it will be forwarded to the application. The pom.xml file contains the following contents:

org.springframework.boot spring-boot-starter-parent 2.0.2.RELEASE org.springframework.boot spring-boot-starter-web

Write startup classes and controllers that provide a "hello" service:

@SpringBootApplication@RestControllerpublic class ServerApp { public static void main(String[] args) { SpringApplication.run(ServerApp.class, args); } @GetMapping("/hello") public String hello() { System.out.println("call hello method"); return "hello"; }}

The hello method in ServerApp will return the hello string. Start ServerApp and use port 8080 by default. Visit http://localhost:8080/hello in the browser to see the browser output results.

Building Gateway

Create an ordinary Maven project, add Spring Cloud Gateway dependencies, pom.xml content is as follows:

org.springframework.boot spring-boot-starter-parent 2.0.0.RELEASE org.springframework.cloud spring-cloud-dependencies Finchley.RC1 pom import org.springframework.cloud spring-cloud-starter-gateway

Add the configuration file application.yml to the gateway project, modify the server port to 9000, and the configuration file content is as follows:

server: port: 9000

Add the startup class and configure a router locator bean. The code is as follows:

@SpringBootApplicationpublic class RouterApp { public static void main(String[] args) { SpringApplication.run(RouterApp.class, args); } @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { Function fn = new Function() { public Route.Builder apply(PredicateSpec t) { t.path("/hello"); return t.uri("http://localhost:8080"); } }; return builder.routes().route(fn).build(); }}

In the above code, use RouteLocatorBuilder bean in Spring container to create route locator. When calling route method of Builder, pass java.util.function.Function instance, which is one of the functional interfaces added by Java8. We can use functional programming to implement the above code. The following code is equivalent to the previous code:

@Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route(t -> t.path("/hello") .and() .uri("http://localhost:8080")) .build(); }

The above two codes set a routing rule. When the browser accesses the http://localhost:9000/hello address of the gateway, it will route to http://localhost:8080/hello.

In addition to routing to port 8080 in our example, you can also route to other websites by changing the uri of PredicateSpec, for example, changing.uri("http://localhost:8080") to.uri("http://www.163.com").

The content of "How to build Spring Cloud Gateway" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

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

Servers

Wechat

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

12
Report