Blog

  • 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 'com.netflix.eureka:eureka-client:1.8.6'
    
    Now this is the groovy class ExampleBaseService.groovy to use in creating the eureka client.
    
    @Singleton 
    
    public class ExampleBaseService { 
    
     
    
        private final ApplicationInfoManager applicationInfoManager; 
    
        private final EurekaClient eurekaClient; 
    
        private final DynamicPropertyFactory configInstance; 
    
     
    
        @Inject 
    
        public ExampleBaseService(ApplicationInfoManager applicationInfoManager, 
    
                                  EurekaClient eurekaClient, 
    
                                  DynamicPropertyFactory configInstance) { 
    
            this.applicationInfoManager = applicationInfoManager; 
            this.eurekaClient = eurekaClient; 
    
            this.configInstance = configInstance; 
    
        } 
    
     
    
        @PostConstruct 
    
        public void start() { 
    
          
    
            System.out.println("Registering service to eureka with STARTING status"); 
    
            applicationInfoManager.setInstanceStatus(InstanceStatus.STARTING); 
    
     
            System.out.println("Simulating service initialization by sleeping for 2 seconds..."); 
    
            try { 
    
                Thread.sleep(2000); 
    
            } catch (InterruptedException e) { 
    
                // Nothing 
            } 
    
     
    
            System.out.println("Done sleeping, now changing status to UP"); 
    
            applicationInfoManager.setInstanceStatus(InstanceStatus.UP); 
    
            waitForRegistrationWithEureka(eurekaClient); 
    
            System.out.println("Service started and ready to process requests.."); 
    
    
    
        } 
    
     
    
        @PreDestroy 
    
        public void stop() { 
    
            if (eurekaClient != null) { 
    
                eurekaClient.shutdown(); 
    
            } 
    
        } 
    
     
    
        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 { 
    
                        Thread.sleep(10000); 
    
                    } catch (InterruptedException e1) { 
    
                        e1.printStackTrace(); 
    
                    } 
    
                } 
    
            } 
    
        } 
    
     
    
    }
    
     
    
    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 = com.netflix.config.DynamicPropertyFactory.getInstance();
    
    ApplicationInfoManager applicationInfoManager = EurekaConfiguration.initializeApplicationInfoManager(new MyDataCenterInstanceConfig());
    
    EurekaClient client = EurekaConfiguration.initializeEurekaClient(applicationInfoManager, new DefaultEurekaClientConfig());
    
            System.out.println("client"+client)
    
    
     
    
    ExampleBaseService exampleServiceBase = new ExampleBaseService(applicationInfoManager, client, configInstance);
    
    try {
    
    exampleServiceBase.start();
    
    } finally {
    
    // the stop calls shutdown on eurekaClient
    }
    
     
    
    Now we add the eureka-client.properties to classpath.

     

    
    
    
     
    
    eureka.region = default
    
    eureka.vipAddress = grails-service
    
    eureka.port = 8080
    
    
    eureka.name=Grails-Service
    
    
    eureka.hostname=192.168.6.188
    
    
    
    eureka.preferSameZone=true
    
    
    
    eureka.shouldUseDns=false
    
    
    
    eureka.serviceUrl.default=http://localhost:8761/eureka/
    
     
    
    Now we create eureka server in spring boot:
     
    We create eureka server by using @EnableEurekaServer.
    
    
     
    
    @SpringBootApplication
    @EnableEurekaServer
    @EnableDiscoveryClient
    public class Application {
    
     
    
    public static void main(String[] args) {
    
    SpringApplication.run(Application.class, args);
    
    }
    
    }
    
     
    
    Then we add eureka properties to application.yml.
    
    
    
     
    
    server:  
    
      port: ${PORT:8761}
    
        
    
    eureka:  
    
      instance:
    
        hostname: localhost
    
      client:
    
        registerWithEureka: false
    
        fetchRegistry: false
        
    
      server:
    
        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.

Tags: grails