【问题标题】:How to run spring boot admin client and server in same application如何在同一应用程序中运行 Spring Boot 管理客户端和服务器
【发布时间】:2019-01-21 22:25:31
【问题描述】:

我想在同一个应用程序中运行 Spring Boot 管理服务器和客户端。我更改了服务器端口,当我更改服务器端口时,spring admin 将访问我更改的端口。所以我可以运行一个管理服务器。但我看不到我的 Web 应用程序页面。

我需要这样的输出。

Localhost:8080/myapplication(我的客户端应用程序)
localhost:8090/admin (spring boot 管理服务器)

【问题讨论】:

  • 是一个应用还是两个应用
  • Ammar ali,在一个应用程序中,管理员和客户端使用端口分开。
  • 您使用的是哪个 Spring Boot 版本
  • 我正在使用 spring boot 2.0.4.RELEASE 和 spring boot admin 2.0.2

标签: spring spring-boot spring-boot-actuator spring-boot-admin


【解决方案1】:

这是一个简单的示例,用于在管理客户端和服务器客户端的两个不同端口上运行应用程序。

@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) + "\"";
        }
    }
}
}

更多详情请点击以下链接: spring-boot-connectors 希望这会有所帮助。

【讨论】:

  • 尝试更改端口
  • 根据错误,另一个应用程序正在使用相同的端口。更改端口然后它会工作
  • 很好,它终于可以工作了。将此答案标记为正确,以便其他人利用。
【解决方案2】:

我们可以像下面这样使用 spring boot 多模块。

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

【讨论】:

    猜你喜欢
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 2018-03-28
    • 2017-01-05
    • 2011-07-22
    • 1970-01-01
    相关资源
    最近更新 更多