Ce contenu n'est pas disponible dans la langue sélectionnée.
Chapter 47. Fabric Component
Abstract
Dependencies Copier lienLien copié sur presse-papiers!
fabric-camel feature is installed. In the context of Fabric, you install a feature by adding it to the relevant profile. For example, if you are using a profile called my-master-profile, you would add the fabric-camel feature by entering the following console command:
karaf@root> fabric:profile-edit --features fabric-camel my-master-profile
fabric-zookeeper and fabric-commands. However, If you are using jetty end-point, then include the camel-jetty feature.
URI format Copier lienLien copié sur presse-papiers!
fabric:ClusterID[:PublishedURI[?Options]]
fabric:ClusterID:PublishedURI[?Options]
PublishedURI, is published in the fabric registry and associated with the ClusterId cluster. The options, Options, are used when creating the consumer endpoint instance, but the options are not published with the PublishedURI in the fabric registry.
fabric:ClusterID
ClusterId, in the fabric registry to discover the URI to connect to.
URI options Copier lienLien copié sur presse-papiers!
- Server-only options—options that are applicable only to the server are applied to the server endpoint (consumer endpoint) at run time.
- Client-only options—options that are applicable only to the client are applied to the client endpoint (producer endpoint) at run time.
- Common options—options common to the client and the server are applied to both.
Use cases for fabric endpoints Copier lienLien copié sur presse-papiers!
Location discovery Copier lienLien copié sur presse-papiers!
Figure 47.1. Location Discovery through Fabric
jetty:http://0.0.0.0:9090. When this route is started, it automatically registers the Jetty URI in the fabric registry, under the cluster ID, foo.
fabric:foo. Now, when the client route starts, it automatically looks up the ID, foo, in the fabric registry and retrieves the associated Jetty endpoint URI. The client then creates a producer endpoint using the discovered Jetty URI and connects to the corresponding server port.
Load-balancing cluster Copier lienLien copié sur presse-papiers!
Figure 47.2. Load Balancing through Fabric
jetty:http://0.0.0.0:9090 and jetty:http://0.0.0.0:9191. Because these published URIs are both prefixed by fabric:foo:, both of the Jetty URIs are registered under the same cluster ID, foo, in the fabric registry.
foo, in the fabric registry. Because the foo ID is associated with multiple endpoint URIs, fabric implements a random load balancing algorithm to choose one of the available URIs. The client then creates a producer endpoint, using the chosen URI.
Auto-reconnect feature Copier lienLien copié sur presse-papiers!
Publishing an endpoint URI Copier lienLien copié sur presse-papiers!
PublishedURI, in the fabric registry, define a fabric endpoint with the publisher syntax, FabricScheme:ClusterID:PublishedURI. Note that this syntax can only be used in a consumer endpoint (that is, an endpoint that appears in a from DSL command).
cluster. The route is a simply HTTP server that returns the constant message, Response from Zookeeper agent, in the body of the HTTP response.
Example 47.1. Publishing a URI
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0
https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<bean id="fabric-camel" class="io.fabric8.camel.FabricComponent"/>
<camelContext id="camel" trace="false" xmlns="http://camel.apache.org/schema/blueprint">
<route id="fabric-server">
<from uri="fabric-camel:cluster:jetty:http://0.0.0.0:9090/fabric"/>
<log message="Request received : ${body}"/>
<setHeader headerName="karaf.name">
<simple>${sys.karaf.name}</simple>
</setHeader>
<transform>
<simple>Response from Zookeeper agent</simple>
</transform>
</route>
</camelContext>
</blueprint>
- The Fabric component uses the
CuratorFrameworkobject to connect to the ZooKeeper server (Fabric registry), where the reference to theCuratorFrameworkobject is provided automatically. - The
fromDSL command defines the fabric URI,fabric-camel:cluster:jetty:http://0.0.0.0:9090/fabric. At run time, this causes two things to happen:- The specified
jettyURI is published to the fabric registry under the cluster ID,cluster. - The Jetty endpoint is activated and used as the consumer endpoint of the route (just as if it had been specified without the
fabric-camel:cluster:prefix).
src/main/resources/OSGI-INF/blueprint directory of a Maven project.
Looking up an endpoint URI Copier lienLien copié sur presse-papiers!
FabricScheme:ClusterID. This syntax is used in a producer endpoint (for example, an endpoint that appears in a to DSL command).
cluster, in the fabric registry.
Example 47.2. Looking up a URI
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0
https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<bean id="fabric-camel" class="io.fabric8.camel.FabricComponent"/>
<camelContext id="camel" trace="false" xmlns="http://camel.apache.org/schema/blueprint">
<route id="fabric-client">
<from uri="timer://foo?fixedRate=true&period=10000"/>
<setBody>
<simple>Hello from Zookeeper server</simple>
</setBody>
<to uri="fabric-camel:cluster"/>
<log message=">>> ${body} : ${header.karaf.name}"/>
</route>
</camelContext>
<reference interface="org.apache.camel.spi.ComponentResolver"
filter="(component=jetty)"/>
</blueprint>
src/main/resources/OSGI-INF/blueprint directory of a Maven project.
Load-balancing example Copier lienLien copié sur presse-papiers!
Example 47.3. Server Template for a Load-Balancing Cluster
<?xml version="1.0" encoding="UTF-8"?>
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0"
xsi:schemaLocation="
http://www.osgi.org/xmlns/blueprint/v1.0.0
https://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<!-- osgi blueprint property placeholder -->
<cm:property-placeholder
id="myConfig"
persistent-id="io.fabric8.examples.camel.loadbalancing.server"/>
<bean id="fabric-camel" class="io.fabric8.camel.FabricComponent"/>
<camelContext id="camel" trace="false" xmlns="http://camel.apache.org/schema/blueprint">
<!-- using Camel properties component and refer
to the blueprint property placeholder by its id -->
<propertyPlaceholder id="properties"
location="blueprint:myConfig"
prefixToken="[[" suffixToken="]]"/>
<route id="fabric-server">
<from uri="fabric-camel:cluster:jetty:http://0.0.0.0:[[portNumber]]/fabric"/>
<log message="Request received : ${body}"/>
<setHeader headerName="karaf.name">
<simple>${sys.karaf.name}</simple>
</setHeader>
<transform>
<simple>Response from Zookeeper agent</simple>
</transform>
</route>
</camelContext>
</blueprint>
io.fabric8.examples.camel.loadbalancing.server persistent ID. A persistent ID in the OSGi Config Admin service identifies a collection of related property settings. After initializing the property placeholder, you can access any property values from the persistent ID using the syntax, [[PropName]].
[[portNumber]], at run time. At deploy time, you can specify the value of the portName property. For example, if using a custom feature, you could specify the property in the feature definition (see Add OSGi configurations to the feature). Alternatively, you can specify configuration properties when defining deployment profiles in the Fuse Management Console.
OSGi bundle plug-in configuration Copier lienLien copié sur presse-papiers!
Import-Package bundle header must be configured to import the following Java packages:
io.fabric8.zookeeper
Example 47.4. Maven Bundle Plug-In Configuration
<project ... >
...
<build>
<defaultGoal>install</defaultGoal>
<plugins>
...
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
<Import-Package>
io.fabric8.zookeeper,
*
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
...
</project>