hazelcast / hazelcast-eureka

Hazelcast Discovery SPI Plugin for Netflix' Eureka Service Discovery V1
Other
30 stars 27 forks source link

Hazelcast Discovery Plugin for Eureka

This repository contains a plugin which provides the automatic Hazelcast member discovery using Eureka Service Registry.

NOTE: hazelcast-eureka-two 3.x is compatible with Eureka 2.x and Hazelcast 5.x, for older hazelcast versions you need to use eureka-one branch.

Embedded mode

To use Hazelcast embedded in your application, you need to add the plugin dependency into your Maven/Gradle file. Then, when you provide hazelcast.xml (and hazelcast-client.properties) as presented below, your Hazelcast instances will use Eureka Server to discover each other automatically.

Maven

<dependency>
  <groupId>com.hazelcast</groupId>
  <artifactId>hazelcast-eureka-two</artifactId>
  <version>${hazelcast-eureka-version}</version>
</dependency>

Gradle

compile group: "com.hazelcast", name: "hazelcast-eureka-two", version: "${hazelcast-eureka-version}"

Configuration

Make sure you have:

Hazelcast Configuration

XML Configuration

<hazelcast>
  <network>
    <join>
      <multicast enabled="false"/>
      <eureka enabled="true">
        <self-registration>true</self-registration>
        <namespace>hazelcast</namespace>
      </eureka>
    </join>
  </network>
</hazelcast>

Java-based Configuration

config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
config.getNetworkConfig().getJoin().getEurekaConfig().setEnabled(true)
      .setProperty("self-registration", "true")
      .setProperty("namespace", "hazelcast");

The following properties can be configured:

Eureka Client Configuration

Properties File

Below you can also find an example of eureka-client.properties.

hazelcast.shouldUseDns=false
hazelcast.name=hazelcast-test
hazelcast.serviceUrl.default=http://<your-eureka-server-url>

Note: hazelcast.name property is crucial for cluster members to discover each other. Please give the identical names in regarding eureka-client.properties on each Hazelcast member.

Embedded Properties

In some environments adding the eureka-client.properties file to the classpath is not desirable or feasible. To support this use case, it is possible to specify the Eureka client properties in the Hazelcast configuration. Set the use-classpath-eureka-client-props property to false, then add the Eureka client properties without prepending the namespace, as they will be applied to the namespace specified with the namespace property.

NOTE: If use-classpath-eureka-client-props is true (its default value), all Eureka client properties in the Hazelcast configuration will be ignored.

The following is an example declarative configuration, equivalent to the example given above.

<hazelcast>
  <network>
    <join>
      <multicast enabled="false"/>
      <eureka enabled="true">
        <self-registration>true</self-registration>
        <namespace>hazelcast</namespace>
        <use-classpath-eureka-client-props>false</use-classpath-eureka-client-props>
        <shouldUseDns>false</shouldUseDns>
        <name>hazelcast-test</name>
        <serviceUrl.default>http://your-eureka-server-url</serviceUrl.default>
      </eureka>
    </join>
  </network>
</hazelcast>

Hazelcast Client Configuration

Configuring Hazelcast Client follows exactly the same steps as configuring Hazelcast member, so you need to have:

Following are example declarative and programmatic configuration snippets.

XML Configuration

<hazelcast-client>
  <network>
    <eureka enabled="true">
      <namespace>hazelcast</namespace>
    </eureka>
  </network>
</hazelcast-client>

Java-based Configuration

clientConfig.getEurekaConfig().setEnabled(true)
            .setProperty("namespace", "hazelcast");

Note: Hazelcast clients do not register themselves to Eureka server, therefore self-registration property has no effect.

Note: The eureka-client.properties file and the eureka.client.props dynamic property mechanism work exactly the same as described in the Hazelcast Member Configuration.

Reusing existing EurekaClient instance

If your application already provides a configured EurekaClient instance e.g. if you are using Spring Cloud, you can reuse your existing client:

EurekaClient eurekaClient = ...
EurekaOneDiscoveryStrategyFactory.setEurekaClient(eurekaClient);
EurekaOneDiscoveryStrategyFactory.setGroupName("dev"); // optional group name. Default is 'dev'.

Note that if you have Hazelcast embedded in your application and you want to use Eureka Service discovery for both the application itself and Hazelcast, by default they collide. You can solve this issue in one of the following manners:

Please note that If you use metadata to store cluster member addresses on Eureka application "hazelcast" then the discovery plugin will only find the host and port stored in the metadata for that app, not for any other Eureka app.

How to find us?

In case of any question or issue, please raise a GH issue or contact as directly via Hazelcast Community Slack.