Search code examples
spring-bootmicroservicesnetflix-eurekaspring-cloud-netflix

How to register spring boot microservices on spring cloud Netflix eureka?


We were planning to use spring cloud Netflix oss components. So I was doing a small sample project. I developed 2 spring microservices and those services runs well on http://localhost:9000/microsvc-one http://localhost:9001/microsvc-two

And also wrote a sample spring cloud etflix eureka maven project which runs well on http://localhost:8761

I used annotations @EurekaDiscoveryClient and @SpringBootApplication on both the spring boot microservices main class

I used annotation @EnableEurekaServer and @SpringBootApplication

Now I am facing a problem in registering those services in eureka server. I referred some samples. I am not understanding those. I did some changes in application.yml files of microsvc-one and microsvc-two and also application.yml file of eureka server. But still it shows empty.

What all changes are required or missing or correct configuration to be done so that my services are being registered on eureka.

I also have other question like do i need to create a separate project which has @EnableConfigServer and @SpringBootApplication Annotations other than the above 2 microservices and eureka server project module to register the services on eureka. I see those in most of the examples.

If yes..how do we link between all these?


Solution

  • If you are using springBoot application you will need the annotaion @SpringBootApplication thats why that annotation is there on the project you are seeing. @EnableConfigServer is when you are using the spring-cloud config server it is used to externalize the configuration properties but since you have the application.yml inside the project so you donot need that either.

    I am thinking you have a spring boot application for both Microservices and the Eureka server. You need to annotate the eureka main class with

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

    Additionally you need annotate you microservice's main class with..

    @SpringBootApplication
    @EnableDiscoveryClient
    public class MicroApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(MicroApplication.class, args);
        }
    }
    

    Since you donot have you application.yml file in the question here is what you need.

    You need the below configuration in application.yml of the microservices.

    eureka:
      client:
        serviceUrl:
          defaultZone: ${eurekaurl:http://localhost:8761/eureka/} 
    

    In the Eureka Server application.yml file I have this in mine. you might need to tweak it based on what you want.

    info:
      component: Registry Server
    
    server: 
      port: ${port:8761}
    
    
    eureka:
      client:
        registerWithEureka: false
        fetchRegistry: false
      server:
        enable-self-preservation: false
        waitTimeInMsWhenSyncEmpty: 0
      instance:
        hostname: localhost
        lease-expiration-duration-in-seconds: 15
        lease-renewal-interval-in-seconds: 5