简体   繁体   中英

How can I discover my Service(Spring Boot application) in legecy(Spring web-mvc) code

I have my legacy spring application (not spring boot).

Now I have moved some code which I thought can be work separately as a service, I was able to move code cleanly and able to run as a separate service (spring boot application).

Now I want to discover my service and want to call from legacy code, I add a NetFlix-Eureka dependency but that is downloading the spring-boot dependency, that I don't want.

How can I discover my service in the legacy application and call its APIS

I assume from your question that -

  • You have Eureka server running
  • Your new spring boot microservice is connecting to eureka server
  • You are asking how to connect your legacy application with Eureka server, As after that you can discover and call the APIs of new microservice

If that's correct. For connecting your legacy application you can do below configurations.

Create a class say CustomEurekaClient.

public class CustomEurekaClient {

private static ApplicationInfoManager appaInfoManager;
private static EurekaClient eurekaClient;

@Autowired
private WebAppInstanceConfig webAppInstanceConfig;
@Autowired
private EurekaClientConfig eurekaClientConfig;

private ApplicationInfoManager initializeApplicationInfoManager(EurekaInstanceConfig instanceConfig) {
    InstanceInfo instanceInfo = new EurekaConfigBasedInstanceInfoProvider(instanceConfig).get();
    return new ApplicationInfoManager(instanceConfig, instanceInfo);
}

private EurekaClient initializeEurekaClient(ApplicationInfoManager applicationInfoManager,                                                EurekaClientConfig clientConfig) {
    eurekaClient = new DiscoveryClient(applicationInfoManager, eurekaClient); //use this eureka client while de
    // registering service
    return eurekaClient;
}

@PostConstruct
public void runRegistration() {
    ApplicationInfoManager applicationInfoManager =
            initializeApplicationInfoManager(webAppInstanceConfig);
    initializeEurekaClient(applicationInfoManager, eurekaClientConfig);
    applicationInfoManager.setInstanceStatus(InstanceInfo.InstanceStatus.UP);
}}

EurekaClientConfig class-

class EurekaClientConfig extends DefaultEurekaClientConfig {
//minimum change needed override
@Override
public List<String> getEurekaServerServiceUrls(String myZone) {
    return Arrays.asList(YOUR_COMMA_SEPRATED_EUREKA_SERVER_URL);
}}

WebAppInstanceConfig class-

class WebAppInstanceConfig extends MyDataCenterInstanceConfig {
// TODO override all the methods of EurekaInstanceConfig, which you need like serviceUrl...interface and provide respective values

}

In postConstruct of CustomEurekaClient we are registering the service to Eureka server. Once both services are registered to Eureka you can access APIs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM