SpringBoot+Dubbo+zookeeper整合
廢話不多說 開整
新建dubbo-parent 父工程 加入以下jar包
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <dubbo-spring-boot>1.0.0</dubbo-spring-boot> </properties> <modules> <module>dubbo-consumer</module> <module>dubbo-provider</module> <module>dubbo-api</module> </modules> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- Spring Boot Dubbo 依賴 --> <dependency> <groupId>io.dubbo.springboot</groupId> <artifactId>spring-boot-starter-dubbo</artifactId> <version>${dubbo-spring-boot}</version> </dependency> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
新建三個Module
dubbo-api
dubbo-provider
dubbo-consumer
在dubbo-provider 和dubbo-consumer下引入dubbo-api.jar
<dependency> <groupId>com.chengzhi</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
<dependency> <groupId>com.chengzhi</groupId> <artifactId>dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
在dubbo下建立要釋出的介面
public interface DubboService { public void sayHello(String name); }
在dubbo-provider 建立啟動類
@SpringBootApplication public class ProviderApplication { public static void main(String[] args){ SpringApplication.run(ProviderApplication.class,args); } }實現dubbo-api 的介面
import com.alibaba.dubbo.config.annotation.Service;(引包)
@Service(version = "1.0.0") public class DubboServiceImpl implements DubboService{ @Override public void sayHello(String name) { System.out.println(name); } }
resource下新建application.properties
server.port=7001 #應用名稱 spring.dubbo.application.name=dubbo-provider #註冊中心地址 spring.dubbo.registry.address=zookeeper://192.168.50.130:4180 #暴露服務方式 spring.dubbo.protocol.name=dubbo #暴露服務埠 spring.dubbo.protocol.port=20880
#暴露服務包 spring.dubbo.scan=com.chengzhi.web.servive.impl
在dubbo-consumer中
@SpringBootApplication public class ConsumerApplication { public static void main(String[] args){ SpringApplication.run(ConsumerApplication.class,args); } }
@RestController @RequestMapping("/dubbo") public class DubboController { // @Resource @Reference(version = "1.0.0") DubboService dubboService; @RequestMapping("sayHello") public void sayHello() { dubboService.sayHello("consumer 請求服務"); } }
@RestController @RequestMapping("/dubbo") public class DubboController { // @Resource @Reference(version = "1.0.0") DubboService dubboService; @RequestMapping("sayHello") public void sayHello() { System.out.println("dubbo開始呼叫"); dubboService.sayHello("consumer 請求服務"); } }
@RestController @RequestMapping("/dubbo") public class DubboController { // @Resource @Reference(version = "1.0.0") DubboService dubboService; @RequestMapping("sayHello") public void sayHello() { System.out.println("dubbo開始呼叫"); dubboService.sayHello("consumer 請求服務"); } }在resource下面新建application.properties
server.port=7002 #應用名稱 spring.dubbo.application.name=dubbo-consumer #註冊中心地址 spring.dubbo.registry.address=zookeeper://192.168.50.130:4180
#路徑 spring.dubbo.scan=com.chengzhi.web
zookeeper需要自己搭建
分別啟動dubbo-provider和dubbo-consumer
控制檯會列印連線zookeeper資訊
再看zookeeper
[zk: 127.0.0.1:4180(CONNECTED) 14]
ls /
[dubbo, zookeeper]
[zk: 127.0.0.1:4180(CONNECTED) 15] ls /dubbo
[com.chengzhi.web.service.DubboService]
[zk: 127.0.0.1:4180(CONNECTED) 16] ls /dubbo/com.chengzhi.web.service.DubboService/consumers
[consumer%3A%2F%2F192.168.50.1%2Fcom.chengzhi.web.service.DubboService%3Fapplication%3Ddubbo-consumer%26category%3Dconsumers%26check%3Dfalse%26dubbo%3D2.5.3%26interface%3Dcom.chengzhi.web.service.DubboService%26methods%3DsayHello%26pid%3D15996%26revision%3D1.0-SNAPSHOT%26side%3Dconsumer%26timestamp%3D1515232367366%26version%3D1.0.0]
[zk: 127.0.0.1:4180(CONNECTED) 17] ls /dubbo/com.chengzhi.web.service.DubboService/providers
[dubbo%3A%2F%2F192.168.50.1%3A20880%2Fcom.chengzhi.web.service.DubboService%3Fanyhost%3Dtrue%26application%3Ddubbo-provider%26dubbo%3D2.5.3%26interface%3Dcom.chengzhi.web.service.DubboService%26methods%3DsayHello%26pid%3D14820%26revision%3D1.0-SNAPSHOT%26side%3Dprovider%26timestamp%3D1515233416711%26version%3D1.0.0]
可以看到我們已經把服務發不到zookeeper上面了
效果如下