springcloud-zuul primary articles

I. Introduction

the central role zuul routing Gateway is a unified management for background services; due to micro service is deployed on multiple servers, ip address of the server and can not be unified, we need to expose a single ip address to call the front desk to use interface; zuul It is to play the role of a unified gateway router; look at Netflix uses Zuul on zuul used as follows:

  1. Authentication: rights seriously
  2. Insights: vision surveillance
  3. Stress Testing: Stress Test
  4. Canary Testing: Canary test
  5. Dynamic Routing: Dynamic Routing
  6. Service Migration: Migration Service
  7. Load Shedding: Load balancing cuts
  8. Security: Security Certification
  9. Static Response handling: Static response processing
  10. Active / Active traffic management: traffic management initiative

This article is the primary entry papers, take some pre-knowledge; If you are a beginner venue springcloud column knowledge seekers systematic study;

Two zuul-client

2.1 pom.xml

Create a new project zuul-client; spring-cloud-starter-netflix-eureka-clientrely on for service registration and discovery;

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

2.2 Presentation Layer

Presentation layer to provide a test interface for zuul-server routing services for unified management;

/**
 * @Author lsc
 * <p>zuul 路由网关测试表现层 </p>
 */
@RestController
public class ZuulController {

    @RequestMapping("/zszxz")
    public String getUser(String username){
        return "hello"+username;
    }
}

2.3 application.yml

  1. Designated port 8101
  2. Service Name zuul-client exposure
  3. eureka service registration
server:
  port: 8101

spring:
  application:
    name: zuul-client # 应用名称

eureka:
  client:
    service-url:
      # 服务注册地址
      defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/
  instance:
    prefer-ip-address: true

2.4 startup class

@EnableDiscoveryClient Eureka open for registration and discovery

/**
 * @Author lsc
 * <p> zuul-client启动类</p>
 */
@SpringBootApplication
@EnableDiscoveryClient//开启eureka注册与发现
public class ZuulApp {

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

Three zuul-server

3.1 pom.xml

Creating zuul-server project, the introduction of eureka, zuul dependence;

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
    </dependencies>

3.2 application.yml

  1. Designated port 8100
  2. Specify the name of zuul-server
  3. eureka service registration and discovery
  4. zuul routing rule set, zuul-client services / ** unified by the zuul routing management / api / **
server:
  port: 8100

spring:
  application:
    name: zuul-server # 应用名称

eureka:
  client:
    service-url:
      # 服务注册地址
      defaultZone: http://peer1:10081/eureka/,http://peer2:10082/eureka/,http://peer3:10083/eureka/
  instance:
    prefer-ip-address: true

zuul:
  routes:
    # zuul-client 路由规则
    zuul-client:
      path: /api/** # 转发路径
      serviceId: zuul-client # 服务id

3.3 startup class

@EnableZuulProxy open embedded routing agent;

/**
 * @Author lsc
 * <p>zuul-server </p>
 */
@SpringBootApplication
@EnableZuulProxy
public class ZuulServerApp {

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

Four test results

4.1 Start project list

  1. Were started three eureka-server (1-3), port 10081,10082,10083
  2. Start zuul-client
  3. Start zuul-server

4.1 zuul-client access

Visit http: // localhost: 8101 / zszxz username = zszxz that is normal interface testing?

4.2 Access zuul-server

Visit http: // localhost: 8100 / api / zszxz username = zszxz Gateway Routing test?

Concluded zuul-client service / ** All paths are mapped zuul-server service / api / ** path;

Five zuul common configuration

5.1 Routing simplifies configuration

Routing simplifies configuration requires strict accordance with the corresponding routing service id name is configured;

The original routing rules

zuul:
  routes:
    # zuul-client 路由规则
    zuul-client:
      path: /api/** # 转发路径
      serviceId: zuul-client # 服务id

Simplified routing rules as follows,

zuul:
  routes:
    zuul-client: /api/**

5.2 exclude routing configuration

In the daily development is inevitable that some internal service is not available to external access, but calls between the internal server then you can use routing configuration troubleshoot configuration; the following configuration will exclude all the services, in addition to zuul-client; of course readers also can not wildcard *, but the use of a specific service id;

zuul:
  ignoredServices: '*'
  routes:
    zuul-client: /api/**

5.3 ip address routing configuration using specific

In addition to using the service id routing configuration, you can also use the ip address configuration, but generally do not use this option;

zuul:
  routes:
    zuul-client:
      path: /api/**
      url: http://127.0.0.1:8101

5.4 zuul load balancing using ribbon

zuul commonly used are simple eureka own load balancing, and now want to use a ribbon load balancing, and support you need to perform the following configuration HystrixCommand

zuul:
  routes:
    zuul-client:
      path: /api/**
      serviceId: zuul-client
      stripPrefix: true #开启前缀

hystrix:
  command:
    zuul-client:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 10000

zuul-client:
  ribbon:
    NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
    listOfServers: http://127.0.0.1:8101,http://127.0.0.1:8102
    ConnectTimeout: 1000
    ReadTimeout: 3000
    MaxTotalHttpConnections: 500
    MaxConnectionsPerHost: 100

5.5 exclude routing configuration

Section 5.2 may be excluded service routing, this section may exclude path to a particular interface mode; follows will be excluded in the interface zuul-client admin all paths contained in the path;

zuul:
  ignoredPatterns: /**/admin/**
  routes:
    zuul-client: /api/**

More than 5.6 Routing Configuration

Before a service id configurations are arranged, the plurality of services if the following configuration example; id and specific services defined by the path from the reader;

zuul:
  routes:
    zuul-client1:
      path: /api/1/**
    zuul-client2:
      path: /api/2**

5.7 exclusion headers

A configuration request will exclude sensitive information in the header, it is a good choice;

zuul:
  routes:
    zuul-client:
      path: /api/**
      sensitiveHeaders: Cookie,Set-Cookie,Authorization
      url: http://127.0.0.1:8101

Six reference documents

Reference Documents

zuul-github:https://github.com/Netflix/zuul/wiki

springcloud official document : https: //cloud.spring.io/spring-cloud-static/Finchley.SR4/single/spring-cloud.html

Source: venue and knowledge seekers spingcloud column description you can get

Published 107 original articles · won praise 250 · Views 100,000 +

Guess you like

Origin blog.csdn.net/youku1327/article/details/104108108