SpringCloud Learning Series -zuul routing gateway

zuul routing gateway

  1. What is

    Zuul request contains routing and filtering two main features:
    wherein the routing function is responsible for forwarding the request to the external micro-specific service instance, is the basis of uniform external access inlet and the filter function is responsible for handling the request intervene in the process, is the basis of requesting a check, and other functions of the service aggregator.

    Zuul integration and Eureka, Zuul will register itself as service management applications in Eureka, while obtaining information from the other widgets and services in Eureka, i.e. after access services are obtained by micro Zuul after the jump.

    Note: Zuul service will eventually enrolled at Eureka

    provides routing + + = proxy filter three functions

    2. Why can

     Routing, filtering

   Information on the official website: https: //github.com/Netflix/zuul/wiki/Getting-Started

   3. Experiment code - Basic Routing Configuration

    1. Create a new module Module microservicecloud-zuul-gateway-9527

    2. Modify Pom

    

 
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <parent>
   <groupId>com.atguigu.springcloud</groupId>
   <artifactId>microservicecloud</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </parent>
 
  <artifactId>microservicecloud-zuul-gateway-9527</artifactId>
 
  <dependencies>
   <!-- zuul路由网关 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-zuul</artifactId>
   </dependency> 
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
   </dependency>
   <!-- actuator监控 -->
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
   </dependency>
   <!--  hystrix容错-->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-hystrix</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
   </dependency>
   <!-- 日常标配 -->
   <dependency>
     <groupId>com.atguigu.springcloud</groupId>
     <artifactId>microservicecloud-api</artifactId>
     <version>${project.version}</version>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jetty</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-test</artifactId>
   </dependency>
   <!-- 热部署插件 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>springloaded</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-devtools</artifactId>
   </dependency>
  </dependencies>
 
</project>

 

  3. Modify ym

   

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway
 
eureka: 
  client: 
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka  
  instance:
    instance-id: gateway-9527.com
    prefer-ip-address: true 
 
 
info:
  app.name: atguigu-MicroCloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

4.hosts modify 127.0.0.1 myzuul.com

The master boot class

package com.atguigu.springcloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
 
@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp
{
  public static void main(String[] args)
  {
   SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
  }
}
 

7. Start

  Three clusters eureka

 A service provider class microservicecloud-provider-dept-8001

A route

8. Test

  Without routing: http: // localhost: 8001 / dept / get / 2

  Enable Routing: http: //myzuul.com: 9527 / microservicecloud-dept / dept / get / 2

 4. Experiment code - the access route mapping rule

   1. Project microservicecloud-zuul-gateway-9527

   2. Agent Name

    YML

   

 
before
http://myzuul.com:9527/microservicecloud-dept/dept/get/2
 
 
zuul: 
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**
 
after
http://myzuul.com:9527/mydept/dept/get/1
 
 
 
 

  In this case the problem

   Routing Access OK: http: //myzuul.com: 9527 / mydept / dept / get / 1

   The original path access OK: http: //myzuul.com: 9527 / microservicecloud-dept / dept / get / 2

3. Ignore the original real service name:

  YML

  

 
zuul: 
  ignored-services: microservicecloud-dept 
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**
 

Single Specifically, a plurality may be "*"

zuul: 
  ignored-services: "*"
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**

5. Set the unified common prefix

YML

 
zuul: 
  prefix: / atguigu
  ignored-services: "*"
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**
 

http://myzuul.com:9527/atguigu/mydept/dept/get/1

Finally yml

server: 
  port: 9527
 
spring: 
  application:
    name: microservicecloud-zuul-gateway
 
zuul: 
  prefix: / atguigu
  ignored-services: "*"
  routes: 
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**
 
eureka: 
  client: 
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka  
  instance:
    instance-id: gateway-9527.com
    prefer-ip-address: true 
 
info:
  app.name: atguigu-MicroCloud
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
 

Guess you like

Origin www.cnblogs.com/XiangHuiBlog/p/12101198.html