简介
Zuul 是Netflix 旗下实现路由网关的一个组件,客户端在进行请求时,首先请求会到达网关,然后网关将请求分发到不同的服务,网关可以根据 URI 区别用户请求的具体服务,然后进行转发,返回响应的资源给用户。
实例
1、创建 maven 工程
创建 maven 工程,添加核心的父 maven 依赖,所有子 pom 文件继承这个父 pom 文件。
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</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>
2、搭建服务注册中心 service-server
添加 maven 依赖
<parent>
<groupId>com.hly</groupId>
<artifactId>05-spring-cloud-zuul</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
application.yml 配置文件
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: service-server
SpringBoot 启动类
@SpringBootApplication
@EnableEurekaServer
public class ServiceServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceServerApplication.class, args);
}
}
3、搭建客户端 service-client-hello 和 service-client-he
两个客户端的配置代码基本一致,这里以一个为例
添加 maven 依赖
<parent>
<groupId>com.hly</groupId>
<artifactId>05-spring-cloud-zuul</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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>
</dependencies>
application.yml 配置文件
server:
port: 8762
spring:
application:
name: service-client-hello
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
SpringBoot 启动类
@SpringBootApplication
@RestController
@EnableEurekaClient
public class ServiceClientHelloApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceClientHelloApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hello")
public String home(@RequestParam(value = "name",defaultValue = "hly") String name){
return "hello "+name+",I am from port: " + port;
}
}
3、搭建路由网关 service-zuul
添加 maven 依赖
<parent>
<groupId>com.hly</groupId>
<artifactId>05-spring-cloud-zuul</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<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.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
application.yml 配置文件
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8769
spring:
application:
name: service-zuul
zuul:
routes:
api-hello:
path: /api-hello/**
serviceId: service-client-hi
api-hi:
path: /api-hi/**
serviceId: service-client-hello
SpringBoot 启动类
@SpringBootApplication
@EnableEurekaServer
public class ServiceServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceServerApplication.class, args);
}
}
演示
1、首先启动注册中心
2、然后启动两个客户端
3、启动路由网关
4、访问 http://localhost:8769/api-hello/hello 路由到 hello 客户端
5、访问 http://localhost:8769/api-hi/hi 路由到 hi 客户端
代码下载
05-spring-cloud-zuul:https://github.com/huangliangyun/Spring-Cloud-Finchley
关于
我的 Github:Github
CSDN: CSDN
个人网站: sirius 的博客
E-mail: [email protected]