研究了一下在springboot中集成dubbo。不同的人有不同的做法,这里用正宗些的springboot方法。
一、中间接口
1.创建maven项目
2.声明所需要的接口
3.使用maven的package命令打包发布
二、 provider和 consumer的pom文件
<!-- 中间接口,视自己项目的情况而定 --> <dependency> <groupId>com.example.serviceApi</groupId> <artifactId>serviceApi</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
<dependency><groupId>io.dubbo.springboot</groupId><artifactId>spring-boot-starter-dubbo</artifactId><version>1.0.0</version></dependency><!-- 标准的引用方式 -->
<dependency><groupId>com.101tec</groupId><artifactId>zkclient</artifactId><version>0.10</version><!-- zookeeper -->
<exclusions><exclusion><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId></exclusion></exclusions></dependency><!-- 解决jar冲突 -->
三、properties配置1.provider
server.port=8090 spring.dubbo.application.name=my_provider spring.dubbo.registry.address=zookeeper://127.0.0.1:4180 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20881 #服务所在包 spring.dubbo.scan=com.example2.consumer
server.port=8091 spring.dubbo.application.name=my_consumer spring.dubbo.registry.address=zookeeper://127.0.0.1:4180 #服务所在包 spring.dubbo.scan=com.example四、服务的发布与调用1.provider 发布
注意@Service 引包时不要用错
package com.example.provider; import com.alibaba.dubbo.config.annotation.Service; import com.example.serviceApi.TestInterfice; /** * Created by zhaohebing on 2017-05-22. */ @Service public class TestInterficeImpl implements TestInterfice { @Override public String core(String msg) { System.out.println("接到了消息:"+msg); return "接到了消息:"+msg; } }
2.consumer 调用
1>import com.alibaba.dubbo.config.annotation.Reference;2>@Reference private TestInterfice testInterfice;3>System.out.println(testInterfice.core(msg));
=======================================================================================
这就好了,可以用了