nikhil kandari
nikhil kandari

Reputation: 1

How to register my Spring 5 (non springboot application ) as a service to the eureka server?

I want to register my spring 5 application with jetty server to the eureka server along with the existing springboot microservices . I tried this implenetation . Where my traditional spring 5 application registered in eureka server but when tried to call this spring service so it give error no server found and i tried to print the instance so it give empty array. which show service but no instance. How to configure my spring application with eureka server so that we can access that service in other springboot services ?

@Bean
public EurekaClient eurekaClient() {
    // Set application properties
    System.setProperty("spring.application.name", "OCPP-SERVICE");
    System.setProperty("eureka.client.serviceUrl.defaultZone", "http://localhost:8099/eureka/");

    System.out.println(CONFIG.getJetty().getServerHost());


    // Configure Eureka Instance
    EurekaInstanceConfig instanceConfig = new MyDataCenterInstanceConfig() {
        @Override
        public String getAppname() {
            return "OCPP-SERVICE"; // Service name for Eureka registration
        }

        @Override
        public String getHostName(boolean refresh) {
            return "192.168.145.200"; // Hostname of the service
        }

        @Override
        public boolean isInstanceEnabledOnit() {
            return super.isInstanceEnabledOnit();
        }

        @Override
        public int getNonSecurePort() {
            return CONFIG.getJetty().getHttpPort(); // Port of the service
        }

        @Override
        public String getInstanceId() {
            return getHostName(false)+":"+getAppname() + ":" + getNonSecurePort();
        }
    };

    // Configure Eureka Client
    EurekaClientConfig clientConfig = new DefaultEurekaClientConfig() {
        @Override
        public List<String> getEurekaServerServiceUrls(String myZone) {
            return List.of("http://localhost:8099/eureka/");
        }
    };

    // Initialize ApplicationInfoManager and EurekaClient
    ApplicationInfoManager applicationInfoManager = new ApplicationInfoManager(instanceConfig);
    EurekaClient eurekaClient = new DiscoveryClient(applicationInfoManager, clientConfig);

    // Set instance status to UP
    applicationInfoManager.setInstanceStatus(InstanceInfo.InstanceStatus.UP);

    return eurekaClient;
}

with dependancy

    <dependency>
        <groupId>com.netflix.archaius</groupId>
        <artifactId>archaius-core</artifactId>
        <version>0.7.3</version>
    </dependency>
    <dependency>
        <groupId>com.netflix.servo</groupId>
        <artifactId>servo-core</artifactId>
        <version>0.10.0</version>
    </dependency>
    <dependency>
        <groupId>com.netflix.eureka</groupId>
        <artifactId>eureka-client</artifactId>
        <version>1.10.13</version>
    </dependency>

Upvotes: 0

Views: 27

Answers (0)

Related Questions