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

Example Analysis of Service Gateway Zuul in Spring Cloud

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the example analysis of the service gateway Zuul in Spring Cloud, which is very detailed and has a certain reference value. Interested friends must read it!

In the micro-service architecture, several key components are required, such as service registration and discovery, service consumption, load balancing, circuit breaker, intelligent routing, configuration management, and so on. A simple micro-service architecture can be formed by these components, as shown below:

The request from the client first goes through the load balancer (zuul, Ngnix), and then to the service gateway (zuul cluster), and then to the specific service. The service is uniformly registered with the highly available service registry cluster. All the configuration files of the service are managed by the configuration service (described in the next article). The configuration file of the configuration service is placed in the git repository to facilitate developers to change the configuration at any time.

A brief introduction to Zuul

The main functions of Zuul are routing and filtering. Routing capabilities are part of micro services, such as / api/user mapping to user services and / api/shop mapping to shop services. Zuul implements load balancing.

Zuul has the following features:

Authentication

Insights

Stress Testing

Canary Testing

Dynamic Routing

Service Migration

Load Shedding

Security

Static Response handling

Active/Active traffic management

II. Preparatory work

Continue to use the project in the previous section. Create a new project on top of the original project.

Third, create a service-zuul project

The pom.xml file is as follows:

Org.springframework.cloudspring-cloud-starter-eureka

Org.springframework.cloudspring-cloud-starter-zuul

Add the annotation @ EnableZuulProxy to its entry applicaton class to open zuul:

@ EnableZuulProxy@EnableEurekaClient@SpringBootApplicationpublic class ServiceZuulApplication {public static void main (String [] args) {SpringApplication.run (ServiceZuulApplication.class, args);}}

Plus the configuration file:

Eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/server: port: 8769spring: application: name: service-zuulzuul: routes: api-a: path: / api-a/** serviceId: service-ribbon api-b: path: / api-b/** serviceId: service-feign

First register yourself with eureka. The port is 8769 and the service name is service-zuul;. All requests starting with / api-a/ point to service-ribbon; and all requests starting with / api-b/ point to service-feign.

Run the five projects in turn; open the browser to visit: http://localhost:8769/api-a/hi?name=forezp; the browser displays:

Hi forezp,i am from port:8762

Open the browser to access: http://localhost:8769/api-b/hi?name=forezp; the browser shows:

Hi forezp,i am from port:8762

This shows that zuul plays the role of routing.

IV. Service filtering

Zuul is not only a route, but also can filter and do some security verification. Continue the renovation project

@ Componentpublic class MyFilter extends ZuulFilter {private static Logger log = LoggerFactory.getLogger (MyFilter.class); @ 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 () Log.info (String.format ("% s > >% s", request.getMethod (), request.getRequestURL (). ToString ()); Object accessToken = request.getParameter ("token"); if (accessToken = = null) {log.warn ("token is empty"); ctx.setSendZuulResponse (false); ctx.setResponseStatusCode (401) Try {ctx.getResponse () .getWriter () .write ("token is empty");} catch (Exception e) {} return null;} log.info ("ok"); return null;}}

FilterType: returns a string that represents the filter type. Four filter types with different lifecycles are defined in zuul, as follows:

Pre: before routin

Routing: when routin

Post: after routin

Error: send error call

FilterOrder: order of filtering

ShouldFilter: here you can write logical judgment, whether to filter or not, this article true, always filter.

Run: the specific logic of the filter. It is complex to use, including checking sql,nosql to determine whether the request has permission to access or not.

Visit: http://localhost:8769/api-a/hi?name=forezp at this time; the web page shows:

Token is empty

Visit http://localhost:8769/api-a/hi?name=forezp&token=22; the web page shows:

Hi forezp,i am from port:8762

The above is all the contents of the article "sample Analysis of Service Gateway Zuul in Spring Cloud". Thank you for reading! Hope to share the content to help you, more related 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.

Share To

Internet Technology

Wechat

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

12
Report