多云服务间调用解决方案
有的公司,可能有业务监管要求或者成本考虑等因素,使用多个云服务商的服务。那么多云服务间的通信就是个问题,下面提出一些浅显的方案,仅供参考。
方案一:IP白名单
一句话:改动小,维护难
开通 IP 白名单,特定IP来源的请求可以直接调用gateway服务;
缺点:如果因为服务升级,需要扩容,这个时候对外IP会增加,需要手动添加,如果扩容是自动操作,那么这个事情下,会造成很多请求不通,这个问题还是很大的。
方案二:openFeign
一句话:持续改动,有延迟
通过Spring cloud 提供的 openFeign 调用gateway 服务,所以有相互调用的接口,都需要维护到gateway服务上;所有的请求,都经过加密、解密,需要设计加密、解密算法,业界有比较成熟的方案。
每个调用其他云服务的接口都需要修改调用feign接口为新增的接口;
多云服务间可能会有比较大的网络延迟(可以通过方案四解决)。
缺点:老接口以及新增接口都需要维护到对应的gateway上,其他服务才能调用;测试也会相对困难。
具体实现
openFeign 实现远程调用。
@FeignClient(name = "health-user", path="health-user", url="https://www.prepared.com/", configuration = FeignConfiguration.class)
public interface UserFeign extends UserServiceI {
}
FeignConfiguration 实现,重试、超时、鉴权方案。
yml 设置超时时间
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
权限、重试策略自定义
@Component
public class FeignConfiguration implements RequestInterceptor {
public FeignConfiguration() {
}
@Bean
public Level logLevel() {
return Level.BASIC;
}
@Bean
public Retryer retryer() {
// 自定义重试方案
return Retryer.NEVER_RETRY;
}
@Bean
public Options options() {
return new Options(5000, 5000);
}
// 权限验证
@Bean
public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
return new BasicAuthRequestInterceptor("user", "password");
}
// 鉴权token
@Override
public void apply(RequestTemplate requestTemplate) {
requestTemplate.header("token", "token-str");
}
}
方案三:直接HTTP请求
改动比较大,每个调用其他服务的地方都需要修改为HTTP调用,需要增加HTTP调用工具类;同样,需要加密、解密操作。
缺点:服务代码比较乱,到处都是HTTP调用。
方案四:费钱方案——云联网
多云服务之间网络互通,直接使用一个配置中心即可,所有代码无需改动。
阿里、腾讯、联调、移动、电信等都提供了云联网方案。
需要申请物理专线,成本比较高。
线上出问题概率比较大,据领导说,这种架构出现的事故是总事故数的60%。
腾讯:https://cloud.tencent.com/document/product/877/18675
对等连接:https://cloud.tencent.com/document/product/553
申请物理专线:https://cloud.tencent.com/document/product/216/34829
安畅网络:https://www.anchnet.com/infrastructure/multicloud-connect
联通:https://cloudbond.chinaunicom.cn/
移动:https://ecloud.10086.cn/home/product-introduction/cloudInternet