1. 程式人生 > >SpringCloud(九)zuul閘道器 路由與負載均衡

SpringCloud(九)zuul閘道器 路由與負載均衡

前言:
        在前面的章節中,微服務架構中,每一個微服務都需要暴漏,不同的url,不同的埠,沒有一個統一的入口,這樣設計顯然不合理,閘道器的一個功能就是實現一個統一的入口,所有請求經過閘道器,然後閘道器統一代理到各個微服務,並且能實現負載均衡的功能。
程式碼:

工程構造如下;


工程的pom.xml檔案:用的springboot2.0

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xhx.springcloud</groupId>
    <artifactId>springcloud10-zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <modules>
        <module>eureka-server</module>
        <module>eureka-service</module>
        <module>eureka-feign</module>
        <module>zuul-service</module>
    </modules>

    <packaging>pom</packaging>

    <name>springcloud10-zuul</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RC2</spring-cloud.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>
    <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>

    <dependencies>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/libs-milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

建立一個Eureka微服務module:

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.xhx.springcloud</groupId>
        <artifactId>springcloud10-zuul</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>eureka-server</artifactId>
    <name>eureka-server</name>

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

    </dependencies>

</project>

application.yml

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      default-zone: http://${eureka.instance.hostname}:${server.port}/eureka

啟動類:

package com.xhx.springcloud;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

建立一個服務端微服務module

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-service</name>

    <parent>
        <groupId>com.xhx.springcloud</groupId>
        <artifactId>springcloud10-zuul</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application.yml

server:
  port: 8082
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka
spring:
  application:
    name: eureka-service

controller類:

package com.xhx.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * xuhaixing
 * 2018/6/3 16:18
 */
@RestController
@RequestMapping(value = "hello")
public class HelloController {

    @Value("${server.port}")
    private String port;

    @RequestMapping(value = "getName")
    public String getName(@RequestParam("name")  String name){
        return name+port;
    }
}

啟動類:

package com.xhx.springcloud;

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

@SpringBootApplication
@EnableEurekaClient 
public class EurekaServiceApplication {

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

建立一個微服務消費者module:

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xhx.springcloud</groupId>
    <artifactId>eureka-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-feign</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.xhx.springcloud</groupId>
        <artifactId>springcloud10-zuul</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId> spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <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-openfeign</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>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

application.yml

server:
  port: 8085
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka
spring:
  application:
    name: eureka-feign
feign:
  hystrix:
    enabled: true

feign api

package com.xhx.springcloud.api;

import com.xhx.springcloud.hystrix.HelloFeignHystrix;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * xuhaixing
 * 2018/6/5 16:51
 */
@FeignClient(value = "EUREKA-SERVICE",path = "hello", fallback =HelloFeignHystrix.class)
public interface HelloFeign {

    @RequestMapping(value = "getName")
    public String getName(@RequestParam("name") String name);//@RequestParam 必須加上,否則可能會接不到引數
}

hystrix

package com.xhx.springcloud.hystrix;

import com.xhx.springcloud.api.HelloFeign;
import org.springframework.stereotype.Component;

/**
 * xuhaixing
 * 2018/6/6 13:36
 */
@Component
public class HelloFeignHystrix implements HelloFeign {
    @Override
    public String getName(String name) {
        return "請求第三方api錯誤";
    }
}

controller

package com.xhx.springcloud.controller;

import com.netflix.discovery.converters.Auto;
import com.xhx.springcloud.api.HelloFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * xuhaixing
 * 2018/6/3 16:27
 */
@RestController
@EnableEurekaClient
@EnableHystrix
public class RibbionController {

    @Autowired
    private HelloFeign helloFeign;

    @RequestMapping(value = "getName")
    public String getName(String name){
        return helloFeign.getName(name);
    }
}

啟動類:

package com.xhx.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients   //啟用feign
public class EurekaFeignApplication {

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

下面建立閘道器:

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud10-zuul</artifactId>
        <groupId>com.xhx.springcloud</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>zuul-service</artifactId>

    <dependencies>
        <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>


    </dependencies>

</project>

application.yml,暫時什麼都不配置

server:
  port: 8090
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka
spring:
  application:
    name: zuul-service

啟動類:

package com.xhx.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**
 * xuhaixing
 * 2018/6/11 16:39
 */
@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
public class ZuulServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ZuulServiceApplication.class,args);
    }
}

這幾個服務都啟動,啟動微服務服務端,改下埠號,啟動兩個例項,訪問eureka http://localhost:8761/


下面通過閘道器訪問微服務:

localhost:8090/eureka-feign/getName 訪問服務消費者的getName方法,服務名稱必須是小寫,多訪問幾次,會發現自帶負載均衡功能



訪問eureka時還需要直接帶上微服務的名稱這樣明顯不好,這一節些實現基本功能,下一節進行閘道器配置。