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 use zuul to realize Gateway function in Springcloud

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

Share

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

This article is about how to use zuul to implement the gateway function in Springcloud. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Personal classification: Spring Cloud

Editing

I. brief introduction

    uses zuul to implement the gateway function in Springcloud. The request from the client first goes through the load balancer Ngnix, then to the service gateway (zuul cluster), and then to the specific service. The main functions of Zuul are route forwarding and filtering. Routing capabilities are part of micro services, such as / api/server1 forwarding to server1 services. Zuul combines with Ribbon to achieve load balancing by default. Friends who are willing to understand the source code directly ask for communication and sharing technology: 2147775633

Second, set up

The     starts with the POM file

Org.springframework.cloud spring-cloud-starter-eureka org.springframework.cloud spring-cloud-starter-zuul org.springframework.boot spring-boot-starter-web

  then adds @ EnableZuulProxy to the applicaton class to turn on the function of zuul

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

The yml configuration is as follows

Eureka: client: serviceUrl: defaultZone: http://name:pass@IP/eureka/ instance: ip-address: Ip address prefer-ip-address: trueserver: port: 8769spring: application: name: service-zuul sleuth: sampler: percentage: 1.0cloud: config: discovery: enabled: true service-id: CONFIG-SERVER label: master profile: dev name: hfz-zuul username: name password: pass

The above   is configured in the project, in order to make the project more flexible, so put the configuration of the route on the github, so that you can read dynamically

Zuul: routes: api-a: path: / api-a/** serviceId: service-ribbon api-b: path: / api-b/** serviceId: service-feign

Requests that begin with / api-a/ are forwarded to the service-ribbon service; requests that begin with / api-b/ are forwarded to the service-feign service

III. Service filtering

Zuul can not only route, but also filter to intercept some services, which can be used for security verification.

Public class MyFilter extends ZuulFilter {private static Logger log = LoggerFactory.getLogger (MyFilter.class); @ Overridepublic String filterType () {return "pre";} @ Overridepublic int filterOrder () {return 0;} @ Overridepublic boolean shouldFilter () {return true;} @ Overridepublic 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: logical judgment, whether to filter or not

Run: specific logical control of the filter

Then you can test the access.

The technical architecture diagram is as follows: data and source code

Thank you for reading! This is the end of the article on "how to use zuul to achieve gateway function in Springcloud". 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.

Share To

Internet Technology

Wechat

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

12
Report