Spring BootEureka Server
Spring Boot

Eureka Server

Eureka Server is Netflix's service registry, integrated into Spring Cloud. It maintains a real-time map of all running microservice instances. Services register on startup, send periodic heartbeats to confirm liveness, and deregister on shutdown. Clients query the registry to discover peer services without hard-coded addresses.

Setting Up Eureka Server

A Eureka Server is a standard Spring Boot application with the eureka-server starter and the @EnableEurekaServer annotation. It exposes a REST API that clients use to register and query, and a web dashboard at / that shows all registered instances in real time.
XML
<!-- pom.xml: -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <!-- Optional: secure the dashboard -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2023.0.3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

Eureka Server Application Class and Configuration

Add @EnableEurekaServer to the main class. The server should not register with itself or attempt to fetch its own registry — set register-with-eureka and fetch-registry to false to prevent this.
yaml
// ── Main application class: ──────────────────────────────────────────
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

// ── application.yml: ──────────────────────────────────────────────────
// server:
//   port: 8761                       # Eureka default port (convention)
//
// spring:
//   application:
//     name: eureka-server
//
// eureka:
//   instance:
//     hostname: localhost
//   client:
//     register-with-eureka: false    # don't register the server with itself
//     fetch-registry: false          # server doesn't need a local copy
//     service-url:
//       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
//
//   server:
//     enable-self-preservation: false  # disable in dev (avoid stale entries)
//     eviction-interval-timer-in-ms: 5000

// ── What Eureka Server exposes: ───────────────────────────────────────
// GET  /eureka/apps              → all registered applications (XML/JSON)
// GET  /eureka/apps/{appName}    → instances of a specific service
// GET  /eureka/apps/{app}/{id}   → single instance info
// POST /eureka/apps/{appName}    → register an instance
// PUT  /eureka/apps/{app}/{id}   → heartbeat (renew lease)
// DELETE /eureka/apps/{app}/{id} → deregister instance
//
// Web dashboard:  http://localhost:8761/
// Shows: registered services, instance count, status, IP, port, metadata.

Securing Eureka Server

By default, Eureka Server is open — any service can register. In production, secure it with HTTP Basic Auth via Spring Security so only authorised services can register or query the registry.
Java
// ── Security configuration on the Eureka Server: ─────────────────────
@Configuration
@EnableWebSecurity
public class EurekaSecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(
            HttpSecurity http) throws Exception {
        http
            .csrf(csrf -> csrf
                // Disable CSRF for Eureka REST endpoints
                // (clients use Basic Auth, not browser sessions)
                .ignoringRequestMatchers("/eureka/**"))
            .authorizeHttpRequests(auth -> auth
                .anyRequest().authenticated())
            .httpBasic(Customizer.withDefaults());
        return http.build();
    }

    @Bean
    public UserDetailsService users() {
        UserDetails admin = User.withDefaultPasswordEncoder()
            .username("eureka-admin")
            .password("secret")
            .roles("SYSTEM")
            .build();
        return new InMemoryUserDetailsManager(admin);
    }
}

// ── application.yml (Eureka Server with security): ───────────────────
// spring:
//   security:
//     user:
//       name: eureka-admin
//       password: secret

// ── Clients must include credentials in the service-url: ─────────────
// (in each microservice's application.yml)
// eureka:
//   client:
//     service-url:
//       defaultZone: http://eureka-admin:secret@localhost:8761/eureka/
//                          ↑─── credentials embedded in URL

High Availability — Peer Eureka Servers

A single Eureka Server is a single point of failure. In production, run two or more Eureka Server instances that replicate registrations to each other (peer replication). Each server registers with the other peers so they stay in sync. Clients are configured with all peer URLs and fall back to their local cache if all servers become unreachable.
yaml
// ── Two-node Eureka cluster: ─────────────────────────────────────────
//
//   eureka-server-1 (host: eureka1, port: 8761)
//       ↕ peer replication
//   eureka-server-2 (host: eureka2, port: 8762)
//
//  Both nodes share the same registration data.
//  If eureka1 goes down, clients continue using eureka2.

// ── application.yml for eureka-server-1: ─────────────────────────────
// spring:
//   profiles:
//     active: peer1
//
// ---
// spring:
//   config:
//     activate:
//       on-profile: peer1
//   application:
//     name: eureka-server
//
// server:
//   port: 8761
//
// eureka:
//   instance:
//     hostname: eureka1
//   client:
//     register-with-eureka: true     # peers DO register with each other
//     fetch-registry: true
//     service-url:
//       defaultZone: http://eureka2:8762/eureka/   # point at peer

// ── application.yml for eureka-server-2: ─────────────────────────────
// spring:
//   config:
//     activate:
//       on-profile: peer2
//
// server:
//   port: 8762
//
// eureka:
//   instance:
//     hostname: eureka2
//   client:
//     register-with-eureka: true
//     fetch-registry: true
//     service-url:
//       defaultZone: http://eureka1:8761/eureka/   # point at peer

// ── Microservice clients point at ALL peers: ──────────────────────────
// eureka:
//   client:
//     service-url:
//       defaultZone: >
//         http://eureka1:8761/eureka/,
//         http://eureka2:8762/eureka/
// If eureka1 is down, client automatically uses eureka2.
// If both are down, client uses its local in-memory cache.