4

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

我需要这样的输出。

localhost:8080/myapplication (我的客户端应用程序)
localhost:8090/admin (spring boot admin server)

4

2 回答 2

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 希望这会有所帮助。

于 2018-08-15T03:51:39.490 回答
0

我们可以使用 Spring Boot 多模块,如下所示。

main app
    spring-boot-admin
    pom.xml ( running port 8888 )
    my project
    pom.xml ( running port 8080 )
pom.xml
于 2018-08-24T07:18:04.960 回答