Home > Mobile >  How to manually inject Eureka Server?
How to manually inject Eureka Server?

Time:02-22

I try to manually inject Eureka Server in a Spring Boot project to mimic the effects of @EnableEurekaServer. I tried:

@Bean
@ConditionalOnProperty(
   value = "sd.mode",
   havingValue = "Server")
public EurekaServerAutoConfiguration startServer() {
        return new EurekaServerAutoConfiguration();
}

and

@EnableEurekaServer
@Configuration
@ConditionalOnProperty(
        value = "sd.mode",
        havingValue = "Server")
public class SDServer {
}

None worked as expected. Is there a proper way to do this?


Edit: to clarify the goal of this:

I want to enhance a Spring Boot application with service discovery abilities. The first step is to make the jar configurable, depending on its role. This works with a lot of components, just Eureka gives me trouble. The goal is:

(in application.properties)

sd.mode=server //Application loads Eureka Server
sd.mode=client //Application runs as Eureka Client
sd.mode=off //Application does not use Eureka

As an example for a working component:

@Configuration
public class OutputConfiguration {

    @Bean
    @ConditionalOnProperty(
            value = "output",
            havingValue = "ConsoleLogger",
            matchIfMissing = false)
    public Output<String, String> createConsoleLogger() {
        return new ConsoleLogger();
    }

    @Bean
    @ConditionalOnProperty(
            value = "output",
            havingValue = "TCPSocketWriter",
            matchIfMissing = false)
    public Output<String, String> createTCPSocketWriter() {
        return new TCPSocketWriter();
    }

}

With this, I can access Output and use the Instance I declared in application properties. I want to do something similar with Eureka.


Edit2: my try on M. Deinums solution (if I understood it correctly), did not show any difference.

public class SDServer {
    @Bean
    public SDServerConfig createSDServerConfig(){
        return new SDServerConfig();
    }
    @Configuration
    @EnableEurekaServer
    class SDServerConfig{

    }
}

run by

@Configuration
public class ServiceDiscovery {

    @Bean
    @ConditionalOnProperty(
            value = "sd.mode",
            havingValue = "Server",
            matchIfMissing = false)
    public SDServer startServer() {
        return new SDServer();
    }

    @Bean
    @ConditionalOnProperty(
            value = "sd.mode",
            havingValue = "Client",
            matchIfMissing = false)
    public SDClient enableClient() {
        return new SDClient();
    }
}

Btw, SDClient with

@EnableDiscoveryClient
public class SDClient {
}

works as expected.

CodePudding user response:

Do you really need the configuration or bean annotation? The general way is to do it in the main class like the following code

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}

I am not sure what you want to achieve.

CodePudding user response:

I suppose you can use eureka server and client. In the eureka server project add the spring-cloud-starter-netflix-eureka-client dependency and use the following annotations in the main class.

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0
  instance:
    hostname: localhost 

In the yml file server: port: 8761

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0
  instance:
    hostname: localhost

And for the other services that you want to register in the server, you must also use the same dependency. However, you must use the following lines on the config file

spring.application.name=app-name
eureka.instance.hostname=localhost 
  • Related