.NET Core + Spring Cloud:API 网关

栏目: Java · 发布时间: 6年前

内容简介:API网关是系统的唯一入口,调用任何服务的请求都需要经过网关层,最终才可能到达目标服务,既然是必经之路,那我们可以在网关层进行一些通用的操作,如:认证、鉴权、限流、智能路由、缓存、日志、监控、超时、熔断、重试等等,这样既使整个框架条理清晰,也让开发者更多注重功能的逻辑实现。常见的 API 网关项目有:

API网关是系统的唯一入口,调用任何服务的请求都需要经过网关层,最终才可能到达目标服务,既然是必经之路,那我们可以在网关层进行一些通用的操作,如:认证、鉴权、限流、智能路由、缓存、日志、监控、超时、熔断、重试等等,这样既使整个框架条理清晰,也让开发者更多注重功能的逻辑实现。

常见的 API 网关项目有: KongTykNetflix zuulOcelot 等。在 Spring Cloud 中,Zuul 是其核心组件,下面将介绍 .NET Core 中通过 Zuul 来进行统一 API 调用。

.NET Core + Spring Cloud:API 网关

搭建 Zuul Server

Eureka Server 的搭建请参考 .NET Core + Spring Cloud:服务注册与发现

  1. 在 IntelliJ IDEA 中新建项目,选 Spring Initializr 完成项目创建

  2. 在 pom.xml 添加 zuul 和 eureka-client 的依赖,我们将会把 Zuul Server 注册到 Eureka Server

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>
    
    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
  3. 在启动类上添加 EnableZuulProxy 注解

    @EnableZuulProxy 
    @SpringBootApplication
    public class EurekaServiceApplication {
    	public static void main(String[] args) {
    		SpringApplication.run(EurekaServiceApplication.class, args);
    	}
    }
    
  4. 修改 application.yml 配置文件( 集群模式通过一个类似 gateway.xxx.com 的域名指向多个 Zuul Server 实例即可

    spring:
      application:
    	name: zuul-service
    
    server:
      port: 5555
    
    eureka:
      instance:
    	hostname: server1
    	# 超过这个时间没收到心跳就剔除这个服务,这个配置一般为服务刷新时间配置的三倍,默认90s
    	lease-expiration-duration-in-seconds: 15
    	# 服务刷新时间,默认30s
    	lease-renewal-interval-in-seconds: 5
      client:
    	service-url:
    	  defaultZone: http://server1:8001/eureka/,http://server2:8002/eureka/,http://server3:8003/eureka/
    
  5. 启动服务,访问: http://server1:8001/(*请确保 Eureka Server 已启动*),可以发现 Zuul Server 已在 5555 端口启动

    .NET Core + Spring Cloud:API 网关

创建 .NET Core 服务

这里直接基于文章 .NET Core + Spring Cloud:服务注册与发现 中的测试项目,修改 .NET Core 客户端服务的配置文件 services:base-service:url

"services": {
	"base-service": {
	  "url": "http://server1:5555/base-service/" // 原来 http://base-service/
	}
  }

因为我们的 Zuul Server 是启动在 server1 的 5555 端口,通过 Zuul 网关调用服务的路由规则是 http://server1:5555/ + {Application 小写},而基础服务的 Application名称为 BASE-SERVICE,所以 url 设置为 http://server1:5555/base-service/

.NET Core + Spring Cloud:API 网关

测试

访问 base-service

通过网关的路由地址 http://server1:5555/base-service/ 访问 api/values 接口,多次请求可测试负载均衡效果

.NET Core + Spring Cloud:API 网关

.NET Core + Spring Cloud:API 网关


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Mechanics of Web Handling

The Mechanics of Web Handling

David R. Roisum

This unique book covers many aspects of web handling for manufacturing, converting, and printing. The book is applicable to any web including paper, film, foil, nonwovens, and textiles. The Mech......一起来看看 《The Mechanics of Web Handling》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

html转js在线工具
html转js在线工具

html转js在线工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具