Implementation of API Gateway Based on zuul

1. The difference between Nginx and Zuul

Similarities: Zuul and Nginx can achieve load balancing, reverse proxy (hiding real ip address), filter requests, and achieve the effect of gateway.

Differences: Nginx-c Language Development

Zuul--java Language Development

Zuul Load Balancing: Local Load Balancing Using ribbon+eureka

Nginx Load Balancing: Using Server to Realize Load Balancing

 

Nginx is more powerful than zuul because it integrates some scripting languages (Nginx+lua)

Nginx is suitable for server-side load balancing

Zuul Suitable for Gateway Implementation in Micro Services

Zuul's framework design is easier because it's written in java

 

2,

,

 

Zuul, projectA and projectB are all registered on the registry Eureka and controlled by eureka:

 

Step 1: Resume Ereka Service

server:
  port: 8100
eureka:
  instance:
    hostname: server1
  client:
    serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    register-with-eureka: false
    fetch-registry: false

Startup class:

package com.aiyuesheng;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(final String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

step2: projectA

server:
  port: 8000
spring:
    application:
        name: projectA
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka

Startup class:

@SpringBootApplication
@EnableDiscoveryClient
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

controller:

@RestController
public class Index {
    @RequestMapping("/")
    public String index() {
        return "PROJECTA";
    }
}

step2: projectB

server:
  port: 8001
spring:
    application:
        name: projectB
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8100/eureka

Startup class:

@SpringBootApplication
@EnableDiscoveryClient
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

controller:

@RestController
public class Index {
    @RequestMapping("/")
    public String index() {
        return "PROJECTB";
    }
}

Step 4: Zuul Configuration Gateway

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8100/eureka/
server:
  port: 8080
spring:
  application:
    name: service-zuul
zuul:
  routes:
    projectA:
      path: /projectA/**
      serviceId: projectA
    projectB:
      path: /projectB/**
      serviceId: projectB
zuul:
  routes:
    api-a:
      path: /api-a/**
      serviceId: itmayiedu_a
    api-b:
      path: /api-b/**
      serviceId: itmayiedu_b

Startup class:

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;
import org.springframework.context.annotation.Bean;
@EnableZuulProxy
@SpringBootApplication
@EnableDiscoveryClient
public class ZuulApplication  {
    public static void main(String[] args) {
        SpringApplication.run(ZuulApplication.class, args);
    }
//    @Bean
//    public TokenFilter accessFilter() {
//        return new TokenFilter();
//    }
}

 

Start the eureka registry first, then start the other three services. After startup, you can check whether the service is registered on the web page:

 

I can configure routes through the gateway.

  routes:
    projectA:
      path: /projectA/**
      serviceId: projectA
    projectB:
      path: /projectB/**
      serviceId: projectB

 

127.0.0.1/projectA automatically jumps to Project A

127.0.0.1/projectB automatically jumps to Project B

Keywords: PHP Nginx Spring Java C

Added by kevinsequeira on Mon, 14 Oct 2019 00:05:24 +0300