Technically this should be possible to integrate both in one single application, but I'm afraid you'll run into problems soon.
But let us give it a try...
Reference: Proof of concept GitHub
Problems with this approach:
- launch 2 servers on startup: the
client
on port 8080
, and the admin-server on port 8081
- the client and the server should have separate configurations
Let's start with the configuration
We could separate the multiple configurations by using a profile
so we can take advantage of Profile Specific Files
application-client.properties
server.address=localhost
spring.boot.admin.client.url = http://localhost:8081/
management.endpoints.web.exposure.include = *
management.endpoint.health.show-details= always
spring.application.name=client-app
spring.boot.admin.client.instance.name=client-app
application-admin.properties
server.address=localhost
server.port=8081
Launch the server, and the client with the correct profile.
DemoApplication
public class DemoApplication {
public static void main(String[] args) throws Exception {
SpringApplication admin = new SpringApplication(DemoAdminServer.class);
admin.setAdditionalProfiles("admin");
admin.run(args);
SpringApplication client = new SpringApplication(DemoClient.class);
client.setAdditionalProfiles("client");
client.run(args);
}
}
@EnableAdminServer
@Configuration
@EnableAutoConfiguration
@Profile("admin")
class DemoAdminServer {
}
@SpringBootApplication
@RestController
@Profile("client")
class DemoClient {
@GetMapping
public String hello(){
return "Hello world!";
}
}
Launch the Application, and we should be good...
If you have two separate applications, then in your client
you could launch the admin-server
via a process.
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) throws Exception{
startAdminServer();
SpringApplication.run(DemoApplication.class, args);
}
static void startAdminServer() throws Exception {
new ProcessBuilder()
.redirectErrorStream(true)
.command("cmd.exe",
"/c",
"java -jar path_to_your_admin_server.jar")
.start();
}
}