All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.foreveross.springboot.dubbo.autoconfigure.DubboxConfig Maven / Gradle / Ivy

There is a newer version: 2.3.8
Show newest version
package com.foreveross.springboot.dubbo.autoconfigure;

import com.alibaba.dubbo.remoting.http.servlet.BootstrapListener;
import com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by VectorHo on 16/4/14.
 *
 * spring-boot集成dubbox必须的一些设置, 即dubbox服务容器交给spring-boot内嵌tomcat容器.
 *
 * ps: 这个设置相当重要,网上大部分资料都是用的dubbox内嵌的tomcat, 从本质上并没有集成到spring-boot.
 */
@Configuration
public class DubboxConfig {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Value("${dubbox.dispatcherServlet.servlet-mapping:/api/*}")
    private String dubboxServletMapping;
    
    // @Value("${springMvc.dispatcherServlet.servlet-mapping:/}")
    // private String springMvcServletMapping;


    /**
     *  dubbox rest协议必须有BootstrapListener容器加载和结束需要设置
     *
     *
     *  @return BootstrapListener
     */
    @Bean
    @ConditionalOnMissingBean(BootstrapListener.class)
    public BootstrapListener bootstrapListener() {
        logger.debug("add com.alibaba.dubbo.remoting.http.servlet.BootstrapListener...");

        return new BootstrapListener();
    }


    /**
     * 加载context覆盖成com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet
     *
     * dubbox rest协议必须有DispatcherServlet容器加载和结束需要设置
     *
     *
     * @return DispatcherServlet
     */
//    @Bean
//    public DispatcherServlet dispatcherServlet() {
//        logger.debug("add com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet...");
//        return new DispatcherServlet();
//    }



    /**
     * Register dispatcherServlet programmatically, 和上述声明方式选一个即可
     *
     * ps: 包括设置Servlet额外参数
     *
     * @return ServletRegistrationBean
     */
    @Bean
    public ServletRegistrationBean dubboxDispatcherServletRegistration() {
        logger.debug("dubbo dispatcherServlet servlet-mapping pattern is {} .", this.dubboxServletMapping);

        ServletRegistrationBean registration = new ServletRegistrationBean(new DispatcherServlet(), this.dubboxServletMapping);
        registration.setName("dubbox-dispatcherServlet");
        registration.setLoadOnStartup(1);
        return registration;
    }



    /**
     * Register dispatcherServlet programmatically, load spring mvc dispatcherServlet.
     *
     * ps: web应用, spring boot默认的会使用AnnotationConfigApplicationContext, dubbox加载的dispatcherServlet会覆盖spring的dispatcherServlet
     * 这里新建一个XmlWebApplicationContext加载spring提供的dispatcherServlet, 即让spring boot支持多个在不同webApplicationContext的dispatcherServlet
     *
     * @return ServletRegistrationBean
     */
    // @Bean
    // public ServletRegistrationBean springMvcDispatcherServletRegistration() {
    //     logger.debug("spring mvc dispatcherServlet servlet-mapping pattern is {} .", this.springMvcServletMapping);
    //
    //     org.springframework.web.servlet.DispatcherServlet dispatcherServlet = new org.springframework.web.servlet.DispatcherServlet();
    //     XmlWebApplicationContext applicationContext = new XmlWebApplicationContext();
    //     applicationContext.setConfigLocation("classpath*:/WEB-INF/springMvc-dispatcherServlet-servlet.xml");
    //     dispatcherServlet.setApplicationContext(applicationContext);
    //
    //     ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet, this.springMvcServletMapping);
    //     registration.setName("springMvc-dispatcherServlet");
    //     registration.setLoadOnStartup(1);
    //     return registration;
    // }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy