zoukankan      html  css  js  c++  java
  • Spring Cloud(五):API网关服务——Spring Cloud Zuul

      通过前面的介绍,我们可以使用Spring Boot进行微服务开发,使用Spring Cloud Eureka实现注册中心以及微服务的注册和发现,使用Spring Cloud Ribbon实现服务间的负载均衡,使用Spring Cloud Hystrix实现线程隔离以及断路器功能。但是实际应用中这样的架构无疑增加了开发成本以及运维难度,而且后期重构难度也很大。为了解决以上各种问题,需要使用API 网关的方式。API 网关是一个服务器,它是进入一个系统的唯一节点,封装了内部系统的架构,并且提供了API给各个客户端,另外它还具备授权,监控,负载均衡,缓存,请求分片和管理,静态响应处理等功能。

      使用Zuul 构建API网关服务:

      在前几节创建的工程(下载地址:https://github.com/francis785/springclouddemo.git)基础上创建 springcloud-demo-zuul 模块,添加pom依赖:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>springcloud-demo-parent</artifactId>
            <groupId>com.fix</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>springcloud-demo-zuul</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <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>
        </dependencies>
    </project>

      编写启动主类 ZuulMain ,添加 @EnableZuulProxy 标签:

    @SpringBootApplication
    @EnableEurekaClient
    @EnableZuulProxy
    public class ZuulMain {
        public static void main(String[] args) {
            SpringApplication.run(ZuulMain.class, args);
        }
    }

      配置 application.yaml 文件:

    server:
      port: 8050
    eureka:
      instance:
        prefer-ip-address: true     #是否显示主机的Ip
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/     #指定eureka服务端地址
    spring:
      application:
        name: springcloud-demo-zuul     #指定应用名称
    zuul:
      routes:
        order-serverId:      #zuul的唯一标识
          path: /order/**       #需要映射的路径
          service-id: springcloud-demo-order

      分别启动服务注册中心、 springcloud-demo-order 以及网关服务 springcloud-demo-zuul ,注册中心已经注册的服务如下图:

      首先通过 springcloud-demo-order 的端口7900直接访问接口http://localhost:7900/order/1,接口响应如下:

      然后通过zuul的端口8050来访问 springcloud-demo-order 实例的接口:http://localhost:8050/springcloud-demo-order/order/1,接口响应如下:

      说明zuul配置的路由功能已经生效,可以看到zuul的配置和使用非常简单。

      除了这种配置方式,也可以不使用Eureka而单独使用zuul,但这种方式在实际应用中不推荐使用。

      

  • 相关阅读:
    基于对象颜色的对象检测(翻译)
    根据颜色和大小来检测和跟踪实时网络摄像机中的对象(翻译)
    C# 笔记 基础(2)
    C#学习笔记 基础 (1)
    深入学习RBAC系列模型——RBAC0模型的开发与学习心得
    RBAC权限管理
    SSL协议的工作流程
    页面的加载
    java实例化对象的方式
    cron表达式详解 原创
  • 原文地址:https://www.cnblogs.com/fengweiweicoder/p/11181784.html
Copyright © 2011-2022 走看看