Spring Cloud Eureka Server

Keine Kommentare
Spring Cloud

One of the youngest children in the Spring stack is Spring Cloud which bundles tools to organize and manage distributed systems. There exist components being self developed by the Spring team like Spring Cloud Config which offer a centralized configuration server.
On the other hand the Spring team included components from the Netflix Open Source Software Center and embeds them in the new and easy annotation fashion known from Spring Boot. One of the main Netflix components is Eureka, a REST based service registration and discovery service.

This post is a very simple tutorial showing how to produce an Eureka server with Spring Cloud. The source code is available on GitHub.

Maven configuration

The Maven configuration for this project is very straight-forward:

  • import the Spring Cloud starter pom for Maven dependency settings
  • add a dependency to the Spring Cloud starter Eureka server
  • extend the Spring Boot Maven plugin configuration
  • add milestone repository
Last step is needed while Spring Cloud isn't released in 1.0 yet.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>cnagel</groupId>
  <artifactId>spring-eureka-server-example</artifactId>
  <version>0.0.2-SNAPSHOT</version>
  <packaging>jar</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <spring-cloud.version>1.0.0.RC1</spring-cloud.version>
    <spring-boot.version>1.2.0.RELEASE</spring-boot.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>${spring-boot.version}</version>
        <configuration>
          <requiresUnpack>
            <dependency>
              <groupId>com.netflix.eureka</groupId>
              <artifactId>eureka-core</artifactId>
            </dependency>
            <dependency>
              <groupId>com.netflix.eureka</groupId>
              <artifactId>eureka-client</artifactId>
            </dependency>
          </requiresUnpack>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <repositories>
    <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>http://repo.spring.io/milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

</project>

Java Application class

While everything is pre-configured by Spring Cloud it just needs to add some annotations to the application class. @ComponentScan, @Configuration and @EnableAutoConfiguration are typically Spring annotations initiating a Spring app the Boot way. @EnableEurekaServer loads the Eureka server.

package cnagel.spring_eureka_server_example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@ComponentScan
@Configuration
@EnableAutoConfiguration
@EnableEurekaServer
public class EurekaServerApp {

 public static void main(String[] args) {
  SpringApplication.run(EurekaServerApp.class, args);
 }

}

Application properties

Properties are defined in the application.yml which is loaded automatically by Spring Boot.

spring:
  application:
    name: eureka-server

server:
  port: 8761

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0
  
logging:
  level:
    com.netflix: 'WARN'
    org.springframework.cloud: 'WARN'

Starting the Eureka server

Last but not least the server gets started by executing mvn spring-boot:run. There exists a dashboard on http://localhost:8761 giving useful information about the Eureka server. The REST representation of registered services is available at http://localhost:8761/eureka/apps.

Keine Kommentare :

Kommentar veröffentlichen

Hinweis: Nur ein Mitglied dieses Blogs kann Kommentare posten.