dubbo.png
节点角色说明
节点 | 角色说明 |
---|---|
Provider | 暴露服务的服务提供方 |
Consumer | 调用远程服务的服务消费方 |
Registry | 服务注册与发现的注册中心 |
Monitor | 统计服务的调用次数和调用时间的监控中心 |
Container | 服务运行的容器 |
调用关系说明
开发环境主要涉及以下方面:
具体代码可以查看 github 的 dubbo 模块: github.com/UniqueDong/…
定义服务接口,打成 jar 包让消费者依赖,服务者实现接口。该工程只有接口定义以及 model 对象。@Data 属于lombok 开源库提供的特性,方便开发。
@Data public class User implements Serializable { private Long id; private String username; } 复制代码
public interface UserProvider { List<User> listUser(); } 复制代码
引入spring-boot-starter,dubbo-api 接口就是我们上面提到的 接口定义 jar ,dubbo-spring-boot-starter ,dubbo-dependencies-zookeeper。
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-logging</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>zero.springboot.study</groupId> <artifactId>dubbo-api</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <!--dubbo start--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.1</version> </dependency> <!-- Zookeeper dependencies --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>2.7.1</version> <type>pom</type> <exclusions> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <!--dubbo end--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.57</version> </dependency> </dependencies> 复制代码
spring: application: name: dubbo-provider #自定义配置 embedded: zookeeper: # zookeeper 服务连接端口 port: 2181 # dubbo 配置 dubbo: # 注册中心配置 registry: id: dubbo-provider address: zookeeper://127.0.0.1:${embedded.zookeeper.port} group: local application: name: dubbo-provider id: dubbo-provider logger: slf4j qosEnable: true qosPort: 22224 qosAcceptForeignIp: false # dubbo 协议配置 protocol: # -1 表示使用随机未被占用的端口 port: -1 name: dubbo scan: # dubbo 服务提供者实现类所在包 base-packages: com.zero.provider.impl 复制代码
实现 api 定义的接口
注意 @Service 是 Dubbo 的,不要导入了 Spring 的。
import com.google.common.collect.Lists; import com.zero.api.model.User; import com.zero.api.provider.UserProvider; import org.apache.dubbo.config.annotation.Service; import java.util.List; @Service(interfaceClass = UserProvider.class) public class UserProviderImpl implements UserProvider { @Override public List<User> listUser() { User user = new User(); user.setId(1L); user.setUsername("青龙"); return Lists.newArrayList(user); } } 复制代码
我们要依赖 spring-boot-starter-web 提供http rest接口给前端调用。同时内部通过 Dubbo 实现 RPC调用服务提供者。
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>zero.springboot.study</groupId> <artifactId>dubbo-api</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> <!--dubbo start--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.7.1</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo</artifactId> <version>2.7.1</version> </dependency> <!-- Zookeeper dependencies --> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>2.7.1</version> <type>pom</type> <exclusions> <exclusion> <artifactId>log4j</artifactId> <groupId>log4j</groupId> </exclusion> <exclusion> <artifactId>slf4j-log4j12</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <!--dubbo end--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> </dependencies> 复制代码
server: # web 日更年期端口 port: 9005 spring: application: name: dubbo-comsumer #自定义配置 embedded: zookeeper: port: 2181 # dubbo 配置 dubbo: registry: id: dubbo-comsumer address: zookeeper://127.0.0.1:${embedded.zookeeper.port} group: local application: name: dubbo-comsumer id: dubbo-comsumer logger: slf4j qosEnable: false qosPort: 22223 qosAcceptForeignIp: false protocol: port: -1 name: dubbo # 是否检查服务提供者有效 consumer: check: false 复制代码
import com.zero.api.model.User; import com.zero.api.provider.UserProvider; import org.apache.dubbo.config.annotation.Reference; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService { @Reference private UserProvider userProvider; public List<User> listUser() { return userProvider.listUser(); } } 复制代码
@RestController @RequestMapping("/users") public class UserController { @Autowired private UserService userService; @GetMapping public Object listUser() { List<User> list = userService.listUser(); return list; } } 复制代码
各种具体协议、注册中心、多注册中心、超时等配置可以查看官方文档 dubbo.apache.org/zh-cn/docs/…
欢迎大家讨论纠正,邮箱:zero_develop@163.com 。点赞与关注微信公众号是我们前进的动力。
扫码_搜索联合传播样式-微信标准绿版.png