[Spring Cloud]-Zuul Practice (1)-Basic Use

Posted May 27, 20205 min read

basic knowledge

Zuul definition:Microservice gateway-the middle layer between client and server
Occurrence reasons:There are many problems in the direct communication between the client and each microservice, for example:the client will request different services multiple times-there is client complexity, there are cross-domain requests-in some scenarios, it is a bit complicated to handle, authentication Problem-each microservice needs to be individually authenticated, and it is difficult to reconstruct-the splitting or merging of microservices is more difficult to implement(relying on the client to communicate directly with the microservices), the security of microservices, etc.
Advantages: Easy to monitor, easy to authenticate, reduce the direct interaction between the client and microservices
Core function:(just look, it can be understood as a streamlined version of Nginx) Image.png

When it comes to Zuul, I have to mention that its developer, Netflix(Netflix), few people will ask, why in the Spring Cloud family bucket, Netflix is ​​leading the trend in microservices module management? Why did Netflix develop Eureka, Zuul, fuse and other services? Rather than other large-scale pure IT companies?

For our developers, this kind of problem still needs to ask more questions-why video sites have led the trend of microservices, why Google has made k8s, why Redhat has made Open shift and so on. Whether it's domain-driven design or business construction needs, developers still have to think more about the world outside the code, solutions from all walks of life, optimal solutions for different businesses, and so on.

Many people on the Internet gave reasons for why Netflix came up with this set of solutions. The excerpts are as follows:

  1. First of all, Netflix is ​​a video-making website. It can be said that the American drama on this website should be the hottest.

  2. Netflix is ​​a company without a CTO. It is precisely such an organizational structure that enables products and technologies to communicate seamlessly, thereby enabling rapid iteration of better products. At the time of software agile development, Netflix's update rate was no less than that of WeChat background changes. Although WeChat developed later than Netflix, the grayscale release and agile development of WeChat should be the most fierce in the industry.

  3. Netflix has a very large number of visits due to video production, which prompts the rapid development of its technology to be supported behind it. This is also the case. Netflix began to migrate the overall system to microservices.

  4. Netflix's microservices are not the earliest, but they are indeed the largest attempts at production-level microservices. It is also this large-scale production-level attempt to rely on the AWS cloud for server operation and maintenance. Of course, the AWS cloud also benefits from the continuous growth of Netflix's large-scale business.

  5. Netflix's large-scale application of microservices has unreservedly opened up a whole set of microservice architecture core technology stack, called Netflix OSS, which is exactly the same, relying on the power of the open source community in technology Growth.

  6. Spring Cloud is the core of building microservices, and Spring Cloud is developed based on Spring Boot.

  7. Pivotal made a series of packages at the same time as a whole set of core technology product lines open sourced by Netflix, and it became Spring Cloud; although Spring Cloud has not only integrated the solutions provided by Netflix until now, there are many solutions. , But Netflix is ​​the most mature.
    Source:What is Netflix and how is it related to Spring Cloud

You can also refer to the official Netflix article: How We Use Zuul At Netflix

Gossip less, start the topic:

Basic usage of Zuul

Zuul's knowledge system depends on Eureka. If you have n’t been in touch with Eureka, I suggest you learn about it first. You can refer to another article(super simple): \ [Spring Cloud ]-Eureka creation and use

  1. Add dependencies-three parts:Spring Cloud version declaration, zuul, eureka client

     <properties>
         <java.version> 1.8 </java.version>
         <spring-cloud.version> Hoxton.SR4 </spring-cloud.version>
     </properties>
     <dependency>
         <groupId> org.springframework.cloud </groupId>
         <artifactId> spring-cloud-starter-netflix-zuul </artifactId>
     </dependency>
     <dependency>
         <groupId> org.springframework.cloud </groupId>
         <artifactId>
             spring-cloud-starter-netflix-eureka-client
         </artifactId>
     </dependency>
    
     <dependencyManagement>
         <dependencies>
             <dependency>
                 <groupId> org.springframework.cloud </groupId>
                 <artifactId> spring-cloud-dependencies </artifactId>
                 <version> ${spring-cloud.version} </version>
                 <type> pom </type>
                 <scope> import </scope>
             </dependency>
         </dependencies>
     </dependencyManagement>
  2. Start class add @ EnableZuulProxy-Because this annotation already contains the EnableEurekaClient annotation, so ** no need to add @EnableEurekaClient

  3. Add this application name and Eureka server host to application.yml:

     spring:
       application:
         name:zuulservice
    
     eureka:
       client:
         service-url:
           defaultZone:http://localhost:8761/eureka
  4. Just start

In this way, one of the simplest Zuul service is completed.
However, zuul has not yet obtained the client service, that is, other microservices, so it is currently not possible to make different jumps. If you use the browser to access the homepage of the zuul project you just created, you will find that there is nothing. Boot's Whitelabel Error Page.

So in the next step we need to register several demo services(ie service providers) to the eureka server.

Add service provider(service provider)

This step is very simple, you can refer to my other document \ [Spring Cloud ]-Eureka creation and use to create eureka client steps.
In this practice, we create two eureka clients.

Note:When creating Eureka Client, be sure to specify application.name in the configuration file. Otherwise, zuul cannot find the service.

After the creation is complete, visit the Eureka server homepage, similar to the following:
image.png

Current project structure:

service service name url
zuul zuulgateway localhost:8088
server1 server1 localhost:8081
server2 defaultservice localhost:8082
eureka server N/A localhost:8761

Note:The service name displayed in the Eureka interface is uppercase, but the name when configured in the actual service is lowercase. Note here that it will be mentioned below.

At this point, you can access other services through zuul.
The format of access is:[ http://zuulhost/servicename /]( http://zuulhost/servicename /)

For example, to access server1, the url is localhost:8088/server1
image.png

Visit defaultservice:
image.png