springboot + mybatis + dubbo

今天整合springboot + dubbo + mybatis时遇到了一点问题,功夫不负有心人,上天还是帮助付出的人的。哈哈~,废话少说直接上代码。(有什么不足之处请各位指出更正)

先把报错信息贴上来(dubbo客户端总是报错):

org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from file [C:\Users\Administrator\IdeaProjects\parent\dubbo_manager\dubbo_manager_web\target\classes\dubbo\dubbo-client.xml]; nested exception is java.lang.IllegalStateException: Duplicate spring bean id userManagerServiceImpl
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:414) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromImportedResources(ConfigurationClassBeanDefinitionReader.java:353) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:142) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:320) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:272) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:92) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:687) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:525) ~[spring-context-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.9.RELEASE.jar:1.5.9.RELEASE]
	at org.springboot.manager.TestStartMain.main(TestStartMain.java:10) [classes/:na]
Caused by: java.lang.IllegalStateException: Duplicate spring bean id userManagerServiceImpl
	at com.alibaba.dubbo.config.spring.schema.DubboBeanDefinitionParser.parse(DubboBeanDefinitionParser.java:101) ~[dubbo-2.5.4.jar:2.5.4]
	at com.alibaba.dubbo.config.spring.schema.DubboBeanDefinitionParser.parse(DubboBeanDefinitionParser.java:439) ~[dubbo-2.5.4.jar:2.5.4]
	at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1410) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1400) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:172) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:142) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:94) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:508) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:392) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]
	... 21 common frames omitted
报错的意思是在我的项目中有两个service 的实现类。接下来就是繁琐的逐步找错过程了。

先贴出我的项目结构,这样更清晰自己的项目结构:



接下来就是我们dubbo的配置文件了:

1.dubbo服务端的配置文件:

properties文件:

#应用名称
application.name=dubbo-manager-service
#注册中心类型
registry.protocol=muticast
#注册中心地址
registry.address=127.0.0.1:2181
#暴露服务方式
protocol.name=dubbo
#暴露服务端口
protocol.port=20880
xml文件:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-3.1.xsd
						http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://code.alibabatech.com/schema/dubbo
                        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <context:property-placeholder location="classpath:/dubbo/dubbo.properties"></context:property-placeholder>
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="${application.name}" />

    <!-- 注册中心暴露服务地址 -->
     <dubbo:registry address="multicast://224.5.6.7:1234" />

    <!-- <dubbo:registry protocol="zookeeper" address="10.170.219.98:2181,10.173.55.173:2181" /> -->
    <!--<dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" />-->

    <!-- 暴露服务 -->
    <dubbo:protocol name="${protocol.name}" port="${protocol.port}" />
    <!--我们需要暴露的接口-->
    <dubbo:service interface="org.springboot.manager.service.UserManagerService" ref="userManagerServiceImpl" retries="0" timeout="6000" />
</beans>
2.dubbo客户端配置文件:

properties文件:

#应用名称
application.name=dubbo-manager-web
#注册中心类型
#dubbo.registry.protocol=zookeeper
#注册中心地址
registry.address=127.0.0.1:2181
xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	                    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
						http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context-3.1.xsd
						http://www.springframework.org/schema/mvc
						http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://code.alibabatech.com/schema/dubbo
                        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="${application.name}" />

    <!-- 注册中心暴露服务地址 -->
     <dubbo:registry address="multicast://224.5.6.7:1234" />

    <!-- <dubbo:registry protocol="zookeeper" address="10.170.219.98:2181,10.173.55.173:2181" /> -->
    <!--<dubbo:registry protocol="${dubbo.registry.protocol}" address="${dubbo.registry.address}" /> -->
    
    <!-- reference -->
<dubbo:reference interface="org.springboot.manager.service.UserManagerService" id="userManagerServiceImpl"/></beans>
 dubbo客户端的配置加载config(服务端是一样的所以就不贴了): 
 

package org.springboot.manager.dubbo;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("classpath:dubbo/dubbo-client.properties")
@ImportResource({"classpath:dubbo/*.xml"})
public class DubboConfig {
}
接下来启动service,启动成功;启动controller就报上面的错误了,原因是我们controller的依赖问题。

先看我们的错误依赖(我们依赖的是Service的实现类):

<dependency>
            <groupId>org.springboot</groupId>
            <artifactId>dubbo_manager_service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
这时候启动的时候我们的controller,扫描dubbo的配置文件的时候,也就是
<dubbo:reference interface="org.springboot.manager.service.UserManagerService" id="userManagerServiceImpl"/>
这行的时候,dubbo会给我们动态生成一个Service的实现类,但是我们又依赖的Service的实现类,这时候spring容器中就存在了两个service接口的实现类,我们只需要将依赖改为:
<dependency>
            <groupId>org.springboot</groupId>
            <artifactId>dubbo_manager_interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
最后启动controller启动成功!

....

2017-12-28 16:58:22.985  INFO 8640 --- [           main] c.a.d.r.multicast.MulticastRegistry      :  [DUBBO] Notify urls for subscribe url consumer://192.168.0.132/org.springboot.manager.service.UserManagerService?application=dubbo-manager-web&category=providers,configurators,routers&dubbo=2.5.4&interface=org.springboot.manager.service.UserManagerService&methods=getUser&pid=8640&side=consumer×tamp=1514451502564, urls: [dubbo://192.168.0.132:20880/org.springboot.manager.service.UserManagerService?anyhost=true&application=dubbo-manager-service&dubbo=2.5.4&generic=false&interface=org.springboot.manager.service.UserManagerService&methods=getUser&pid=7432&retries=0&side=provider&timeout=6000×tamp=1514448486895], dubbo version: 2.5.4, current host: 192.168.0.132
2017-12-28 16:58:22.988  INFO 8640 --- [           main] com.alibaba.dubbo.config.AbstractConfig  :  [DUBBO] Refer dubbo service org.springboot.manager.service.UserManagerService from url multicast://224.5.6.7:1234/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=dubbo-manager-web&check=false&dubbo=2.5.4&generic=false&interface=org.springboot.manager.service.UserManagerService&methods=getUser&pid=8640&retries=0&side=consumer&timeout=6000×tamp=1514451502564, dubbo version: 2.5.4, current host: 192.168.0.132
2017-12-28 16:58:23.242  INFO 8640 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@3901d134: startup date [Thu Dec 28 16:58:20 CST 2017]; root of context hierarchy
2017-12-28 16:58:23.304  INFO 8640 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/login]}" onto public org.springboot.manager.pojo.User org.springboot.manager.controller.UserController.getUser()
2017-12-28 16:58:23.306  INFO 8640 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-12-28 16:58:23.307  INFO 8640 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-12-28 16:58:23.329  INFO 8640 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-28 16:58:23.329  INFO 8640 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-28 16:58:23.361  INFO 8640 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-28 16:58:23.386  WARN 8640 --- [           main] .t.AbstractTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2017-12-28 16:58:23.753  INFO 8640 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-12-28 16:58:23.797  INFO 8640 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2017-12-28 16:58:23.800  INFO 8640 --- [           main] org.springboot.manager.TestStartMain     : Started TestStartMain in 4.389 seconds (JVM running for 4.748)








猜你喜欢

转载自blog.csdn.net/zhao_xinhu/article/details/78923487