Explore spring cloud series Chapter 6: creating Zuul intelligent routing Palace

Posted by vcarter on Sat, 22 Feb 2020 08:26:41 +0100

Welcome to spring cloud. In this chapter, we will teach you how to create the parent-child project architecture of spring cloud.
Knowledge is endless, and stories are good or bad. Articles are purely fictions. Make complaints about them.
How can I walk in the Jianghu without any tricks. The technologies and tools involved in this chapter are as follows:
Intellij Idea 2018.1
MAVEN 3.2.2
SpringBoot 1.5.13.RELEASE
Spring-Cloud Edgware.SR3
 Zuul gateway

Why use service gateway

Suppose Zhang San goes to the tavern. First, he needs to call the user's microservice to get Zhang San's information, and then he needs to call the tavern's microservice to get the tavern's information.

  • In this way, two microservices need to be called. If the business is more complex, more microservices need to be called. Increases the complexity of the client.
  • If there is a cross domain request, it is relatively complex to process in a certain scenario
  • When the system needs to be authenticated, each micro service needs to be authenticated separately.
  • It is not conducive to later refactoring. If the client directly communicates with the microservice, the refactoring will be relatively complex.
  • ...

After using the gateway, the architecture between the client and the microservice becomes as follows:

[failed to save the image in the external link. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-DQE9xMEF-1582355219709)(./images/zuul/zuul.png))
The client then accesses the gateway and invokes the service through the gateway.

  • Easy to monitor, all external calls are through the gateway, easy to monitor data by mobile phone
  • Easy authentication, security authentication can be performed on the micro service gateway without authentication on each micro service
  • Dynamic routing, dynamically forwarding the request to the corresponding microservice
  • Current limiting and fusing control can be applied to the request
  • ...

Create zuul service

Then right click on the spring cloud Edgware project, new - > module, select spring initializer, and click Next.

Note that add this sub module in spring cloud Edgware's pom.xml: cloud zuul

  • groupId: com.maple
  • artifactId: cloud-zuul
  • version: 1.0.0

After creation, modify pom.xml file

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <description>zuul demo.</description>



Modify application.price to application.yml, and add the following configuration

  port: 9521

#Register service to Eureka server

Modify CloudZuulApplication.java to add @ EnableDiscoveryClient, @ EnableZuulProxy annotation

package com.maple.zuul;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

public class CloudZuulApplication {

    public static void main(String[] args) {
        SpringApplication.run(CloudZuulApplication.class, args);

Test zuul's route jump

Start the Eureka server, cloud zuul, user service and pub service programs.

Then visit: in the browser

The results are as follows:

In this way, the basic use of zuul is completed.

zuul's dynamic routing

  1. Customize the access path specified as a service

Configure zuul.route. Specify the serviceId of the microservice = specify the path. For example:

   user-service: /user/**

After setting, the user service microservice will be mapped to the / user / * * path.

  1. Ignore specified microservices
  ignored-services: user-service,pub-service

In this way, zuul ignores user service and pub service microservices and only proxy other microservices.

  1. Ignore all microservices and route only specified services
  ignored-services: '*' # '*' for all microservices
    user-service: /user/**

In this way, zuul only routes user service microservices.

  1. At the same time, specify the serviceId and the corresponding path of the microservice. For example:
    user-route:     # This configuration is only an alias, which can be named arbitrarily
      service-id: user-service
      path: /user/**     # Path corresponding to service ID
  1. Specify both path and URL, for example:
    user-route:     # This configuration is only an alias, which can be named arbitrarily
      url: specify url
      path: /user/**     # Path corresponding to service ID

This method does not use the Ribbon to load balance multiple URL s.

  1. Specify path and URL at the same time, and do not break the features of Hystrix and Ribbon of Zuul
    user-route:     # This configuration is only an alias, which can be named arbitrarily
      path: /user/**     # Path corresponding to service ID
      service-id: user-service
    enabled: false # Disable Eureka for Ribbon

7. Route prefix
Example 1:

 prefix: /api
 strip-prefix: false
   user-service: /user/**

In this way, access Zuul's / API / user service / 1 path, and the request will be forwarded to user service's / api/1.

Example 2:

      path: /user/**
      strip-prefix: false

In this way, access Zuul's / user/1 path, and the request will be forwarded to / user/1 of user service

8. Local forwarding

      path: /path-a/**
      url: forward:/path-b

In this way, access zuul's / path-a / * * path and forward it to zuul's / path-b / *.

This concludes this chapter. Subsequent articles will be updated one after another, and documents will be updated synchronously in CSDN and GitHub.

CSDN: https://blog.csdn.net/qq_34988304/category_8820134.html

Github documentation: https://github.com/hack-feng/Java-Notes/tree/master/src/note/SpringCloud

GitHub source code: https://github.com/hack-feng/Spring-Cloud-Edgware.git

63 original articles published, 32 praised, 110000 visitors+
Private letter follow

Topics: Spring Maven github xml