How to run spring boot admin client and server in same application

独自空忆成欢 提交于 2021-02-07 08:01:53

问题


I want to run a spring boot admin server and client inside the same application.I changed server port, when I change the server port spring admin will access my changed port. so I can run an admin server. but I can't see my web application pages.

i need output like this.

Localhost:8080/myapplication (my client application)
localhost:8090/admin (spring boot admin server)


回答1:


Here is a simple example to run the application on two different ports for admin client and for server client.

@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder(Application.class);
    parentBuilder.child(ServiceOneConfiguration.class).properties("server.port:8081").run(args);
    parentBuilder.child(ServiceTwoConfiguration.class).properties("server.port:8082").run(args);
}

@Service
static class SharedService {
    public String getMessage(String name) {
        return String.format("Hello, %s, I'm shared service", name);
    }
}

@Configuration
@EnableAutoConfiguration
static class ServiceOneConfiguration {
    @Controller
    @RequestMapping("/server")
    static class ControllerOne {
        @Autowired
        private SharedService service;

        @RequestMapping(produces = "text/plain;charset=utf-8")
        @ResponseBody
        public String getMessage(String name) {
            return "ControllerOne says \"" + service.getMessage(name) + "\"";
        }
    }
}

@Configuration
@EnableAutoConfiguration
static class ServiceTwoConfiguration {
    @Bean
    EmbeddedServletContainerFactory servletContainer() {
        TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory();
        tomcat.setUriEncoding("cp1251");
        return tomcat;
    }

    @Controller
    @RequestMapping("/client")
    static class ControllerTwo {
        @Autowired
        private SharedService service;

        @RequestMapping(produces = "text/plain;charset=utf-8")
        @ResponseBody
        public String getMessage(String name) {
            return "ControllerTwo says \"" + service.getMessage(name) + "\"";
        }
    }
}
}

For more detail here is a link: spring-boot-connectors Hope this will help.




回答2:


we can use spring boot multi modules for this. i used spring boot multi module with 2 modules.

main app
    spring-boot-admin
    pom.xml ( running port 8888 )
    my project
    pom.xml ( running port 8080 )
pom.xml


来源:https://stackoverflow.com/questions/51851985/how-to-run-spring-boot-admin-client-and-server-in-same-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!