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

Spring Cloud getting started tutorial-Zuul implementation of API Gateway and request filtering

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Brief introduction

Zuul is an api gateway and filtering component provided by Spring Cloud, which provides the following functions:

Authentication filtering stress test Canary test dynamic routing service migration load balancing security static request processing dynamic traffic management

In this tutorial, we will use zuul to forward the request / product on the web side to the corresponding product service, and define a pre filter to verify that it has been forwarded by zuul.

Basic environment JDK 1.8Maven 3.3.9IntelliJ 2018.1Git project source code

Gitee Code Cloud

Create a Zuul service

Create a maven project in IntelliJ:

Cn.zxuqianapiGateway

Then add the following code to pom.xml:

4.0.0 cn.zxuqian apiGateway 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent 2.0.1.RELEASE org.springframework.cloud spring-cloud-starter-netflix-zuul org.springframework.cloud spring-cloud-starter-netflix-eureka-client Org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-web org.springframework.cloud Spring-cloud-dependencies Finchley.M9 pom import 1.8 org.springframework.boot spring-boot-maven-plugin Spring-milestones Spring Milestones https://repo.spring.io/libs-milestone false

It is important to note that the tutorials on the Spring website give zuul artifactId spring-cloud-starter-zuul, which is the name of the old version of zuul, which has been renamed spring-cloud-starter-netflix-zuul in our Finchley.M9 version.

Add a src/main/resources/bootstrap.yml file and specify spring.application.name:

Spring: application: name: zuul-server

Create the cn.zxuqian.Application class:

Package cn.zxuqian;import cn.zxuqian.filters.PreFilter;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.netflix.zuul.EnableZuulProxy;import org.springframework.context.annotation.Bean;@EnableZuulProxy@EnableDiscoveryClient@SpringBootApplicationpublic class Application {public static void main (String [] args) {SpringApplication.run (Application.class, args) @ Bean public PreFilter preFilter () {return new PreFilter ();}}

Here @ EnableZuulProxy is used to specify the reverse proxy that uses zuul to forward our request to the corresponding server. Service discovery for eureka is then enabled. Zuul also uses Ribbon as load balancer by default, so you can discover registered services through eureka. PreFilter is a pre-filter used to perform some operations before the request request is processed. Its code is as follows:

Package cn.zxuqian.filters;import com.netflix.zuul.ZuulFilter;import com.netflix.zuul.context.RequestContext;import com.netflix.zuul.exception.ZuulException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.servlet.http.HttpServletRequest;public class PreFilter extends ZuulFilter {private static Logger log = LoggerFactory.getLogger (PreFilter.class); @ Override public String filterType () {return "pre";} @ Override public int filterOrder () {return 1 } @ Override public boolean shouldFilter () {return true;} @ Override public Object run () throws ZuulException {RequestContext ctx = RequestContext.getCurrentContext (); HttpServletRequest request = ctx.getRequest (); log.info (String.format ("% s request% s", request.getMethod (), request.getRequestURL () .toString (); return null;}}

There are four built-in filter types in filterType-Zuul, pre and route,post,error, which represent before request processing, during processing, after processing and after error, respectively.

FilterOrder-specifies the order in which the filter is executed.

ShouldFilter-whether to turn on this filter.

Run-the business logic of the filter. Here is just a simple log about the reqeust request method and the request path.

Next, create the zuul-server.yml file in the git repository in our configuration center and add the following configuration:

Server: port: 8083zuul: routes: products: path: / product/** serviceId: product-service

Here we configure zuul with port 8083, and then map all / product/ requests to our product-service service. If you do not configure serviceId, then the products Key will default to ServiceId, and in our example, ServiceId includes -, so the specified ServiceId is shown below. Submit to git when the configuration is complete.

Update productService

ProductService's uri has been changed a little to make it more rest-style:

@ RequestMapping ("/ list") public String productList () {log.info ("Access to / products endpoint"); return "jacket, jacket, sweater, T-shirt";}

Here the path to the @ RequestMapping match has been changed to / list, which was / products before.

Update web client

Add a new method to the ProductService of our web client:

Public String productListZuul () {return this.restTemplate.getForObject ("http://zuul-server/product/list", String.class);}

This time we request the zuul-server service directly, and it then reflects our request to the product-service service. Finally, add a request handling method to ProductController:

@ RequestMapping ("/ product/list") public String productListZuul () {return productService.productListZuul ();}

Used to process the / product/list request and then call the method in the ProductService class.

test

Use mvn spring-boot:run to start the configServer,registry, zuulServer, productService,web projects, and then start the second productService, using SERVER_PORT=8082 spring-boot:run.

Visit http://localhost:8080/product/list several times, and then, in addition to seeing the returned result in the browser, we will also see the following words in the command line window of zuulServer:

Request http://xuqians-imac:8083/product/list in GET mode

Then in the two command line windows of productService, we will also see the random

Access to / products endpoint

This indicates that zuulServer also automatically performs load balancing.

Welcome to my blog Zhang Xuqian's blog

If you have any thoughts, welcome to discuss.

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