In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you how to use SpringCloud Gateway, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
SpringCloud Gateway is the gateway service in the SpringCloud technology stack. This paper constructs a SpringCloud environment and develops a SpringCloud Gateway application to quickly experience the gateway service.
Environmental information
Operating system: win10 (64 bit)
JDK:1.8.0_181
Maven:3.5.0
Spring Cloud:Greenwich.SR
Source code download
If you do not plan to write code, you can also download the source code, address and link information of this battle from GitHub as shown in the following table:
Name Link Note Project Home Page https://github.com/zq2599/blog_demos the home page of the project on GitHub git warehouse address (https) https://github.com/zq2599/blog_demos.git the warehouse address of the project source code, https protocol git warehouse address (ssh) git@github.com:zq2599/blog_demos.git the warehouse address of the project source code, ssh protocol
There are multiple folders in this git project. The source code for this chapter is under the gatewaydemo folder, as shown in the red box below:
Overall design
The source code of this actual combat involves three applications: registry, service provider and gateway. Their relationship and business logic are as follows: the whole project is based on maven and adopts a parent-child structure. The parent project is called gatewaydemo, in which there are three modular, namely: eureka () registry, provider (service provider) and gateway (gateway). The structure presented on IDEA is shown in the following figure: when you are ready, start coding.
Create a parent project
Create a maven project named gatewaydemo with the following pom.xml content, which is a typical parent-child project where the pom,dependencyManagement node takes over version matching:
4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.6.RELEASE com.bolingcavalry gatewaydemo pom 1.0-SNAPSHOT eureak provider gateway 1.8 2.1.6.RELEASE 3.5 2.8.2 2.18.1 2.21. 0 Greenwich.SR2 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import
If you are creating a project with IDEA, IDEA may automatically create a src folder in the same directory as pom.xml, please delete it manually because it is not needed
Eureka project
Next, to create a registry, right-click the gatewaydemo folder and select "New-> Module":
Select Spring Initializr in the pop-up window, as shown below:
The next window fills in Group, Artifact (here is eureka), Version and other information. By default, you can complete the creation of the sub-project.
Modify the pom.xml of the new eureka module to the following. You can see that besides specifying the parent project, it also depends on spring-cloud-starter-netflix-eureka-server:
Gatewaydemo com.bolingcavalry 1.0-SNAPSHOT 4.0.0 eureak war eureak Maven Webapp org.springframework.cloud spring-cloud-starter-netflix-eureka-server ${project.artifactId} org.springframework.boot spring-boot-maven-plugin
Add a new application.yml file in the src\ main\ resources directory with the following contents, which is a common registry setting:
Spring: application: name: eurekaserver: port: 8080eureka: client: service-url: defaultZone: http://localhost:${server.port}/eureka/ fetch-registry: false register-with-eureka: false
There is only one java file, that is, the startup class, and the registry service is enabled by annotating EnableEurekaServer:
Package com.bolingcavalry.eureka;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class EurekaApplication {public static void main (String [] args) {SpringApplication.run (EurekaApplication.class, args);}}
This is the content of the registry eureka. Run EurekaApplication to start the service. The result of accessing port 8080 is as follows: now that the registry is ready, start writing the code for the service provider provider application.
Provider project
Create a sub-project under gatewaydemo named provider,pom.xml as follows. You can see that two dependencies, spring-boot-starter-web and spring-cloud-starter-netflix-eureka-client, are used to support web service and registration discovery respectively:
Gatewaydemo com.bolingcavalry 1.0-SNAPSHOT 4.0.0 provider org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-netflix-eureka-client ${project.artifactId} Org.springframework.boot spring-boot-maven-plugin
The configuration file application.yml is as follows, which specifies the registry address and its own port is 8081:
Eureka: client: serviceUrl: defaultZone: http://localhost:8080/eureka/server: port: 8081spring: application: name: provider
Launch class ProviderApplication.java:
Package com.bolingcavalry.provider;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ProviderApplication {public static void main (String [] args) {SpringApplication.run (ProviderApplication.class, args);}}
Add a controller to respond to the web request. Note that the hello method takes the property value named extendtag from the header of the request and returns it to the browser:
Package com.bolingcavalry.provider.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest;import java.text.SimpleDateFormat;import java.util.Date @ RestController@RequestMapping ("/ hello") public class HelloController {@ RequestMapping (value = "time", method = RequestMethod.GET) public String hello (HttpServletRequest request) {return "hello," + new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss") .format (new Date ()) + ", extendtag [" + request.getHeader ("extendtag") + "]";}}
Launch the application and refresh the eureka page localhost:8080 again. You can see that the provider application has been registered, as shown in the red box below:
Access address: http://localhost:8081/hello/time, which is the web service interface provided by controller. The response is shown below. Because there is no property named "extendtag" in header, null is returned: the provider providing the service is already OK, and the gateway service can be developed.
Gateway project
Create a sub-project under gatewaydemo named gateway,pom.xml as follows. You can see that two dependencies, spring-cloud-starter-gateway and spring-cloud-starter-netflix-eureka-client, are used to support gateway service and registration discovery respectively:
Gatewaydemo com.bolingcavalry 1.0-SNAPSHOT 4.0.0 gateway org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.cloud spring-cloud-starter-gateway ${project.artifactId} Org.springframework.boot spring-boot-maven-plugin
The configuration file application.yml is as follows, which specifies the address of the registry, has its own port 8082, and enables the gateway service:
Server: port: 8082spring: application: name: gateway cloud: gateway: discovery: locator: enabled: true lowerCaseServiceId: trueeureka: client: service-url: defaultZone: http://localhost:8080/eureka/
Start the class GatewayApplication .java. You can see that an RouteLocator is instantiated. This instance is the routing rule. For specific functions, please refer to the comments in the code:
Package com.bolingcavalry.gateway;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.context.annotation.Bean;@SpringBootApplicationpublic class GatewayApplication {public static void main (String [] args) {SpringApplication.run (GatewayApplication.class, args) } @ Bean public RouteLocator customRouteLocator (RouteLocatorBuilder builder) {return builder.routes () / / add a path match where requests starting with "/ gateway/hello/" are routed .route (r-> r.path ("/ customize/hello/**") / / means to delete the first-level parameter in the path Use the rest of the path to join with the path of provider. / / this is "lb://provider/hello/" Can match the path of the HelloController to provider (f-> f.stripPrefix (1) / / add a key&value .addRequestHeader ("extendtag", "geteway-" + System.currentTimeMillis ()) / / specify the matching service provider in the requested header Lb means load balance. Uri ("lb://provider"). Build () }}
Launch the application and refresh the eureka page localhost:8080 again. You can see that the gateway application has been registered, as shown in the red box below:
Access address: http://localhost:8082/customize/hello/time, this is the path that conforms to the routing rules we configured earlier. After the customize is deleted, the remaining path is forwarded to the provider service, so the real address of the request is the / hello/time of the provider service. The response is shown in the figure below. Because gateway sets the attribute named "extendtag" to the header when forwarding, the extendtag is returned with content:
The above is all the contents of this article "how to use SpringCloud Gateway". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.