In micro service architecture, Discovery server is a key component.
As we don't have to build everything from the scratch, we have Eureka server which can use very easily with very much less code.
1. Create java project
Create the project using starter io ,
https://start.spring.io/
Make sure you do this as a habit, Do not create project manually with creating pom file etc. That was old fashion and get use to use the available resources.
Here you can give
- Project type - Maven , Gradle
- Language =Java, Groovy, Kotlin
- Spring Boot version = 2.2. M5 , 2.17 etc
- Project meta data = which used give in creating pom file
- group - com.test.micro
- artifact - eurekaServer
- Description - Demo project for Eureka Server
- package name - com.test.micro.eurekaServer
- packaging - Jar | War
- java version - 12, 11, 8
- dependencies = Eureka server , Actuator, devtools
2. download the project and open with eclipse and run
This will give some errors
It will try to behave as a Eureka Client
use "@EnableEurekaServer" to make this a Eureka server
package com.saap.micro.eurekaServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
As we have not given any properties to configure, This will start with default configs
taking port 8080 as default port
you can acess the eureka server via below url
http://localhost:8080/
As we have no any running clients, we will have an empty dashboard.
Now let's add an yml file and configure the Discovery server as we want.
Create "application.yml" file inside "resources" folder ( eurekaServer\src\main\resources)
eureka:
instance:
hostname: localhost
client: # Not a client, don't register with yourself
registerWithEureka: false
fetchRegistry: false
server:
port: 1111 # HTTP (Tomcat) port
you will get the console out as below - see 1111 newly created port
you can see the port in the console out put also
================================================================
INFO 18152 --- [ Thread-16] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
INFO 18152 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 1111 (http) with context path ''
INFO 18152 --- [ restartedMain] .s.c.n.e.s.EurekaAutoServiceRegistration :
Updating port to 1111
INFO 18152 --- [ restartedMain] c.s.m.e.EurekaServerApplication : Started EurekaServerApplication in 11.818 seconds (JVM running for 12.9)
INFO 18152 --- [io-1111-exec-10] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO 18152 --- [io-1111-exec-10] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
INFO 18152 --- [io-1111-exec-10] o.s.web.servlet.DispatcherServlet : Completed initialization in 17 ms
package com.saap.micro.eurekaServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}