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--
This article is about how to use Zuul in Spring Cloud's Web project. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Using Zuul in Web projects about Zuul
Spring Cloud cluster provides several components for communication within the cluster, such as service management component Eureka, load balancing component Ribbon. If the cluster provides API or Web services and needs to communicate with the outside world, it is better to add a gateway to hide all the cluster services behind the gateway. In this way, for external clients, they do not need to care about the internal structure of the cluster, they only need to care about the location of the gateway and other information; for Spring Cloud clusters, there is no need to expose too many services, which improves the security of the cluster.
As the door of the application cluster, the agent layer is particularly important in the selection of technology. Many traditional solutions choose Nginx, Apache and other servers in software. Netflix provides its own solution: Zuul. Zuul is a sub-project of Netflix. Spring Cloud further implements and encapsulates Zuul and integrates it into the spring-netflix project to provide proxy, filtering, routing and other functions for micro-service clusters.
Web Project Integration Zuul
Create a new Maven project named "first-router" that uses the following dependencies:
Org.springframework.cloud spring-cloud-starter-zuul org.apache.httpcomponents httpclient 4.5.3
You need to add "spring-cloud-starter-zuul" dependencies, and since HttpClient is used at the bottom of Zuul, you also need to add corresponding dependencies. To enable the Web project to support Zuul, add the @ EnableZuulProxy annotation to the application class, as shown in listing 7-1.
Listing 7-1:
Codes\ 07\ 02\ first-router\ src\ main\ java\ org\ crazyit\ cloud\ GatewayApplication.java
@ EnableZuulProxy@SpringBootApplicationpublic class GatewayApplication {public static void main (String [] args) {new SpringApplicationBuilder (GatewayApplication.class) .properties ("server.port=8080") .run (args);}}
Notice that the startup port for this project is 8080. After completing the above work, a Web project with Zuul functionality has been set up, and next, its routing capabilities will be tested.
Test routing function
The router project has been established in the previous section, and then the source service project has been established. The structure of the test example is shown in figure 7-1.
Figure 7-1 Test example structure diagram
Create a new Maven project named "book-server", which is the most common Spring Boot project, using the following dependencies:
Org.springframework.boot spring-boot-starter-web 1.5.4.RELEASE
Add a "/ hello" service to "book-server", the startup class for the project, and the controller, as shown in listing 7-2.
Listing 7-2:codes\ 07\ 02\ book-server\ src\ main\ java\ org\ crazyit\ cloud\ BookApplication.java
@ SpringBootApplication@RestControllerpublic class BookApplication {@ RequestMapping (value = "/ hello/ {name}", method = RequestMethod.GET) public String hello (@ PathVariable String name) {return "hello" + name;} public static void main (String [] args) {new SpringApplicationBuilder (BookApplication.class). Properties ("server.port=8090") .run (args);}}
For simplicity, this example writes the startup class with the controller, and notice that the port of "book-server" is 8090. In the controller, a service of "/ hello/ {name}" is established, and the corresponding string is returned after a successful call. Next, modify the configuration file of the "first-router" project to forward it.
Modify the application.yml file of the "first-router" project by adding the following:
Zuul: routes: books: url: http://localhost:8090
After adding the above configuration, all requests sent to http://localhost:8080/books will be forwarded to port 8090, that is, to access the "first-router" project, which will actually eventually invoke the "book-server" service. Launch two applications, enter the following address in the browser: http://localhost:8080/books/hello/crazyit, and you can see the browser output as follows:
Hello crazyit
According to the output, the sent request has been forwarded to "book-server" for processing.
Filter operation mechanism
For the routing project in the previous example, we use the @ EnableZuulProxy annotation. When this annotation is enabled, when the Spring container is initialized, the relevant configuration of Zuul will be initialized, which includes a bean:ServletRegistrationBean of Spring Boot, which is mainly used to register Servlet. Zuul provides a ZuulServlet class that executes various Zuul filters (ZuulFilter) in the service method of Servlet. Figure 7-2 shows the life cycle of HTTP requests in ZuulServlet.
Figure 7-2 Lifecycle of HTTP request
When the service method of ZuulServlet receives the request, it executes the filter of the "pre" phase, then the filter of the "routing" phase, and finally the filter of the "post" phase. The filter of "routing" forwards the request to the "source service", which can be a third-party Web service or a Spring Cloud cluster service. When executing filters for the pre and routing phases, if an exception occurs, the "error" filter is executed. The HTTP request, HTTP response, status, and other data for the entire process are encapsulated in a RequestContext object, which will be described in later chapters.
Thank you for reading! This is the end of this article on "how to use Zuul in Spring Cloud's Web project". I hope the above content can be of some help to you, so that you can learn more knowledge. 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.
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.