Home > AI > Backend > SpringBoot > Eureka >

Integrate Eureka into SpringBoot application

Step 1: create a multi-module project

Step 2: create a standlone SpringBoot module as Eureka Server

Choose dependency Eureka Server

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

application.yml

server:
  port: 8761

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false

Add @EnableEurekaServer to the Application.java

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

Step 3: create a standlone SpringBoot module as Eureka Client

<dependency>
	<groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

application.yml

spring:
  application:
    name: pte-eureka-client

server:
  port: 8888


eureka:
  client:
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
  instance:
    preferIpAddress: true

Modify the Application.java

@SpringBootApplication
@RestController
public class EurekaClientApplication {

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



    @Autowired
    @Lazy
    private EurekaClient eurekaClient;

    @Value("${spring.application.name}")
    private String appName;

    @GetMapping("/greeting")
    public String greeting() {
        return String.format(
                "Hello from '%s'!", eurekaClient.getApplication(appName).getName());
    }


}
Related posts:
    No posts found.

Leave a Reply