Spring-Cloud-Finchley | 路由网关 Zuul

简介

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: 1136513099@qq.com

推荐阅读
史上最全,最完美的 JAVA 技术体系思维导图总结,没有之一!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星尘Pro

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值