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 configure API Gateway Service Zuul in Spring Cloud

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "how to configure API gateway service Zuul in Spring Cloud". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to configure API gateway service Zuul in Spring Cloud".

Build a gateway

The construction of the gateway is realized through the following three steps.

1. Create a Spring Boot project and add dependencies

First, we create a common Spring Boot project named api-gateway, and then add related dependencies. Here we mainly add two dependencies, spring-cloud-starter-zuul and spring-cloud-starter-eureka,spring-cloud-starter-zuul, which include ribbon, hystrix, actuator, and so on, as follows:

Org.springframework.boot spring-boot-starter-parent 1.5.7.RELEASE UTF-8 UTF-8 1.8 Dalston.SR3 org.springframework.boot spring-boot-starter org.springframework.cloud spring-cloud-starter-zuul org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud Spring-cloud-dependencies ${spring-cloud.version} pom import 2. Add comment

Then add the @ EnableZuulProxy annotation to the entry class to enable Zuul's API gateway service, as follows:

@ SpringBootApplication@EnableZuulProxypublic class ApiGatewayApplication {public static void main (String [] args) {SpringApplication.run (ApiGatewayApplication.class, args);}} 3. Configure routing rules

The configuration in the application.properties file can be divided into two parts, one is the basic information of the Zuul application, and the other is the routing rules, as follows:

# basic Information configuration spring.application.name=api-gatewayserver.port=2006# routing rules configuration zuul.routes.api-a.path=/api-a/**zuul.routes.api-a.serviceId=feign-consumer# API Gateway will also be registered with eureka.client.service-url.defaultZone= http://localhost:1111/eureka/ on eureka-server as a service

We have configured the routing rule here that all requests that meet the / api-a/** will be forwarded to the feign-consumer service. As for what the address of the feign-consumer service is, it is up to eureka-server to analyze it. We only need to write the service name here. Taking the above configuration as an example, if I request the http://localhost:2006/api-a/hello1 interface, it is tantamount to requesting http://localhost:2005/hello1(. The address of the feign-consumer here is http://localhost:2005). The api-a we configured in the routing rule is the name of the route, which can be defined arbitrarily, but the routing names of a set of path and serviceId mappings should be the same.

OK, after doing this, we start our eureka-server, provider, and feign-consumer in turn, and then visit the following address: http://localhost:2006/api-a/hello1

Seeing this effect shows that our API gateway service has been built successfully, and the requests we sent in accordance with the routing rules are automatically forwarded to the corresponding service for processing.

Request filtering

Now that the gateway is built, let's take a look at how to use the gateway to achieve a simple permission verification. This involves another core function in Spring Cloud Zuul: request filtering. Request filtering is a bit like the Filter filter in Java, where all requests are intercepted and then processed differently according to the situation on the spot. Let's take a look at how the filter in Zuul can be used. It's simple, two steps:

1. Define filter

First, we define a filter that inherits from ZuulFilter, as follows:

Public class PermisFilter extends ZuulFilter {@ Override public String filterType () {return "pre";} @ Override public int filterOrder () {return 0;} @ Override public boolean shouldFilter () {return true;} @ Override public Object run () {RequestContext ctx = RequestContext.getCurrentContext (); HttpServletRequest request = ctx.getRequest (); String login = request.getParameter ("login") If (login = = null) {ctx.setSendZuulResponse (false); ctx.setResponseStatusCode (401); ctx.addZuulResponseHeader ("content-type", "text/html;charset=utf-8"); ctx.setResponseBody ("illegal access");} return null;}}

I would like to make the following points about this category:

The return value of the 1.filterType method is the type of filter, which determines the lifecycle in which the filter is executed. Pre means that the filter is executed before routing. Other optional values are post, error, route, and static. Of course, you can also customize it.

The 2.filterOrder method represents the order in which filters are executed, which makes sense when there are many filters.

The 3.shouldFilter method is used to determine whether the filter is executed, true indicates execution, and false indicates no execution. In actual development, we can decide whether to filter the address based on the current request address. Here I return true directly.

The 4.run method indicates the specific logic of filtering. If the login parameter is carried in the request address, it is considered to be a legitimate request, otherwise it is an illegal request. If it is an illegal request, first set ctx.setSendZuulResponse (false); means that the request is not routed, and then set the response code and response value. The return value of this run method does not make any sense for the time being in the current version (Dalston.SR3) and can return any value.

two。 Configure filter Bean

Then configure the relevant Bean in the portal class, as follows:

@ BeanPermisFilter permisFilter () {return new PermisFilter ();} Thank you for reading. The above is the content of "how to configure the API gateway service Zuul in Spring Cloud". After the study of this article, I believe you have a deeper understanding of how to configure the API gateway service Zuul in Spring Cloud, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report