构建meteor应用程序_构建弹性的android应用程序

构建meteor应用程序

In the world of distributed systems and micro-services resiliency is an important concept that helps with building fault tolerant systems. In the Java world, Netflix has pioneered in this space with Hystrix, which is now in maintenance mode, and instead resilience4j is the recommended framework to use. And while these tools are very popular and powerful, I’ve never seen them being used in an Android context until recently.

在分布式系统和微服务领域,弹性是一个重要的概念,可帮助构建容错系统。 在Java世界中,Netflix凭借Hystrix进入了这一领域,该技术现已处于维护模式,因此, resilience4j是推荐使用的框架。 尽管这些工具非常流行且功能强大,但直到最近我才看到它们在Android环境中使用。

While working on connectivity for Microsoft’s Your Phone app, we determined that having a resiliency framework will make it significantly easier to have fault tolerance. Consider the following example: You link your Android phone to your PC and are looking at your phone’s pictures from the PC when your phone suddenly loses its internet connection. Ideally, the phone and PC will now try to reconnect automatically and resume the session and this way recover gracefully from connectivity issues. In fact, it is the right thing to do from a UX perspective. We want to give the user the feeling that their devices are always connected or at-least trying their best to be connected. So how did we go about this?

在为Microsoft的Your Phone应用程序进行连接时,我们确定拥有弹性框架将大大简化容错能力。 请考虑以下示例:您将Android手机链接到PC,并且当手机突然失去互联网连接时,正在从PC查看手机的图片。 理想情况下,电话和PC现在将尝试自动重新连接并恢复会话,从而从连接问题中正常恢复。 实际上,从用户体验的角度出发,这是正确的做法。 我们希望给用户一种感觉,即他们的设备始终处于连接状态,或者至少会尽最大努力进行连接。 那么我们如何做到这一点呢?

Given that the Your Phone app consists of a UWP app for Windows and an Android app, we had to look at solutions for both platforms. On the Windows side, things were really easy because we could simply use the popular Polly framework, which can be used like this:

鉴于您的电话应用程序包含Windows的UWP应用程序和Android应用程序,因此我们必须研究两种平台的解决方案。 在Windows方面,事情真的很容易,因为我们可以简单地使用流行的Polly框架 ,可以这样使用:

var policy = Policy
              .Handle<SomeExceptionType>()
              .Retry();


var result = policy.Execute(() => DoSomething());

The great thing about Polly and C# is that for asynchronous code we can simply use the async/await mechanism which is fully supported by Polly and makes it very effortless to write resilient code for both synchronous and asynchronous operations. But what about Android?

Polly和C#的伟大之处在于,对于异步代码,我们可以简单地使用async / await机制,该机制得到了Polly的完全支持,这使得为同步和异步操作编写弹性代码变得非常轻松。 但是Android呢?

For Android and Java things are a lot harder due to Java’s lack of a built-in concurrency framework and the resulting framework fragmentation, as well as Android’s (up until recently) lacking support for Java 8’s functional constructs. For instance, resilience4j is designed for Java 8 and functional programming and therefore requires JDK8 to be used. This obviously wasn’t possible on Android up until recently when core library desugaring became stable. At the same time our application does not use a single concurrency framework, we use both RxJava and a CompletableFuture backport. For this reason we ended up building our own minimalist resiliency framework that supports both Retry and CircuitBreaker for RxJava using custom operators.

对于Android和Java而言,由于Java缺乏内置的并发框架和由此产生的框架碎片,以及Android(直到最近)缺乏对Java 8的功能构造的支持,所以事情变得更加困难。 例如,resilience4j专为Java 8和功能编程而设计,因此需要使用JDK8。 直到最近核心库的废止变得稳定之前,这显然在Android上是不可能的。 同时,我们的应用程序不使用单个并发框架,而是同时使用RxJava和CompletableFuture反向端口。 因此,我们最终建立了自己的极简弹性框架,该框架使用自定义运算符同时支持Retry和CircuitBreaker for RxJava。

If we had to start from scratch again today, it would probably be wise to use resilience4j by enabling core library desugaring. The great thing about the framework is that it comes with adapters for commonly used frameworks, including CompletableFuture (default), RxJava 2 and 3 and Kotlin coroutines and flow. Let’s look at a simple example to explore how easy it can be to write resilient code using Kotlin’s coroutines and Resilience4j. Let’s assume that we have a (very simplified) class that represents a connection between two devices and looks like this:

如果今天我们不得不从头开始,那么通过启用核心库调试功能来使用resilience4j可能是明智的。 该框架的优点在于,它带有适用于常用框架的适配器,包括CompletableFuture(默认),RxJava 2和3以及Kotlin协程和流程。 让我们看一个简单的示例,探索使用Kotlin的协程和Resilience4j编写弹性代码有多么容易。 假设我们有一个(非常简化的)类,它表示两个设备之间的连接,如下所示:

interface Connection {
    val isConnected: Boolean
    
    suspend fun connectToDevice(): ConnectionResult
    
    suspend fun sendMessage(payload: ByteArray)
}

We will design a wrapper around this class that attempts to send a message to a device by first attempting to establish a connection and if successful, sending a message. The difficult part about this is that we have to assume that both operations can easily fail and therefore resiliency is key. Let’s first look at how we can define the retries using resilience4j:

我们将围绕该类设计一个包装器,该包装器首先尝试建立连接,然后尝试发送消息,然后尝试将消息发送到设备。 困难的部分是我们必须假设两个操作都容易失败,因此弹性是关键。 首先让我们看一下如何使用resilience4j定义重试:

private val connectRetry = Retry.of("connect", RetryConfig<ConnectionResult> {
    maxAttempts(3)
    waitDuration(Duration.ofSeconds(3))
    retryOnResult { it == ConnectionResult.UNREACHABLE }
})


private val sendMessageRetry = Retry.of("sendMessage", RetryConfig<Unit> {
    maxAttempts(3)
    intervalFunction { retryNumber -> 2.toDouble().pow(retryNumber).roundToLong() }
    retryOnException { it is FailedToSendException }
})

We can simply define two retry strategies using the handy DSL that the Kotlin extension provides. For connecting to the device we will use an enum to represent the result of that operation and will retry if the remote device is unreachable using a delay of 3s between each try. For sending a message, we will make a total of 3 attempts with an exponential back off if we encounter a FailedToSendException. Resilience4j offers a rich API for defining Retries, CircuitBreakers, Bulkheads, Ratelimiters, Caches and more. To use a Retry, the library provides extension functions that allow us to compose the whole operation like so:

我们可以使用Kotlin扩展程序提供的便捷DSL简单定义两种重试策略。 为了连接到设备,我们将使用一个枚举来表示该操作的结果,如果远程设备不可访问,则将重试,每次尝试之间的延迟为3秒。 对于发送消息,如果遇到FailedToSendException ,我们将总共尝试3次,并以指数方式退回。 Resilience4j提供了丰富的API,用于定义重试,CircuitBreakers,隔板,Ratelimiters,Caches等。 要使用Retry ,该库提供了扩展功能,使我们可以像这样构成整个操作:

suspend fun connectAndSendMessage(message: ByteArray) {
    val result = if (!connection.isConnected) {
        connectRetry.executeSuspendFunction {
            connection.connectToDevice()
        }
    } else ConnectionResult.CONNECTED


    if (result == ConnectionResult.CONNECTED) {
        sendMessageRetry.executeSuspendFunction {
            connection.sendMessage(message)
        }
    } else throw FailedToSendException()
}

While this is a very simple example it shows how we can use the abstractions provided by Resilience4j to build powerful, fault tolerant code. The great thing about this framework is that it’s extremely flexible and comes with adapters for the most commonly used concurrency frameworks or we could even build an adapter for our own concurrency framework.

尽管这是一个非常简单的示例,但它说明了我们如何使用Resilience4j提供的抽象来构建功能强大的容错代码。 这个框架的最大优点是它非常灵活,并且为最常用的并发框架提供了适配器,或者我们甚至可以为自己的并发框架构建一个适配器。

While not every app will equally benefit from adding resiliency mechanisms like this, frameworks like Resilience4j can make it extremely simple to make our code more fault tolerant without adding too much overhead. Especially when throwing Kotlin in the mix, adding resiliency can be as simple as calling an extension function and passing in an existing suspend function.

虽然并非每个应用程序都会从​​像这样的弹性机制中获得同等的收益,但是像Resilience4j这样的框架可以使我们的代码更加容错,而又不会增加太多开销,这非常简单。 特别是在将Kotlin混入其中时,添加弹性可以像调用扩展函数并传入现有的暂停函数一样简单。

If you haven’t tried the Your Phone app, I can highly recommend to download it to connect your Android phone to your Windows computer:

如果您尚未尝试使用“手机”应用程序,强烈建议您下载该应用程序以将您的Android手机连接到Windows计算机:

翻译自: https://medium.com/android-microsoft/building-resilient-android-applications-cf8448106e23

构建meteor应用程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值