项目github:https://github.com/nalidou/spring-dubbo
提供了接口定义、实体类等,其他项目可以直接导入jar包,也可以用maven导入。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wzy</groupId> <artifactId>dubbo-component</artifactId> <version>1.0</version> </project>
实现了公共组件接口
<dependencies> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <!-- https://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>com.wzy</groupId> <artifactId>dubbo-component</artifactId> <version>1.0</version> </dependency> </dependencies>
package com.wzy.dubbo.producer.server; import com.alibaba.dubbo.config.annotation.Service; import com.wzy.component.entity.User; import com.wzy.component.server.IUserServer; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; @Service(interfaceClass = IUserServer.class) //这里用的是dubbo的接口 @Component public class UserServerImpl implements IUserServer { @Override public User getById(long id) { return new User(id, "小明"); } @Override public List<User> getAll() { List<User> list = new ArrayList<User>(); list.add(new User(100, "jerry")); list.add(new User(200, "tom")); return list; } }
3. web服务(服务消费者):dubbo-web
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> <!-- https://mvnrepository.com/artifact/com.101tec/zkclient --> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>com.wzy</groupId> <artifactId>dubbo-component</artifactId> <version>1.0</version> </dependency> </dependencies>
package com.wzy.dubbo.web; import com.alibaba.dubbo.config.annotation.Reference; import com.wzy.component.entity.User; import com.wzy.component.server.IOrderServer; import com.wzy.component.server.IUserServer; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class WebCtl { @Reference private IOrderServer orderServer; @Reference private IUserServer userServer; @RequestMapping("getUser") public User getUser() { System.out.println("WebCtl getUser..."); System.out.println(orderServer.getName()); return userServer.getById(100); } }
4. 在zookeeper上查看注册节点