我的Spring Cloud(五):Zuul 服务网关

一、什么是Zuul 服务网关

    Zuul 是 Netflix 提供的⼀个开源的 API ⽹关服务器,是客户端和⽹站后端所有请求的中间层,对外开放 ⼀个 API,将所有请求导⼊统⼀的⼊⼝,屏蔽了服务端的具体实现逻辑,Zuul 可以实现反向代理的功 能,在⽹关内部实现动态路由、身份认证、IP 过滤、数据监控等。Zuul也是Spring Cloud集成的组件,通过它来实现服务网关。

二、Zuul的功能列表

    1. 身份认证与安全:识别每个资源的验证要求,并拒绝那些与要求不符合的请求。

    2. 审查与监控:在边缘位置追踪有意义的数据和统计结果,从而带来精确的生产视图。

    3. 动态路由:动态的将请求路由到不同的后端集群。

    4. 压力测试:逐渐增加指向集群的流量,以了解性能。

    5. 负载分配:为每一种负载类型分配对应容量,并弃用超出限定值的请求。

    6. 静态响应处理:在边缘位置直接建立部分响应,从而避免其转发到内部集群。

    7. 多区域弹性:跨越AWS Region进行请求路由,旨在实现ELB(Elastic Load Balancing)使用的多样化,以及让系统的便越更贴近系统的使用者。

三、实战!

1.创建maven工程,pom.xml配置如下:

    <dependency>
		 <groupId>org.springframework.cloud</groupId>
		 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		 <version>2.0.2.RELEASE</version>
	 </dependency>
	 
	 <dependency>
		 <groupId>org.springframework.cloud</groupId>
		 <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
		 <version>2.0.2.RELEASE</version>
	 </dependency>

2.创建配置文件application.yml配置如下:

server:
  port: 8030
spring:
  application:
    name: gateway
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
zuul:
  routes:
    provider: /p/**

属性说明

    * zuul.routes.provider: 给服务提供者 provider 设置映射,可以不需要再记住服务提供者的端口

3.创建启动类,代码如下:

package com.zing;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableZuulProxy
@EnableAutoConfiguration
public class ZuulApplication {
	public static void main(String[] args) throws Exception {
		SpringApplication.run(ZuulApplication.class, args);
	}

}

注解说明

    *  @EnableZuulProxy : 包含了 @EnableZuulServer ,设置该类是⽹关的启动类。

    *  @EnableAutoConfiguration :可以帮助 Spring Boot 应⽤将所有符合条件的 @Configuration 配置加载到当前 Spring Boot 创建并使⽤的 IOC 容器中。

四、Zuul的负载均衡

    1.修改服务提供者的 controller 层代码,具体如下:

package com.zing.controller;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.zing.entity.Student;
import com.zing.repository.StudentRepository;


@RestController //在Spring中@RestController的作用等同于@Controller + @ResponseBody。
@RequestMapping("/student")
public class StudentHandler {
	@Autowired
	private StudentRepository studentRepository;
	
	@Value("${server.port}")
	private String port;
	
	@GetMapping("/findAll")
	public Collection<Student> findAll(){
		return studentRepository.findAll();
	}
	
	@GetMapping("/findById/{id}")
	public Student findById(@PathVariable("id") long id) {
		return studentRepository.findById(id);
	}
	
	@PostMapping("/save")
	public void save(@RequestBody Student s) {
		studentRepository.saveOrUpdate(s);
	}
	
	@PutMapping("/Update")
	public void Update(@RequestBody Student s) {
		studentRepository.saveOrUpdate(s);
	}
	
	@DeleteMapping("/deleteById/{id}")
	public void deleteById(@PathVariable("id") long id) {
		studentRepository.deleteById(id);
	}
	
	@GetMapping("/index")
	public String index() {
		return "当前端口:" + this.port;
	}
	
}

    注解说明

    * @Value("${server.port}"):获取当前项目application.yml配置文件中的server.port属性值。

    2.首先启动注册中心,然后启动服务提供者,修改端口为8011后,再建一个启动类再次启动,我们就可以在注册中心看到两个prider服务,如图:

3.启动zuul的启动类,我们可以访问 http://localhost:8030/p/student/index查看调用服务的端口,我们不断的刷新,可以看到端口是交替出现的:

五、总结

    zuul 服务网关是微服务架构中不可或缺的的部分。通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由、负载均衡之外,它还具备了权限控制等功能。 它还可以和Ribbon 搭配使用,那么Ribbon 又是如何使用呢, 让我们期待下一篇《我的Spring Cloud(六):Ribbon 负载均衡》。

    更多精彩内容,敬请扫描下方二维码,关注我的微信公众号【Java觉浅】,获取第一时间更新哦!

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Java觉浅

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

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

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

打赏作者

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

抵扣说明:

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

余额充值