2 jin07430119 jin07430119 于 2014.11.17 19:00 提问

dubbo webservice 调用

项目原来用的是cxf 的webservice接口,需要集成dubbo系统。根据配置浏览器可以访问,但是原程序中,spring mvc的注入对象为null,详细问题如下:
web.xml 主要代码

 <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value> 
                classpath*:/applicationContext.xml
            </param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

        <filter>
            <filter-name>encodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                <param-name>forceEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping> 

        <!--dubbo 服务根路径-->   
        <servlet>     
            <servlet-name>dubbo</servlet-name>      
            <servlet-class>
                com.alibaba.dubbo.remoting.http.servlet.DispatcherServlet
            </servlet-class>        
            <load-on-startup>1</load-on-startup>    
        </servlet>
        <servlet-mapping>
            <servlet-name>dubbo</servlet-name>
            <url-pattern>/webservice/*</url-pattern>
        </servlet-mapping>
        <servlet>
            <servlet-name>SearchEngine</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath*:/spring-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>SearchEngine</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>30</session-timeout>
        </session-config>
        <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener </listener-class>
        </listener>

provider.xml

<dubbo:application name="dubbo-webservice-provider" />
        <!-- registry address, used for service to register itself -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181" />
        <!-- 用webservice协议在8181端口暴露服务 -->   
        <dubbo:protocol name="webservice" port="8181" server="servlet" />
        <dubbo:service interface="com.chinasofti.searchengine.webservice.server.searchService.SearchService" ref="demoWSService" />
        <bean id="demoWSService" class="com.chinasofti.searchengine.webservice.server.searchService.impl.SearchServiceImpl" />

consumer.xml

 <dubbo:application name="consumer" />  
    <!-- registry address, used for consumer to discover services -->  
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />  
    <dubbo:consumer timeout="5000"/>  
    <!-- which service to consume? -->  
    <dubbo:reference id="demoWSService" interface="com.chinasofti.searchengine.webservice.server.searchService.SearchService" url="webservice://192.168.1.25:8181/SearchEngine/webservice/com.chinasofti.searchengine.webservice.server.searchService.SearchService"/>  

通过上边的配置,可以在浏览器中访问“http://localhost:8181/SearchEngine/webservice/com.chinasofti.searchengine.webservice.server.searchService.SearchService?wsdl”,可以访问。

在客户端访问服务主要代码:

URL url = null;
        try {
             url = new URL("http://localhost:8181/SearchEngine/webservice/com.chinasofti.searchengine.webservice.server.searchService.SearchService?wsdl");
        } catch (MalformedURLException e) {
        }
        Service service = new SearchService(url);
        SearchServicePortType port = ((SearchService) service).getSearchServicePort();  
        {
        com.chinasofti.searchengine.webservice.client.ws.SearchModel _searchAll_arg0 = null;
        com.chinasofti.searchengine.webservice.client.ws.MultConditionsVO _searchAll_arg1 = null;
        java.lang.String _searchAll__return = port.searchAll(searchModel,vo);
         }

通过调试,可以调用到服务端代码,但是服务端的spring mvc @Resource标签注入的对象为null。

   @Resource(name = "conClassDaoImpl")
        private ConClassDao conClassDao;

调试时,上边的conClassDao为null。

项目中调用其他功能spring 标签是可以注入的,也没有问题。
我是在原有的工程中集成dubbo配置的,哪个地方可能导致调用接口是spring 标签注入失败啊?跪求各位大神,谢谢啦!

4个回答

jin07430119
jin07430119   2014.11.17 19:16

好吧,我自己发现问题了。provider.xml文件需要添加在applicationContext.xml文件中

<import resource="dubbo-demo-provider.xml" />

这样才能与spring集成。

hanzhaoping
hanzhaoping   2015.08.01 10:21

这种方式能够实现负载均衡么?比如我有10个节点,按照这种方式我只能配置其中一个,一旦这个节点发生故障,是不是就无法调通呢?

Dranix_j
Dranix_j 用zookeeper配合dubbo做负载均衡
大约 2 年之前 回复
hanzhaoping
hanzhaoping   2015.08.01 10:21

这种方式能够实现负载均衡么?比如我有10个节点,按照这种方式我只能配置其中一个,一旦这个节点发生故障,是不是就无法调通呢?

zhanqizi
zhanqizi   2017.06.06 09:53

亲请问你用的哪个版本啊。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!