zoukankan      html  css  js  c++  java
  • spring boot项目集成zuul网关

    1 zuul简介

      Zuul 的官方介绍是 “Zuul is the front door for all requests from devices and web sites to the backend of the Netflix streaming application. As an edge service application, Zuul is built to enable dynamic routing, monitoring, resiliency and security. It also has the ability to route requests to multiple Amazon Auto Scaling Groups as appropriate.”

      大致是说zuul是设备和网站到Netflix后台应用程序的所有的请求的前门,是一个边缘化应用程序,它的创建是为了实现动态路由,监控,弹性,和安全性, 它还能够根据需要将请求路由到多个Amazon Auto Scaling组。 

       其实zuul主要实现的功能就是API Gateway(api网关)的功能,为什么使用api gateway:

      1 客户端会多次请求不同的微服务,导致客户端复杂度增加,使用网关时客户端只与网关交互,降低客户端的调用逻辑的复杂度,同时网关也可以实现认证逻辑简化内部服务的之间相互调用的复杂度。

      2 对不同客户端的支持及数据的聚合,如一个网站有web端,手机端,页面所需的数据有同有异,可以将数据整合或者裁剪,减少客户端的请求次数,比如BFF架构。

      3可以更好的对项目的微服务封装,可将项目的微服务统一封装在一个内网环境中,只通过网关提供服务,同时网关也可以对安全,认证,监控,防御单独强化。

    2 Demo大致介绍

      2.1 eureka-server 用来提供eureka的服务

      2.2 eureka-consumer 用来饰演服务消费者

      2.3 zuul 用来提供路由服务

    3 在zuul服务的pom.xml中添加相关依赖

          
    <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-netflix-zuul</artifactId> </dependency>

    4 在yml文件中做相关配置

    spring:
      application:
        name: zuul
    eureka:
      instance:
        lease-expiration-duration-in-seconds: 30
        lease-renewal-interval-in-seconds: 10
        prefer-ip-address: true
        ip-address: 127.0.0.1
        instance-id: dragon.com
      client:
        fetch-registry: true
        register-with-eureka: true
        service-url:
          defaultZone: http://localhost:7001/eureka/
    server:
      port: 7003
    zuul:
      #  隐藏所有微服务名称(即使用微服务名称无法访问到服务)
      ignored-services: "*"
      #  服务前缀名,想要访问项目之前要加上此路径
      prefix: /fengyuntec
      routes:
    #   想要被替换的服务名称(con. 自己取得名字)
        con.serviceId: eureka-consumer
    #    替换后访问的名称
        con.path: /con/**

    5 在主类上添加注解

    @SpringBootApplication
    @EnableZuulProxy
    public class ZuulApplication {
        public static void main(String[] args) {
            SpringApplication.run(ZuulApplication.class, args);
        }
    }
  • 相关阅读:
    Django的是如何工作的
    Robot Framework自动化测试(五)--- 开发系统关键字
    Swarm 如何存储数据?- 每天5分钟玩转 Docker 容器技术(103)
    如何滚动更新 Service?- 每天5分钟玩转 Docker 容器技术(102)
    Service 之间如何通信?- 每天5分钟玩转 Docker 容器技术(101)
    神奇的 routing mesh
    如何访问 Service?- 每天5分钟玩转 Docker 容器技术(99)
    Swarm 如何实现 Failover?- 每天5分钟玩转 Docker 容器技术(98)
    如何实现 Service 伸缩?- 每天5分钟玩转 Docker 容器技术(97)
    运行第一个 Service
  • 原文地址:https://www.cnblogs.com/cl-rr/p/9699093.html
Copyright © 2011-2022 走看看