How To Register Legacy Grails 2.4 Application In Eureka Server

Posted By : Pooja Agarwal | 31-Jan-2018
Spring boot apps use spring cloud netflix by which we can use annotation to enable and cofigure common patterns such as service discovery (Eureka),circuit breaker (Hystrix),Api gateway or routing (Zuul) and client side load balancing(Ribbon). We can easily use microservices in any grails 3 application because grails 3 based on spring boot.We can use microservice in grails 2.4 by using the netflix eureka libraries directly.
Here we see the example in which eureka server is based on spring boot and eureka client is based on grails 2.4 and we register the grails 2.4 service to eureka.
Creating eureka client in grails 2.4:
First we add the dependency to BuildConfig.groovy.
runtime ''
Now this is the groovy class ExampleBaseService.groovy to use in creating the eureka client.


public class ExampleBaseService { 


    private final ApplicationInfoManager applicationInfoManager; 

    private final EurekaClient eurekaClient; 

    private final DynamicPropertyFactory configInstance; 



    public ExampleBaseService(ApplicationInfoManager applicationInfoManager, 

                              EurekaClient eurekaClient, 

                              DynamicPropertyFactory configInstance) { 

        this.applicationInfoManager = applicationInfoManager; 
        this.eurekaClient = eurekaClient; 

        this.configInstance = configInstance; 




    public void start() { 


        System.out.println("Registering service to eureka with STARTING status"); 


        System.out.println("Simulating service initialization by sleeping for 2 seconds..."); 

        try { 


        } catch (InterruptedException e) { 

            // Nothing 


        System.out.println("Done sleeping, now changing status to UP"); 



        System.out.println("Service started and ready to process requests.."); 




    public void stop() { 

        if (eurekaClient != null) { 





    private void waitForRegistrationWithEureka(EurekaClient eurekaClient) { 


        String vipAddress = configInstance.getStringProperty("eureka.vipAddress", "").get(); 


        InstanceInfo nextServerInfo = null; 

        while (nextServerInfo == null) { 

            try { 

                nextServerInfo = eurekaClient.getNextServerFromEureka(vipAddress, false); 


                System.out.println("Found an instance of example service to talk to from eureka: "

                        + nextServerInfo.getVIPAddress() + ":" + nextServerInfo.getPort()+":"+nextServerInfo.getHostName());


                System.out.println("healthCheckUrl: " + nextServerInfo.getHealthCheckUrl());

                System.out.println("override: " + nextServerInfo.getOverriddenStatus());


            } catch (Throwable e) { 

                System.out.println("Waiting ... verifying service registration with eureka ..."+e); 


                try { 


                } catch (InterruptedException e1) { 








Now this code would be written in bootstrap.groovy in def init.


private static ApplicationInfoManager applicationInfoManager;

    private static EurekaClient eurekaClient;


    private static synchronized ApplicationInfoManager initializeApplicationInfoManager(EurekaInstanceConfig instanceConfig) {

        if (applicationInfoManager == null) {

            InstanceInfo instanceInfo = new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get();

            applicationInfoManager = new ApplicationInfoManager(instanceConfig, instanceInfo);



        return applicationInfoManager;



    private static synchronized EurekaClient initializeEurekaClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig clientConfig) {

        if (eurekaClient == null) {

            eurekaClient = new DiscoveryClient(applicationInfoManager, clientConfig);



        return eurekaClient;



DynamicPropertyFactory configInstance =;

ApplicationInfoManager applicationInfoManager = EurekaConfiguration.initializeApplicationInfoManager(new MyDataCenterInstanceConfig());

EurekaClient client = EurekaConfiguration.initializeEurekaClient(applicationInfoManager, new DefaultEurekaClientConfig());



ExampleBaseService exampleServiceBase = new ExampleBaseService(applicationInfoManager, client, configInstance);

try {


} finally {

// the stop calls shutdown on eurekaClient

Now we add the to classpath.



eureka.region = default

eureka.vipAddress = grails-service

eureka.port = 8080





Now we create eureka server in spring boot:
We create eureka server by using @EnableEurekaServer.


public class Application {


public static void main(String[] args) {, args);



Then we add eureka properties to application.yml.



  port: ${PORT:8761}




    hostname: localhost


    registerWithEureka: false

    fetchRegistry: false


    enableSelfPreservation: false   

First we run the eureka server and then run the eureka client and then we see that grails-service register to eureka server like this.
In this way we can register any grails 2.4 application to eureka server.

About Author

Author Image
Pooja Agarwal

Pooja is an MCA and oracle certified associate. She has good knowledge of core Java , advanced Java and Hibernate.

Request for Proposal

Name is required

Comment is required

Sending message..