spring cloud - API Gateway

spring cloud API Gateway

  通过API Gateway,可以统一向外部系统提供REST API。Spring Cloud中使用Zuul作为API Gateway。Zuul提供了动态路由、监控、回退、安全等功能。

  1. eureka+Zuul配置和使用

(1).准备工作

// 为了更贴近生产,我们首先配置Host

127.0.0.1 gateway

// 启动服务:microservice-discovery-eureka
// 启动服务:microservice-provider-user

(2).Zuul代码示例


//gradle依赖如下

compile "org.springframework.cloud:spring-cloud-starter-zuul:Brixton.SR5"
compile "org.springframework.cloud:spring-cloud-starter-eureka:Brixton.SR5"

//maven依赖如下

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>Brixton.SR5</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zuul</artifactId>
    <version>Brixton.SR5</version>
</dependency>
// 启动类ZuulApiGatewayApplication.java

/**
 * 使用@EnableZuulProxy注解激活zuul。
 * 跟进该注解可以看到该注解整合了@EnableCircuitBreaker、@EnableDiscoveryClient,是个组合注解,目的是简化配置。
 * @author eacdy
 */
@SpringBootApplication
@EnableZuulProxy
public class ZuulApiGatewayApplication {
  public static void main(String[] args) {
    SpringApplication.run(ZuulApiGatewayApplication.class, args);
  }
}
// 配置文件:application.yml

spring:
  application:
    name: microservice-api-gateway
server:
  port: 8050
eureka:
  instance:
    hostname: gateway
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/

// 这样,一个简单的API Gateway就完成了。      

(3).测试工作


// 启动microservice-api-gateway项目。还记得我们之前访问通过http://localhost:8000/1去访问microservice-provider-user服务中id=1的用户信息吗?

// 我们现在访问http://localhost:8050/microservice-provider-user/1试试。会惊人地看到:

{"id":1,"username":"Tom","age":12}

// 这正是microservice-provider-user服务中id=1的用户信息

// 所以我们可以总结出规律:访问

http://GATEWAY:GATEWAY_PORT/想要访问的Eureka服务id的小写/**

// 将会访问到

http://想要访问的Eureka服务id的小写:该服务端口/**

(4).自定义路径

// 上文我们已经完成了通过API Gateway去访问微服务的目的,是通过

http://GATEWAY:GATEWAY_PORT/想要访问的Eureka服务id的小写/**

//的形式访问的,那么如果我们想自定义在API Gateway中的路径呢?譬如想使用

http://localhost:8050/user/1

// 就能够将请求路由到http://localhost:8000/1呢?

// 只需要做一点小小的配置即可:

spring:
  application:
    name: microservice-api-gateway
server:
  port: 8050
eureka:
  instance:
    hostname: gateway
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/
# 下面整个树都非必须,如果不配置,将默认使用 http://GATEWAY:GATEWAY_PORT/想要访问的Eureka服务id的小写/** 路由到:http://想要访问的Eureka服务id的小写:该服务端口/**
zuul:
  routes:
    user:                                               # 可以随便写,在zuul上面唯一即可;当这里的值 = service-id时,service-id可以不写。
      path: /user/**                                    # 想要映射到的路径
      service-id: microservice-provider-user            # Eureka中的serviceId

(5).如何忽略某些服务

// 启动服务:microservice-discovery-eureka
// 启动服务:microservice-provider-user
// 启动服务:microservice-consumer-movie-ribbon

// 如果我们现在只想将microservice-consumer-movie-ribbon服务暴露给外部,microservice-provider-user不想暴露,那么应该怎么办呢?

// 依然只是一点小小的配置即可:

spring:
  application:
    name: microservice-api-gateway
server:
  port: 8050
eureka:
  instance:
    hostname: gateway
  client:
    serviceUrl:
      defaultZone: http://discovery:8761/eureka/
zuul:
  ignored-services: microservice-provider-user          # 需要忽视的服务(配置后将不会被路由)
  routes:
    movie:                                              # 可以随便写,在zuul上面唯一即可;当这里的值 = service-id时,service-id可以不写。
      path: /movie/**                                   # 想要映射到的路径
      service-id: microservice-consumer-movie-ribbon-with-hystrix    # Eureka中的serviceId

// 这样microservice-provider-user服务就不会被路由,microservice-consumer-movie-ribbon服务则会被路由。      

(7).描述来源于:http://book.itmuch.com/2%20Spring%20Cloud/2.6%20API%20Gateway.html

  1. 使用Zuul不使用Eureka
// Zuul并不依赖Eureka,可以脱离Eureka运行,此时需要配置

spring:
  application:
    name: microservice-api-gateway
server:
  port: 8050
zuul:
  routes:
    movie:                                              # 可以随便写
      path: /user/**
      url: http://localhost:8000/                       # path路由到的地址,也就是访问http://localhost:8050/user/**会路由到http://localhost:8000/**


// 我们可尝试访问http://localhost:8050/user/1 ,会发现被路由到了http://localhost:8000/1 。不过在大多数情况下,笔者并不建议这么做,因为得手动大量地配置URL,不是很方便。

// Zuul还支持更多的特性、更多的配置项甚至是定制开发,具体还请读者自行发掘。

// Zuul只是API Gateway的一种实现,可作为API Gateway的软件有很多,譬如Nginx Plus、Kong等等

描述来源于:http://book.itmuch.com/2%20Spring%20Cloud/2.6%20API%20Gateway.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值