I have a legacy service that is running on-prem and have built a new service that runs on cloud which provides the same functionality. I have another java (spring) service which consumes the on-prem service. There are number of apis exposed by this on-prem service which is being called in multiple places within the consuming app. In order to prepare for migration to new service which provides the exact set of apis , I have defined a external toggle (using spring cloud config server). In order to flip the traffic from legacy to cloud endpoint , all I need to do is update the flag in the property file.
I currently have a service locator which has a resttemplate that makes a call to the legacy endpoint. While calling the cloud endpoint , I need to update different set of header values. Is there any java design pattern that I can leverage to make a call to cloud endpoint?
-
Do you consider using a command pattern? metamug.com/article/command-design-pattern-java-example.htmlSorter– Sorter2018年10月16日 06:23:28 +00:00Commented Oct 16, 2018 at 6:23
1 Answer 1
You can achieve this approach by mixing Strategy Pattern and Command Pattern.
You can define class(es) for service method(s), a class for service proxy, a class for configuration and a class to make a service call.
Define an interface for your service methods:
public interface IServiceMethod {
void setClient(Service service);
Object execute();
}
Create classes for your service methods:
public class SomeMethod implements IServiceMethod {
private Service service;
public SomeMethod() {
}
@Override
public void setClient(Service service) {
this.client = client;
}
@Override
public T execute() {
return client.getService().someMethod();
}
}
Define a configuration class:
public class ServiceConfiguration {
private String endPointUrl;
private EnvironmentType envType; //DEV,TEST,PROD
private String userName;
private String password;
private boolean isActive;
}
Define a class to wrap service and its configuration:
public interface IServiceProxy {
Service getService();
ServiceConfiguration getConfiguration();
}
Construct your service proxy with configuration and get endpoint (and other needed configuration data) from some provider:
public class ServiceProxy implements IServiceProxy {
private Service service;
private ServiceConfiguration configuration;
public ServiceProxy(ServiceConfiguration serviceConfiguration) {
this.service = someFactory.getInstance().getClient(serviceConfiguration);
this.serviceConfiguration = serviceConfiguration;
}
@Override
public Service getService() {
return service;
}
@Override
public ServiceConfiguration getConfiguration() {
return configuration;
}
}
Construct MethodExecutor with method:
public class MethodExecutor{
private IServiceMethod iServiceMethod;
public MethodExecutor(IServiceMethod iServiceMethod) {
this.iServiceMethod = iServiceMethod;
}
public Object execute() {
return iServiceMethod.execute();
}
}