SpringCloud5:路由网关zuul

基于上一篇包含EurekaServer、EurekaClient、Ribbon、Feign的工程。

一、创建service-zuul工程
在主工程上创建Maven module
pom文件如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.jianyu</groupId>
        <artifactId>chapter5</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.jianyu</groupId>
    <artifactId>service-zuul</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <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>Dalston.SR4</spring-cloud.version>
    </properties>

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

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

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

二、applicaton类加上注解@EnableZuulProxy

1
2
3
4
5
6
7
8
9
10
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class ServiceZuulApplication
{
    public static void main( String[] args )
    {
        SpringApplication.run(ServiceZuulApplication.class, args);
    }
}

三、application.properties配置文件
在src/main下创建resources文件夹
创建application.properties。
1、指定服务注册中心的地址为http://localhost:8900/eureka/
2、zuul服务的端口为8960,服务名为service-zuul
3、以/api-a/ 开头的请求都转发给service-ribbon服务;以/api-b/开头的请求都转发给service-feign服务;

1
2
3
4
5
6
7
8
9
10
11
#ribbon port
server.port=8960
#eureka server
eureka.client.service-url.defaultZone=http://127.0.0.1:8900/eureka/
# ribbon name
spring.application.name=service-zuul
# routes
zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=service-ribbon
zuul.routes.api-b.path=/api-b/**
zuul.routes.api-b.serviceId=service-feign

四、测试
1、依次启动EurekaServer、EurekaClient、Ribbon、Feign、Zuul工程
2、访问:http://localhost:8960/api-a/hello?name=baozi
3、访问:http://localhost:8960/api-b/hello?name=baozi
浏览器都显示:Hello, baozi,I am from port:8922,说明两个路径得到了路由

源码:https://github.com/baijy/springcloudlearn/tree/master/chapter5