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

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.

Related

Expose particular REST controller on specific port using Spring Webflux

We have a spring boot based application and using spring webflux, we are exposing 3 ports
server.port
management.server.port (actuator) and are planning to expose another port called as admin port.
We want to expose a specific REST controller on this admin port which will be private. This api will provide all the admin level configuration and will not be available publicly to the user.
Note: We don't want to expose api on actuator port. Need to open a new port.
Using the following code to open a new port by starting a new server instance.
#Configuration
public class NettyServerForAdminPort {
#Value("${admin.port}")
private Integer adminPort;
#Autowired
HttpHandler httpHandler;
WebServer http;
#PostConstruct
public void start() {
ReactiveWebServerFactory factory = new NettyReactiveWebServerFactory(8081);
this.http = factory.getWebServer(this.httpHandler);
this.http.start();
}
#PreDestroy
public void stop() {
this.http.stop();
}
}
Any insights on how this can be achieved?

Standalone wiremock server as spring boot application

I am trying to make a mock service as a spring boot application.
Can I use standalone mock server inside a spring boot application?
When I tried to run a mock server on any port inside the spring boot application it throws the "Address already bound exception"
Is there a way to over come that so that I can have a mockservice running as a spring boot docker container and just configure the urls I want to mock.
Basically, you would want to avoid any starter that brings up a container/server. Initially, I have only these two dependencies: com.github.tomakehurst:wiremock-jre8:2.31.0 and org.springframework.boot:spring-boot-starter-json. Optionally, confirm you don't want to run any server(s): spring.main.web-application-type: none.
Finally, declare a config file to setup WireMock:
#Configuration
#AllArgsConstructor
#EnableConfigurationProperties(WireMockConfig.ApplicationProps.class)
public class WireMockConfig {
private final ApplicationProps props;
#Bean
public WireMockServer mockServer() {
return new WireMockServer(WireMockConfiguration.options().port(8081));
}
}
...and a listener to start the server:
#Component
#AllArgsConstructor
public class ApplicationListener {
private final WireMockServer server;
#EventListener
public void onApplicationEvent(final ApplicationReadyEvent event) {
server.start();
}
}

Invoke a service method after the Spring Boot deployed successfully

I have a spring boot application and i want to invoke a service method once the application gets deployed successfully.
Ex:
#Service
public class MyServiceImpl implements MyUtilityService<Object, Object>{
#Override
public Object runOnce(Object credential) {
return null;
}
}
I want to invoke myService.runOnce(...) only after my application deployed successfully
You can register an EventListener for the ApplicationReadyEvent.
From the docs:
Event published as late as conceivably possible to indicate that the
application is ready to service requests. The source of the event is
the SpringApplication itself, but beware of modifying its internal
state since all initialization steps will have been completed by then.
#Service
public class MyServiceImpl implements MyUtilityService<Object, Object>{
#Override
#EventListener(ApplicationReadyEvent.class)
public Object runOnce(Object credential) {
return null;
}
}

Define different Feign client implementations based on environment

I have a Spring boot application which uses Feign to call an external web service via Eureka. I'd like to be able to run the application using a mocked out implementation of the Feign interface, so I can run the application locally without necessarily having Eureka or the external web service running. I had imagined defining a run configuration that allowed me to do this, but am struggling to get this working. The issue is that the Spring "magic" is defining a bean for the Feign interface no matter what I try.
Feign interface
#FeignClient(name = "http://foo-service")
public interface FooResource {
#RequestMapping(value = "/doSomething", method = GET)
String getResponse();
}
Service
public class MyService {
private FooResource fooResource;
...
public void getFoo() {
String response = this.fooResource.getResponse();
...
}
}
I tried adding a configuration class that conditionally registered a bean if the Spring profile was "local", but that was never called when I ran the application with that Spring profile:
#Configuration
public class AppConfig {
#Bean
#ConditionalOnProperty(prefix = "spring.profile", name = "active", havingValue="local")
public FooResource fooResource() {
return new FooResource() {
#Override
public String getResponse() {
return "testing";
}
};
}
}
At the point my service runs, the FooResource member variable in MyService is of type
HardCodedTarget(type=FoorResource, url=http://foo-service)
according to IntelliJ. This is the type that is automatically generated by the Spring Cloud Netflix framework, and so tries to actually communicate with the remote service.
Is there a way I can conditionally override the implementation of the Feign interface depending on a configuration setting?
the solution is like below:
public interface FeignBase {
#RequestMapping(value = "/get", method = RequestMethod.POST, headers = "Accept=application/json")
Result get(#RequestBody Token common);
}
then define your env based interface:
#Profile("prod")
#FeignClient(name = "service.name")
public interface Feign1 extends FeignBase
{}
#Profile("!prod")
#FeignClient(name = "service.name", url = "your url")
public interface Feign2 extends FeignBase
{}
finally, in your service impl:
#Resource
private FeignBase feignBase;
Having posted the same question on the Spring Cloud Netflix github repository, a useful answer was to use the Spring #Profile annotation.
I created an alternative entry point class that was not annotated with #EnabledFeignClients, and created a new configuration class that defined implementations for my Feign interfaces. This now allows me to run my application locally without the need to have Eureka running, or any dependent services.
I'm using a simpler solution to avoid having multiples interfaces for a variable parameter like url.
#FeignClient(name = "service.name", url = "${app.feign.clients.url}")
public interface YourClient{}
application-{profile}.properties
app.feign.clients.url=http://localhost:9999

Problems injecting a BayeuxService into another class with annotations

I have a web app that is using Bayeux to handle Comet connections. I initialize a BayeuxServer and tie it into Spring annotations and it all works fine, listening on selected channels and responding.
I have a Jersey annotated class and an annotated Bayeux service as shown below. The idea is I wanted to be able to control resources via Rest from an individual web app, and then right after the resource is changed, do a server push via Comet to all other applicable clients to tell them to update their information.
Here is the problem: A Bayeux Service is created when the webapp is deployed, setting up proper channels to listen on and monitoring clients. There should only be one instance of this. When Jersey attempts to use the Bayeux service it creates a whole new service, when it should be using the original one. This new service doesn't have the BayeuxServer properly injected so I can't access client information through it.
It makes since that this should be doable, but I don't seem to understand how to inject these things properly via annotations. Can anyone point me in the right direction?
Jersey Annotated Class:
#Path("JsonTest")
public class JsonTest {
#Context
Request request;
#Context
UriInfo uriInfo;
#Context
ResourceContext resourceContext;
protected final Logger log = Logger.getLogger(getClass());
public JsonTest() {
}
#DELETE
#Path("{id}")
public void deleteJson(#PathParam("id") String id) {
JsonTestDao.instance.getModel().remove(id);
log.info("Deleted Json..." + id);
log.info("New json: " + JsonTestDao.instance.getModel().toString());
JsonTestService jsonTestService = resourceContext.getResource(JsonTestService.class);
jsonTestService.sendUpdate();
}
}
BayeuxService:
#Named
// Singleton here didn't seem to make a difference
#Service
public class JsonTestService {
protected final Logger log = Logger.getLogger(getClass());
#Inject
private BayeuxServer bayeux;
#Session
private ServerSession serverSession;
#PostConstruct
public void init() {
log.info("Initializing JsonTest Bayeux HelloService...");
log.info("Current sessions are: " + bayeux.getSessions().toString());
}
#Listener("/cometd/JsonTest")
public void jsonTestHandler(ServerSession remote, ServerMessage.Mutable message) {
}
public void sendUpdate() {
//bayeux.newMessage(); // Need a method that the Jersey class can call to notify changes
log.info("Bayeux server should be sending an update now...");
}
#PreDestroy
public void destroy() {
log.info("Destroying JsonTest Bayeux HelloService...");
}
}
See Jersey and spring integration - bean Injections are null at runtime.
Another question I asked. Both of these stem from the same problem involving properly setting the Jersey dependency and integrating it with spring.

Resources