You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/11/18 09:00:59 UTC

[camel] 17/23: CAMEL-13691: camel-resilience4j - WIP

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch CAMEL-13691
in repository https://gitbox.apache.org/repos/asf/camel.git

commit cc808de724c50c43d9d64c4e828e8ea27a91b418
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Nov 18 06:13:45 2019 +0100

    CAMEL-13691: camel-resilience4j - WIP
---
 .../src/main/docs/eips/circuitBreaker-eip.adoc     |  61 +++++++++++++++
 .../src/main/docs/eips/hystrix-eip.adoc            |   2 +-
 .../src/main/docs/eips/resilience4j-eip.adoc       |   2 +-
 .../ROOT/assets/images/eip/CircuitBreaker.png      | Bin 0 -> 24745 bytes
 docs/user-manual/modules/ROOT/nav.adoc             |   2 +-
 .../modules/ROOT/pages/circuitBreaker-eip.adoc     |  82 +++++++++++----------
 .../modules/ROOT/pages/hystrix-eip.adoc            |   6 +-
 .../modules/ROOT/pages/resilience4j-eip.adoc       |  31 +++++---
 8 files changed, 131 insertions(+), 55 deletions(-)

diff --git a/core/camel-core-engine/src/main/docs/eips/circuitBreaker-eip.adoc b/core/camel-core-engine/src/main/docs/eips/circuitBreaker-eip.adoc
new file mode 100644
index 0000000..46909dc
--- /dev/null
+++ b/core/camel-core-engine/src/main/docs/eips/circuitBreaker-eip.adoc
@@ -0,0 +1,61 @@
+[[circuitBreaker-eip]]
+= CircuitBreaker EIP
+
+The Circuit Breaker pattern is inspired by the real-world electrical circuit breaker,
+which is used to detect excessive current draw and fail fast to protect electrical equipment.
+The software-based circuit breaker works on the same notion, by encapsulating
+the operation and monitoring it for failures. The Circuit Breaker pattern operates in
+three states, as illustrated in the following figure:
+
+image::eip/CircuitBreaker.png[image]
+
+The states are as follows:
+
+* *Closed* — When operating successfully.
+* *Open* — When failure is detected and the breaker opens to short-circuit and fail
+  fast. In this state, the circuit breaker avoids invoking the protected operation and
+  avoids putting additional load on the struggling service.
+* *Half Open* — After a short period in the open state, an operation is attempted to
+  see whether it can complete successfully, and depending on the outcome, it will
+  transfer to either open or closed state.
+
+== Example
+
+Below is an example route showing a circuit breaker endpoint that protects against slow operation by falling back to the in-lined fallback route. By default the timeout request is just *1000ms* so the HTTP endpoint has to be fairly quick to succeed.
+[source,java]
+----
+from("direct:start")
+    .circuitBreaker()
+        .to("http://fooservice.com/slow")
+    .onFallback()
+        .transform().constant("Fallback message")
+    .end()
+    .to("mock:result");
+----
+
+And in XML DSL:
+[source,xml]
+----
+<camelContext xmlns="http://camel.apache.org/schema/spring">
+  <route>
+    <from uri="direct:start"/>
+    <circuitBreaker>
+      <to uri="http://fooservice.com/slow"/>
+      <onFallback>
+        <transform>
+          <constant>Fallback message</constant>
+        </transform>
+      </onFallback>
+    </circuitBreaker>
+    <to uri="mock:result"/>
+  </route>
+</camelContext>
+----
+
+== CircuitBreaker Implementations
+
+Camel provides two implementations of this pattern:
+
+* xref:hystrix-eip.adoc[Hystrix] - Using the Netflix Hystrix implementation
+* xref:resilience4j-eip.adoc[Resilience4j] - Using the Resilience4j implementation
+
diff --git a/core/camel-core-engine/src/main/docs/eips/hystrix-eip.adoc b/core/camel-core-engine/src/main/docs/eips/hystrix-eip.adoc
index 5323d15..2bd2fd5 100644
--- a/core/camel-core-engine/src/main/docs/eips/hystrix-eip.adoc
+++ b/core/camel-core-engine/src/main/docs/eips/hystrix-eip.adoc
@@ -111,7 +111,7 @@ You can find an example with the source code: https://github.com/apache/camel/tr
 
 == Using Hystrix with Spring Boot
 
-See the xref:hystrix-component.adoc[Hystrix Component].
+See the xref:components::hystrix.adoc[Hystrix Component].
 
 == Camel's Error Handler and Circuit Breaker EIP
 
diff --git a/core/camel-core-engine/src/main/docs/eips/resilience4j-eip.adoc b/core/camel-core-engine/src/main/docs/eips/resilience4j-eip.adoc
index 9a0ad62..7f23bda 100644
--- a/core/camel-core-engine/src/main/docs/eips/resilience4j-eip.adoc
+++ b/core/camel-core-engine/src/main/docs/eips/resilience4j-eip.adoc
@@ -112,7 +112,7 @@ You can find an example with the source code: https://github.com/apache/camel/tr
 
 == Using Resilience4j with Spring Boot
 
-See the xref:components::resilience4j-component.adoc[Resilience4j Component].
+See the xref:components::resilience4j.adoc[Resilience4j Component].
 
 == Camel's Error Handler and Circuit Breaker EIP
 
diff --git a/docs/user-manual/modules/ROOT/assets/images/eip/CircuitBreaker.png b/docs/user-manual/modules/ROOT/assets/images/eip/CircuitBreaker.png
new file mode 100644
index 0000000..5014eb2
Binary files /dev/null and b/docs/user-manual/modules/ROOT/assets/images/eip/CircuitBreaker.png differ
diff --git a/docs/user-manual/modules/ROOT/nav.adoc b/docs/user-manual/modules/ROOT/nav.adoc
index 719f335..1083741 100644
--- a/docs/user-manual/modules/ROOT/nav.adoc
+++ b/docs/user-manual/modules/ROOT/nav.adoc
@@ -65,7 +65,7 @@
  ** xref:batch-config-eip.adoc[Batch-config EIP]
  ** xref:bean-eip.adoc[Bean EIP]
  ** xref:choice-eip.adoc[Choice EIP]
- ** xref:circuitBreaker-eip.adoc[Circuit Breaker EIP (deprecated)]
+ ** xref:circuitBreaker-eip.adoc[CircuitBreaker EIP]
  ** xref:claimCheck-eip.adoc[Claim Check EIP]
  ** xref:content-based-router-eip.adoc[Content Based Router]
  ** xref:content-filter-eip.adoc[Content Filter]
diff --git a/docs/user-manual/modules/ROOT/pages/circuitBreaker-eip.adoc b/docs/user-manual/modules/ROOT/pages/circuitBreaker-eip.adoc
index f68c55d..b3c13e3 100644
--- a/docs/user-manual/modules/ROOT/pages/circuitBreaker-eip.adoc
+++ b/docs/user-manual/modules/ROOT/pages/circuitBreaker-eip.adoc
@@ -1,54 +1,62 @@
 [[circuitBreaker-eip]]
-= Circuit Breaker EIP (deprecated)
+= CircuitBreaker EIP
 :page-source: core/camel-core-engine/src/main/docs/eips/circuitBreaker-eip.adoc
 
-The Circuit Breaker load balancer is a stateful pattern that monitors all calls for certain exceptions. Initially the Circuit Breaker is in closed state and passes all messages. If there are failures and the threshold is reached, it moves to open state and rejects all calls until halfOpenAfter timeout is reached. After this timeout is reached, if there is a new call, it will pass and if the result is success the Circuit Breaker will move to closed state, or to open state if there was an error.
-When the circuit breaker is closed, it will throw a `java.util.concurrent.RejectedExecutionException`. This can then be caught to provide an alternate path for processing exchanges.
+The Circuit Breaker pattern is inspired by the real-world electrical circuit breaker,
+which is used to detect excessive current draw and fail fast to protect electrical equipment.
+The software-based circuit breaker works on the same notion, by encapsulating
+the operation and monitoring it for failures. The Circuit Breaker pattern operates in
+three states, as illustrated in the following figure:
 
-// eip options: START
-The Circuit Breaker EIP supports 3 options which are listed below:
+image::eip/CircuitBreaker.png[image]
 
-[width="100%",cols="2,5,^1,2",options="header"]
-|===
-| Name | Description | Default | Type
-| *exception* | A list of class names for specific exceptions to monitor. If no exceptions is configured then all exceptions is monitored |  | List
-| *halfOpenAfter* | The timeout in millis to use as threshold to move state from closed to half-open or open state |  | Long
-| *threshold* | Number of previous failed messages to use as threshold to move state from closed to half-open or open state |  | Integer
-|===
-// eip options: END
+The states are as follows:
 
+* *Closed* — When operating successfully.
+* *Open* — When failure is detected and the breaker opens to short-circuit and fail
+  fast. In this state, the circuit breaker avoids invoking the protected operation and
+  avoids putting additional load on the struggling service.
+* *Half Open* — After a short period in the open state, an operation is attempted to
+  see whether it can complete successfully, and depending on the outcome, it will
+  transfer to either open or closed state.
 
-An example using Java DSL:
+== Example
+
+Below is an example route showing a circuit breaker endpoint that protects against slow operation by falling back to the in-lined fallback route. By default the timeout request is just *1000ms* so the HTTP endpoint has to be fairly quick to succeed.
 [source,java]
 ----
 from("direct:start")
-    .onException(RejectedExecutionException.class)
-        .handled(true)
-        .to("mock:serviceUnavailable")
+    .circuitBreaker()
+        .to("http://fooservice.com/slow")
+    .onFallback()
+        .transform().constant("Fallback message")
     .end()
-    .loadBalance()
-        .circuitBreaker(2, 1000L, MyCustomException.class)
-        .to("mock:service")
-    .end();
+    .to("mock:result");
 ----
 
-And the same example using Spring XML:
+And in XML DSL:
 [source,xml]
 ----
-<camelContext id="camel" xmlns="http://camel.apache.org/schema/spring">
-    <route>
-        <from uri="direct:start"/>
-        <onException>
-            <exception>java.util.concurrent.RejectedExecutionException</exception>
-            <handled><constant>true</constant></handled>
-            <to uri="mock:serviceUnavailable"/>
-        </onException>
-        <loadBalance>
-            <circuitBreaker threshold="2" halfOpenAfter="1000">
-                <exception>MyCustomException</exception>
-            </circuitBreaker>
-            <to uri="mock:service"/>
-        </loadBalance>
-    </route>
+<camelContext xmlns="http://camel.apache.org/schema/spring">
+  <route>
+    <from uri="direct:start"/>
+    <circuitBreaker>
+      <to uri="http://fooservice.com/slow"/>
+      <onFallback>
+        <transform>
+          <constant>Fallback message</constant>
+        </transform>
+      </onFallback>
+    </circuitBreaker>
+    <to uri="mock:result"/>
+  </route>
 </camelContext>
 ----
+
+== CircuitBreaker Implementations
+
+Camel provides two implementations of this pattern:
+
+* xref:hystrix-eip.adoc[Hystrix] - Using the Netflix Hystrix implementation
+* xref:resilience4j-eip.adoc[Resilience4j] - Using the Resilience4j implementation
+
diff --git a/docs/user-manual/modules/ROOT/pages/hystrix-eip.adoc b/docs/user-manual/modules/ROOT/pages/hystrix-eip.adoc
index bd98db1..a2c4d62 100644
--- a/docs/user-manual/modules/ROOT/pages/hystrix-eip.adoc
+++ b/docs/user-manual/modules/ROOT/pages/hystrix-eip.adoc
@@ -11,7 +11,7 @@ Hystrix is one such implementation.
 
 Maven users will need to add the following dependency to their pom.xml to use this EIP:
 
-[source]
+[source,xml]
 ----
 <dependency>
     <groupId>org.apache.camel</groupId>
@@ -28,7 +28,7 @@ The Hystrix EIP supports 2 options which are listed below:
 [width="100%",cols="2,5,^1,2",options="header"]
 |===
 | Name | Description | Default | Type
-| *hystrixConfiguration* | Configures the Hystrix EIP Use end when configuration is complete, to return back to the Hystrix EIP. |  | HystrixConfiguration Definition
+| *hystrixConfiguration* | Configures the Hystrix EIP Use end when configuration is complete, to return back to the Hystrix EIP. |  | HystrixConfigurationDefinition
 | *hystrixConfigurationRef* | Refers to a Hystrix configuration to use for configuring the Hystrix EIP. |  | String
 |===
 // eip options: END
@@ -112,7 +112,7 @@ You can find an example with the source code: https://github.com/apache/camel/tr
 
 == Using Hystrix with Spring Boot
 
-See the xref:hystrix-component.adoc[Hystrix Component].
+See the xref:components::hystrix.adoc[Hystrix Component].
 
 == Camel's Error Handler and Circuit Breaker EIP
 
diff --git a/docs/user-manual/modules/ROOT/pages/resilience4j-eip.adoc b/docs/user-manual/modules/ROOT/pages/resilience4j-eip.adoc
index 46a6a8c..ad4c07f 100644
--- a/docs/user-manual/modules/ROOT/pages/resilience4j-eip.adoc
+++ b/docs/user-manual/modules/ROOT/pages/resilience4j-eip.adoc
@@ -23,6 +23,14 @@ Maven users will need to add the following dependency to their pom.xml to use th
 == Configuration options
 
 // eip options: START
+The Hystrix EIP supports 2 options which are listed below:
+
+[width="100%",cols="2,5,^1,2",options="header"]
+|===
+| Name | Description | Default | Type
+| *resilienceConfiguration* | Configures the Resilience EIP Use end when configuration is complete, to return back to the Resilience EIP. |  | ResilienceConfigurationDefinition
+| *resilienceConfigurationRef* | Refers to a Resilience configuration to use for configuring the Resilience EIP. |  | String
+|===
 // eip options: END
 
 See xref:resilience4jConfiguration-eip.adoc[Resilience4j Configuration] for all the configuration options on Resilience Circuit Breaker.
@@ -60,11 +68,10 @@ And in XML DSL:
 </camelContext>
 ----
 
-== Configuring Resilienc4j
+== Configuring Resilience4j
 
 You can fine-tune Resilience4j by the many xref:resilience4jConfiguration-eip.adoc[Resilience4j Configuration] options.
 
-TODO: Update example!!!
 For example to use a 2 second execution timeout, you can do as follows:
 
 [source,java]
@@ -72,12 +79,12 @@ For example to use a 2 second execution timeout, you can do as follows:
 from("direct:start")
     .circuitBreaker()
         // use 2 second timeout
-        .hystrixConfiguration().executionTimeoutInMilliseconds(2000).end()
-        .log("Hystrix processing start: ${threadName}")
+        .resilience4jConfiguration().timeoutEnabled(true).timeoutDuration(2000).end()
+        .log("Resilience processing start: ${threadName}")
         .toD("direct:${body}")
-        .log("Hystrix processing end: ${threadName}")
+        .log("Resilience processing end: ${threadName}")
     .end()
-    .log("After Hystrix ${body}");
+    .log("After Resilience ${body}");
 ----
 
 And in XML:
@@ -87,12 +94,12 @@ And in XML:
 <route>
   <from uri="direct:start"/>
   <circuitBreaker>
-    <hystrixConfiguration executionTimeoutInMilliseconds="2000"/>
-    <log message="Hystrix processing start: ${threadName}"/>
+    <resilience4jConfiguration timeoutEnabled="true" timeoutDuration="2000"/>
+    <log message="Resilience processing start: ${threadName}"/>
     <toD uri="direct:${body}"/>
-    <log message="Hystrix processing end: ${threadName}"/>
+    <log message="Resilience processing end: ${threadName}"/>
   </circuitBreaker>
-  <log message="After Hystrix: ${body}"/>
+  <log message="After Resilience: ${body}"/>
 </route>
 ----
 
@@ -102,11 +109,11 @@ See xref:onFallback-eip.adoc[onFallback].
 
 == Other examples
 
-You can find an example with the source code: https://github.com/apache/camel/tree/master/examples/camel-example-hystrix[camel-example-hystrix].
+You can find an example with the source code: https://github.com/apache/camel/tree/master/examples/camel-example-resilience4j[camel-example-resilience4j].
 
 == Using Resilience4j with Spring Boot
 
-See the xref:components::resilience4j-component.adoc[Resilience4j Component].
+See the xref:components::resilience4j.adoc[Resilience4j Component].
 
 == Camel's Error Handler and Circuit Breaker EIP