In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what is the role of dubbo's FailbackClusterInvoker". In daily operation, I believe many people have doubts about the role of dubbo's FailbackClusterInvoker. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "what is the role of dubbo's FailbackClusterInvoker?" Next, please follow the editor to study!
Order
This paper mainly studies the FailbackClusterInvoker of dubbo.
FailbackClusterInvoker
Dubbo-2.7.3/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvoker.java
Public class FailbackClusterInvoker extends AbstractClusterInvoker {private static final Logger logger = LoggerFactory.getLogger (FailbackClusterInvoker.class); private static final long RETRY_FAILED_PERIOD = 5; private final int retries; private final int failbackTasks; private volatile Timer failTimer; public FailbackClusterInvoker (Directory directory) {super (directory); int retriesConfig = getUrl (). GetParameter (RETRIES_KEY, DEFAULT_FAILBACK_TIMES) If (retriesConfig = retries) {logger.error ("Failed retry times exceed threshold (" + retries + "), We have to abandon, invocation- >" + invocation);} else {rePut (timeout) } private void rePut (Timeout timeout) {if (timeout = = null) {return;} Timer timer = timeout.timer (); if (timer.isStop () | | timeout.isCancelled ()) {return } timer.newTimeout (timeout.task (), tick, TimeUnit.SECONDS);}}
The constructor of FailbackClusterInvoker initializes retriesConfig and the failbackTasksConfig;doInvoke method executes the addFailed method when catch to Throwable, which registers a RetryTimerTask,delay with HashedWheelTimer for 5 seconds; the run method of RetryTimerTask first selects a retryInvoker through the select method, then retries, increments retryTimes when catch to Throwable, executes the rePut method when the limit is not exceeded, and re-registers RetryTimerTask
FailbackClusterInvokerTest
Dubbo-2.7.3/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java
@ TestMethodOrder (MethodOrderer.OrderAnnotation.class) public class FailbackClusterInvokerTest {List invokers = new ArrayList (); URL url = URL.valueOf ("test://test:11/test?retries=2&failbacktasks=2"); Invoker invoker = mock (Invoker.class); RpcInvocation invocation = new RpcInvocation (); Directory dic; Result result = new AppResponse () / * * @ throws java.lang.Exception * / @ BeforeEach public void setUp () throws Exception {dic = mock (Directory.class); given (dic.getUrl ()) .willReturn (url); given (dic.list (invocation)) .willReturn (invokers); given (dic.getInterface ()) .willReturn (FailbackClusterInvokerTest.class); invocation.setMethodName ("method1"); invokers.add (invoker) } @ AfterEach public void tearDown () {dic = null; invocation = new RpcInvocation (); invokers.clear ();} private void resetInvokerToException () {given (invoker.invoke (invocation)) .willThrow (new RuntimeException ()); given (invoker.getUrl ()) .willReturn (url); given (invoker.getInterface ()) .willReturn (FailbackClusterInvokerTest.class) } private void resetInvokerToNoException () {given (invoker.invoke (invocation)) .willReturn (result); given (invoker.getUrl ()) .willReturn (url); given (invoker.getInterface ()) .willReturn (FailbackClusterInvokerTest.class);} @ Test @ Order (1) public void testInvokeException () {resetInvokerToException (); FailbackClusterInvoker invoker = new FailbackClusterInvoker (dic); invoker.invoke (invocation) Assertions.assertNull (RpcContext.getContext (). GetInvoker ()); DubboAppender.clear ();} @ Test @ Order (2) public void testInvokeNoException () {resetInvokerToNoException (); FailbackClusterInvoker invoker = new FailbackClusterInvoker (dic); Result ret = invoker.invoke (invocation); Assertions.assertSame (result, ret) @ Test @ Order (3) public void testNoInvoke () {dic = mock (Directory.class); given (dic.getUrl ()) .willReturn (url); given (dic.list (invocation)) .willReturn (null); given (dic.getInterface ()) .willReturn (FailbackClusterInvokerTest.class); invocation.setMethodName ("method1"); invokers.add (invoker); resetInvokerToNoException () FailbackClusterInvoker invoker = new FailbackClusterInvoker (dic); LogUtil.start (); DubboAppender.clear (); invoker.invoke (invocation); assertEquals (1, LogUtil.findMessage ("Failback to invoke")); LogUtil.stop ();} @ Disabled @ Test @ Order (4) public void testARetryFailed () throws Exception {/ / Test retries and resetInvokerToException () FailbackClusterInvoker invoker = new FailbackClusterInvoker (dic); LogUtil.start (); DubboAppender.clear (); invoker.invoke (invocation); Assertions.assertNull (RpcContext.getContext (). GetInvoker ()); / / invoker.retryFailed () / / when retry the invoker which get from failed map already is not the mocked invoker,so / / Ensure that the main thread is online CountDownLatch countDown = new CountDownLatch (1); countDown.await (15000L, TimeUnit.MILLISECONDS); LogUtil.stop (); Assertions.assertEquals (4, LogUtil.findMessage (Level.ERROR, "Failed retry to invoke method"), "must have four error message") Assertions.assertEquals (2, LogUtil.findMessage (Level.ERROR, "Failed retry times exceed threshold"), "must have two error message"); Assertions.assertEquals (1, LogUtil.findMessage (Level.ERROR, "Failback background works error"), "must have one error message"); / / it can be invoke successfully}}
Here we use mockito to mock resetInvokerToException and resetInvokerToNoException.
Summary
The constructor of FailbackClusterInvoker initializes retriesConfig and the failbackTasksConfig;doInvoke method executes the addFailed method when catch to Throwable, which registers a RetryTimerTask,delay with HashedWheelTimer for 5 seconds; the run method of RetryTimerTask first selects a retryInvoker through the select method, then retries, increments retryTimes when catch to Throwable, executes the rePut method when the limit is not exceeded, and re-registers RetryTimerTask
At this point, the study on "what is the role of FailbackClusterInvoker of dubbo" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.