Spring-Boot:如何添加tomcat连接器以绑定到控制器 [英] Spring-Boot : How can I add tomcat connectors to bind to controller

查看:174
本文介绍了Spring-Boot:如何添加tomcat连接器以绑定到控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring-Boot文档中,有一节描述了如何为tomcat启用多个连接器( http://docs.spring.io/spring-boot/docs/1.1.7.RELEASE/reference/htmlsingle/ #howto-enable-multiple-connectors-in-tomcat )。

In the Spring-Boot documentation, there is a section that describe how to enable multiple connectors for tomcat (http://docs.spring.io/spring-boot/docs/1.1.7.RELEASE/reference/htmlsingle/#howto-enable-multiple-connectors-in-tomcat).

但有没有办法简单地将连接器添加到现有的连接器(网络和管理连接器)?并将它们绑定到一些mvc控制器?
我想要做的是创建一些可以在不同端口上访问的Web服务。

But is there a way to simply add connectors to the existing one (the web and the management connectors)? And to bind them to some mvc controllers? What I want to do is to create some web services that are accessible on a different ports.

推荐答案

你可以为每个服务使用子应用程序,每个子节点通过设置其 server.port 属性配置为使用单独的端口。您想要隔离的任何组件都应该包含在其中一个子组件中。您要共享的任何组件都应该放在父级中。

You could use a child application for each service, with each child configured to use a separate port by setting its server.port property. Any components that you want to be isolated should go in one of the children. Any components that you want to share should go in the parent.

以下是此方法的示例。有两个子应用程序,一个侦听端口8080,另一个侦听端口8081.每个都包含一个控制器,映射到 / one /二分别。

Here's an example of this approach. There are two child applications, one listening on port 8080 and one listening on port 8081. Each contains a controller which is mapped to /one and /two respectively.

package com.example;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

public class Application {

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

    @Configuration
    static class ApplicationConfiguration {

        @Bean
        public MySharedService sharedService() {
            return new MySharedService();

        }   
    }

    @Configuration
    @EnableAutoConfiguration
    static class ServiceOneConfiguration {

        @Bean
        public ControllerOne controller(MySharedService service) {
            return new ControllerOne(service);
        }
    }

    @Configuration
    @EnableAutoConfiguration
    static class ServiceTwoConfiguration {

        @Bean
        public ControllerTwo controller(MySharedService service) {
            return new ControllerTwo(service);
        }
    }

    @RequestMapping("/one")
    static class ControllerOne {

        private final MySharedService service;

        public ControllerOne(MySharedService service) {
            this.service = service;
        }

        @RequestMapping
        @ResponseBody
        public String getMessage() {
            return "ControllerOne says \"" + this.service.getMessage() + "\"";
        }
    }

    @RequestMapping("/two")
    static class ControllerTwo {

        private final MySharedService service;

        public ControllerTwo(MySharedService service) {
            this.service = service;
        }

        @RequestMapping
        @ResponseBody
        public String getMessage() {
            return "ControllerTwo says \"" + this.service.getMessage() + "\"";
        }
    }

   static class MySharedService {

        public String getMessage() {
            return "Hello";
        }
    }
}

这篇关于Spring-Boot:如何添加tomcat连接器以绑定到控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆