码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Cloud(五):API网关服务——Spring Cloud Zuul

时间:2019-07-13 20:18:56      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:一个   增加   aml   dem   yam   授权   apache   重构   boot   

  通过前面的介绍,我们可以使用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,但这种方式在实际应用中不推荐使用。

 

 

 

 

  

Spring Cloud(五):API网关服务——Spring Cloud Zuul

标签:一个   增加   aml   dem   yam   授权   apache   重构   boot   

原文地址:https://www.cnblogs.com/fengweiweicoder/p/11181784.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!