In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
SpringCloud distributed Micro Services Cloud Architecture part 5: routing Gateway (zuul) (Finchley version)
In the micro-service architecture, several basic service governance components are required, including service registration and discovery, service consumption, load balancing, circuit breakers, intelligent routing,
Configuration management, etc., understanding the springcloud architecture can be added: 3536247259, which is formed by the cooperation of these basic components.
A simple micro-service system. A microservice system with a short answer is shown below:
Note: a service and B service can be called each other, and the configuration service is registered with the service registry.
In Spring Cloud micro-service system, a common load balancing method is that the client requests first go through the load balancing (zuul, Ngnix).
Then to the service gateway (zuul cluster), and then to the specific service to register with the highly available service registry cluster, all the configuration files of the service.
Managed by the configuration service (described in the next article), the configuration file for the configuration service is placed in the git repository, making it easy for developers to change the configuration at any time.
A brief introduction to Zuul
The main functions of Zuul are route forwarding and filtering. Routing functions are part of micro services, such as / api/user forwarding to user services, / api/shop
Forward to the shop service. Zuul combines with Ribbon to achieve load balancing by default.
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:
4.0.0 com.forezp service-zuul 0.0.1-SNAPSHOT jar service-zuul Demo project for Spring Boot com.forezp sc-f-chapter5 0.0.1-SNAPSHOT org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot- Starter-web org.springframework.cloud spring-cloud-starter-netflix-zuul
Add @ EnableZuulProxy to the entry applicaton class to enable the function of zuul:
@ SpringBootApplication@EnableZuulProxy@EnableEurekaClient@EnableDiscoveryClientpublic class ServiceZuulApplication {public static void main (String [] args) {SpringApplication.run (ServiceZuulApplication.class, args);}}
Add the configuration file application.yml plus the following configuration code:
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, specify that the address of the service registry is http://localhost:8761/eureka/, the port of the service is 8769, and the service name service-zuul; is forwarded to the service-ribbon service if it starts with / api-a/; and all requests starting with / api-b/ are forwarded to the service-feign service.
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
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.