Wednesday, June 14, 2023

MS - chapter 3 - Register services with Eureka

 Tech Stack

Java 8, Maven, Springboot 3.10 , 

Scenario

Here we will register create web services with Eureka server


1. Add Eureka client dependency

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

Here if dependency cannot download add version under properties

<properties>
<java.version>17</java.version>
<spring-cloud.version>2022.0.3</spring-cloud.version>
</properties>

And the dependency Management to download

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


2. Inject Eureka client dependency at main class - register service with Eureka server

Here you can set the name of the service also

@Autowired
@Lazy
private EurekaClient eurekaClient;

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


3. Register Service With Eureka server / Add service as a Eureka client

Add below properties

Note: My service name is "student"

spring.application.name=student
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

4. You can start the client. Now your client will be registered with the Eureka Server.

Note: If you are using a database, make sure it is up and running

In here, we are using Mysql.

If it is not running you can start using command "mysqld --console"


Below is the Important Code base


Main Class

package com.demo.micro.student;

import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Lazy;


@SpringBootApplication
public class StudentApplication {

@Autowired
@Lazy
private EurekaClient eurekaClient;

@Value("${spring.application.name}")
private String appName;
public static void main(String[] args) {
SpringApplication.run(StudentApplication.class, args);
}

}


application.properties for your reference

server.port=8203
spring.datasource.url=jdbc:mysql://localhost:3306/student_mgt
spring.datasource.username=root
spring.datasource.password=root

# for Spring Boot 2
# spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
# for Spring Boot 3
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQLDialect
spring.jpa.show-sql=false
spring.jpa.generate-ddl=true
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.application.name=student
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

References

https://cloud.spring.io/spring-cloud-netflix/multi/multi__service_discovery_eureka_clients.html

https://www.baeldung.com/spring-cloud-netflix-eureka

No comments:

Post a Comment