创建三个项目,使用springboot1.5,项目名称springboot-dubbo-provide服务提供者,服务接口springboot-dubbo-api,服务消费者springboot-dubbo-consume。
一、springboot-dubbo-provide的pom.xml
UTF-8 UTF-8 1.8 1.0.0
com.example.api springboot-dubbo-api 0.0.1-SNAPSHOT io.dubbo.springboot spring-boot-starter-dubbo ${dubbo-spring-boot} org.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test
二、springboot-dubbo-provide的application.properties
## Dubbo 服务提供者配置 spring.dubbo.application.name=provider ## Dubbo 服务对象的注册中心zookeeper的地址和端口 spring.dubbo.registry.address=zookeeper://192.168.145.128:2181 ## 用Dubbo协议在20880端口暴露服务 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 ## 包扫描范围 spring.dubbo.scan=com
三、springboot-dubbo-api
public interface ExampleService { public String helloDoubb(); }
四、springboot-dubbo-provide(@Service注解是spring的)
import com.example.api.service.ExampleService; import org.springframework.stereotype.Service; @Service public class ExampleServiceImpl implements ExampleService { @Override public String helloDoubb() { System.out.println("提供者执行......................"); return "helloDoubb "; } }
五、springboot-dubbo-consume的pom.xml
UTF-8 UTF-8 1.8 1.0.0
六、springboot-dubbo-consume的application.propertiescom.example.api springboot-dubbo-api 0.0.1-SNAPSHOT org.springframework.boot spring-boot-starter-web io.dubbo.springboot spring-boot-starter-dubbo ${dubbo-spring-boot} org.springframework.boot spring-boot-starter-test test
## 避免和 server 工程端口冲突 server.port=8082 ## Dubbo 服务消费者配置 spring.dubbo.application.name=consumer ## Dubbo 服务对象的注册中心zookeeper的地址和端口 spring.dubbo.registry.address=zookeeper://192.168.145.128:2181 ## 服务对象的被注入的包扫描范围 spring.dubbo.scan=com
七、springboot-dubbo-consume的Controller
@Autowired ExampleService exampleService; @RequestMapping("/") @ResponseBody public String index(){ exampleService.helloDoubb(); return "hello,springboot"; }启动顺序springboot-dubbo-provide->springboot-dubbo-consume。