Why to use Zuul
Let's take a look at the diagram below.
If we now have four microservices, which are users, orders, payments, and collection microservices, they are invoked using HTTP, restful, thrift, Kafka, respectively. This time if we call the client directly in the sense is not too much effort, the client needs to maintain these methods of invocation of the client, if the late addition of micro-services or reduce or replace the call mode, etc., need to modify the client. So let's see what it's like after joining Zuul.
Zuul is responsible for all the calls, decoupling the client and microservices. It is also in line with our single principle of responsibility for design.
Using Zuul
First of all, we added a Cloud-demo-zuul module under the original parent project to introduce a dependency
<Dependency><groupid>org.springframework.cloud </groupid> <artifactId> Spring-cloud-starter-netflix-eureka-server</artifactId>< Span class= "Hljs-tag" ></dependency>< dependency> <groupId> Org.springframework.cloud</groupid> <artifactid>spring-cloud-starter-netflix-zuul </artifactid></ DEPENDENCY>
Create the package Cn.org.zhixiang, under this package, new startup class Clouddemozuulapplication
@SpringBootApplication@EnableZuulProxypublic class Clouddemozuulapplication { springapplication. Run (Clouddemozuulapplication.class, args); }}
@EnableZuulProxy is the annotation that opens Zuul
Now look at application.yml.
Spring: application: zuul-demoserver:8093Eureka: client:true Service-url:http://root:[email protected]:8761/eureka
This configuration is in fact with our previous service consumer configuration is the same, why this configuration, because before we need to review the previous knowledge points, have not seen the previous blog's small partners can refer to this: https://zhixiang.org.cn
Start our Eureka service first, and then start a provider Cloud-demo-provider. We know this time we have direct access to HTTP://LOCALHOST:8078/USER/GETUSER/2 to get data, right, so how does my zuul use it? Let's start our Zuul.
This time we access HTTP://LOCALHOST:8093/PROVIDER-DEMO/USER/GETUSER/2 the same data, that is to say, our Zuul did a route forwarding. We can see that there is a provider-demo in this URL, This is the value of the provider's spring.application.name, so if we don't want to use this, for example now I know this is the user micro service, I think if I access the user microservices I will use Http://localhost:8093/user What about/USER/GETUSER/2?
Configuration
We need to add a configuration to the yml.
Zuul: routes: /user/**
This configuration is to map the service ID Provider-demo's MicroServices project address to/user/**. Note the last is two *, why is two, if it is a * can only match/user/a,/user/b this path, and not match/user/a/b this. And we have two * words can not only match/user/a/b, but also can match/user/a/b/c/d and so on.
We can get the data when we visit HTTP://LOCALHOST:8093/USER/USER/GETUSER/2 at this time.
In fact, there are other formulations. Let's take a look.
1.
Zuul: routes: Demo: /user/** serviceid:provider-demo
2.
Zuul: routes: Demo: /user/** url:http://localhost:8078/
The above two ways are OK, the demo is actually custom, as long as it is not repeated on the line. Then path is the URL we visit on the page. These two ways are to map this brother path to Serviceid or a URL, respectively.
Load Balancing
Just now we are talking about a single service situation, so what should we do when we have more than one micro-service?
Zuul: routes: Demo: /user/** serviceid:provider-demoprovider-demo: Ribbon: listofservers:http://localhost:8078,http://localhost:8079
In this way, we start our original Cloud-demo-provider-2 project, and then start Zuul access and find that load balancing has been achieved
File Upload
We use Zuul to do the route may encounter the need to upload files, upload small files to is OK, but if the large file is not able to upload it, here there is a way to bypass the Springmvc Dispatchservlet.
For example, we upload the file interface is Http://localhost:8093/user/user/uploadFile then we can upload the time we have access to http://localhost:8093/zuul/user/user/ UploadFile to implement uploading large files
Github:https://github.com/2388386839/spring-cloud-demo
Code Cloud: Https://gitee.com/zhixiang_blog/spring-cloud-demo
If you have any help, please remember to help a star oh.
This article is from https://zhixiang.org.cn/#/blog/read/ae383e8b-93b9-4c52-9987-bb6801e87301, reproduced please retain.
Learn some springcloud every day (vii): Routers and Filters-zuul