Search code examples
springclientnetflix-eureka

How to register spring 5 application to eureka client


Thank you for your reply. i don't know what registers spring(5) application as eureka client. my application is just read data. i found that example code before, but i can't apply some code. and i didn't know what how to execute eureka client code when start my application.

found code

DiscoveryManager.getInstance().initComponent(new MyDataCenterInstanceConfig(), new DefaultEurekaClientConfig());

  String vipAddress = "MY-SERVICE";

    InstanceInfo nextServerInfo = null;
    try {
        nextServerInfo = DiscoveryManager.getInstance()
                .getEurekaClient()
                .getNextServerFromEureka(vipAddress, false);
    } catch (Exception e) {
        System.err.println("Cannot get an instance of example service to talk to from eureka");
        System.exit(-1);
    }

    System.out.println("Found an instance of example service to talk to from eureka: "
            + nextServerInfo.getVIPAddress() + ":" + nextServerInfo.getPort());

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

    System.out.println("Server Host Name "+ nextServerInfo.getHostName() + " at port " + nextServerInfo.getPort() );

i thought my application executes url "/" in MainController when start. so, i just that code writes in "/" GetMapping method of MainController. but i got a error this. cannot access javax.inject.Provider class file for javax.inject.Provider not found.

how to make code for registers as eureka client and how to use that code... please help junior! thanks!


Solution

  • Why don't you try use configuration files? example: application.properties or yml?

    Eureka Server:

    eureka:
      instance:
        hostname: localhost
        port: 9091
      client:
        registerWithEureka: false
        fetchRegistry: false
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      server:
        waitTimeInMsWhenSyncEmpty: 3000
        enableSelfPreservation: false
    

    Microservices Client:

    eureka:
      instance:
        hostname: localhost
        port: 9091
      client:
        registerWithEureka: true
        fetchRegistry: false
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:${eureka.instance.port}/eureka/
      server:
        wait-time-in-ms-when-sync-empty: 3000