简体   繁体   中英

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?

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

Suppose you have a microservice named "LoginServer" now, let's see how to register this service with discovery server (Eureka Server) at startup.

Here Spring Boot startup class of LoginServer.java:

@EnableAutoConfiguration
@EnableDiscoveryClient
public class LoginServer {
public static void main(String[] args) {

     // Will configure using login-server.yml

     System.setProperty("spring.config.name", "login-server");
     SpringApplication.run(LoginServer.class, args);

  }
}

The @EnableDiscoveryClient - enables service registration and discovery. In this case, this process registers itself with the discovery-server service using its application name, that is configured in YML configuration file.

let's see the complete setup:

First create a login-server.yml (any name but extension should be .yml) file into src/main/resources package folder. And write those configurations and save.

# Spring properties
spring:
 application:
   name: login-server # This name used as ID so ("spring.config.name", 
                      #"login-server"); must be same.

# Discovery Server Access
eureka:
 client:
  serviceUrl:
   defaultZone: http://localhost:1111/eureka/

# HTTP Server
server:
 port: 2222   # HTTP (Tomcat) port

Run the LoginServer and let it finish initializing. Open the dashboard by putting this URL http://localhost:1111 in your favorite browser and refresh. After few seconds later you should see the LOGIN-SERVER. Generally registration takes up to 30 seconds (by default) so wait or restart.

And this is the microservice complete registration process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM