Questo contenuto non è disponibile nella lingua selezionata.
Chapter 34. Fabric Component
Abstract
Dependencies Copia collegamentoCollegamento copiato negli appunti!
fabric-camel feature is installed. If necessary, you can install it using the following console command:
karaf@root> features:install fabric-camel
fabric-camel feature is installed by including it in your feature definition. For example:
<?xml version="1.0" encoding="UTF-8"?>
<features>
<feature name="fabric-component-example">
<feature>fabric-camel</feature>
<bundle>URIforMyBundle</bundle>
<!-- Specify any other required bundles or features -->
...
</feature>
</features>
URI format Copia collegamentoCollegamento copiato negli appunti!
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 producer 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 Copia collegamentoCollegamento copiato negli appunti!
- 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 Copia collegamentoCollegamento copiato negli appunti!
Location discovery Copia collegamentoCollegamento copiato negli appunti!
Figure 34.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 Copia collegamentoCollegamento copiato negli appunti!
Figure 34.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 Copia collegamentoCollegamento copiato negli appunti!
Publishing an endpoint URI Copia collegamentoCollegamento copiato negli appunti!
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 34.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
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<reference id="curator" interface="org.apache.curator.framework.CuratorFramework"/>
<bean id="fabric-camel" class="io.fabric8.camel.FabricComponent">
<property name="curator" ref="curator"/>
</bean>
<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>
- A reference to the
org.apache.curator.framework.CuratorFrameworkOSGi service is created using thereferenceelement. Apache Curator is a ZooKeeper client component that manages connections to the ZooKeeper server, providing a high quality of service (including automatic reconnection).NoteTheCuratorFrameworkOSGi service is provided by the fabric agent. Hence, a fabric agent must already be installed and running in the container where the route is deployed. - The Fabric component uses the
CuratorFrameworkobject to connect to the ZooKeeper server (Fabric registry). - 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 Copia collegamentoCollegamento copiato negli appunti!
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 34.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
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
<reference id="curator" interface="org.apache.curator.framework.CuratorFramework"/>
<bean id="fabric-camel" class="io.fabric8.camel.FabricComponent">
<property name="curator" ref="curator"/>
</bean>
<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>
org.apache.curator.framework.CuratorFramework OSGi service, which the Fabric component uses to connect to the underlying fabric.
src/main/resources/OSGI-INF/blueprint directory of a Maven project.
Load-balancing example Copia collegamentoCollegamento copiato negli appunti!
Example 34.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
http://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"/>
<reference id="curator" interface="org.apache.curator.framework.CuratorFramework"/>
<bean id="fabric-camel" class="io.fabric8.camel.FabricComponent">
<property name="curator" ref="curator"/>
</bean>
<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 Copia collegamentoCollegamento copiato negli appunti!
Import-Package bundle header must be configured to import the following Java packages:
io.fabric8.zookeeper.spring
io.fabric8.zookeeper
Example 34.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.spring,
io.fabric8.zookeeper,
*
</Import-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>
...
</project>