dubbo 实现
- 创建项目
<?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.sean.dubbo</groupId>
<artifactId>dubbo-order</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>order-api</module>
<module>order-provider</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
-
api项目中创建接口和实体(实体需要实现序列化接口,dubbo协议传输需要),并在xml中配置消费者的引用
-
创建接口
public interface OrderService { BaseResponse saveOrder(Order order); BaseResponse<Order> queryOrder(String id); }
-
创建实体
@Data @ToString @NoArgsConstructor @AllArgsConstructor public class Order implements Serializable { private String id; private String orderName; private int orderCount; }
@Data @ToString @NoArgsConstructor @AllArgsConstructor public class BaseResponse<T> implements Serializable { private String code; private String msg; private T data; }
-
提供消费者的引用
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName"> <context:annotation-config></context:annotation-config> <!--订阅接口--> <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" protocol="dubbo" version="1.0"/> </beans>
-
-
provider项目实现api中创建的接口
@Service("orderService") public class OrderServiceImpl implements OrderService { private static List<Order> orders = new ArrayList<Order>(); public BaseResponse saveOrder(Order order) { if (order == null) { return new BaseResponse("010", "order is null", null); } orders.add(order); return new BaseResponse("0", "success", null); } public BaseResponse<Order> queryOrder(String id) { if (StringUtils.isEmpty(id)) { return new BaseResponse("010", "id is null", null); } for (Order order : orders) { if (order == null) { continue; } if (id.equals(order.getId())) { return new BaseResponse("0", "success", order); } } return new BaseResponse("010", "no order", null); } }
-
发布接口至注册中心(会将接口存储至zookeeper的一个节点上,包含接口的各部分信息)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName"> <context:annotation-config></context:annotation-config> <context:component-scan base-package="com.sean.dubbo.order"/> <!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签--> <dubbo:application name="order-provider" owner="sean"/> <!--dubbo这个服务所要暴露的服务地址所对应的注册中心--> <dubbo:registry protocol="zookeeper" address="192.168.71.19:2181"/> <!--当前服务发布所依赖的协议;webserovice、Thrift、Hessain、http--> <dubbo:protocol name="dubbo" port="20880" host="10.13.65.17"/> <!--增加hessian协议--> <dubbo:protocol name="hession" port="8090" server="jetty"/> <!--服务发布的配置,需要暴露的服务接口--> <dubbo:service interface="com.sean.dubbo.order.service.OrderService" ref="orderService" protocol="dubbo" version="1.0" loadbalance="random"/> <!--服务发布的配置,需要暴露的服务接口--> <dubbo:service interface="com.sean.dubbo.order.service.OrderService" ref="orderService" protocol="hession" version="1.2" loadbalance="random"/> </beans>
5.消费者注册订阅
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-autowire="byName">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.sean.dubbo.user"/>
<!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签-->
<dubbo:application name="user-provider" owner="mic"/>
<!--dubbo这个服务所要暴露的服务地址所对应的注册中心-->
<dubbo:registry protocol="zookeeper" address="192.168.71.19:2181"/>
<!--引用消费的配置文件-->
<import resource="classpath*:order-customer.xml"/>
</beans>
-
实现测试
public class App { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("user-provider.xml"); // 获取接口,调用 OrderService orderService = (OrderService) applicationContext.getBean("orderService"); Order order = new Order("aaa", "sss", 12); BaseResponse baseResponse = orderService.saveOrder(order); System.out.println(baseResponse); BaseResponse<Order> aaa = orderService.queryOrder("aaa"); System.out.println(aaa); } }
dubbo 控制台
控制台主要用于服务治理,控制服务的权重,路由等
- 在以下链接的最后,找到Dubbo Admin
-
克隆或下载源码到本地`
-
修改以下文件的注册地址
dubbo-admin-server/src/main/resources/application-production.properties
-
maven 进行打包
mvn clean package
- 启动项目
mvn --projects dubbo-admin-server spring-boot:run
或者cd dubbo-admin-distribution/target
;java -jar dubbo-admin-0.1.jar
- 访问
http://localhost:8080
dubbo 监控中心
监控中心主要查看服务间的调用情况和调用关系以及响应事件
- 在以下链接拉取整个dubbo源码
- 配置monitor项目下的dubbo.properties文件的注册地址
- 启动项目,使用命令
- 访问
http://localhost:8080
dubbo 配置
相关配置
-
启动服务检查
<!--check 会检查引用的服务是否存在,默认检查--> <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" check="false" />
-
多协议
<!--dubbo协议--> <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" protocol="dubbo"/> <!--hessian协议--> <dubbo:reference id="orderService2" interface="com.sean.dubbo.order.service.OrderService" protocol="hessian"/>
-
多版本
<!--1.0.0版本--> <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" version="1.0.0"/> <!--1.2.0版本--> <dubbo:reference id="orderService2" interface="com.sean.dubbo.order.service.OrderService" version="1.1.0"/>
-
多注册中心
<dubbo:registry protocol="zookeeper" address="192.168.71.19:2181" check="true"/> <dubbo:registry protocol="redis" address="xxx" check="true"/>
-
异步调用
<!--采取异步调用策略--> <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" async="true"/>
-
只订阅
<!--只订阅服务--> <dubbo:registry protocol="zookeeper" address="192.168.71.19:2181" register="false"/>
-
只注册
<!--只注册服务--> <dubbo:registry protocol="zookeeper" address="192.168.71.19:2181" subscribe="false"/>
-
负载均衡
<!--随机的负载均衡--> <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" loadbalance="random"/>
-
集群容错
<!--快速失败的容错方式,可选:failover/failfast/failsafe/failback/forking--> <dubbo:reference id="orderService" interface="com.sean.dubbo.order.service.OrderService" cluster="failfast"/>
-
配置管理员信息
<!--当前项目在整个分布式架构里面的唯一名称,计算依赖关系的标签--> <dubbo:application name="user-provider" owner="mic"/>
application上面配置的owner,建议配置2个人以上,因为owner都能够在监控中心看到。
-
配置dubbo缓存文件
<!--配置缓存文件--> <dubbo:registry protocol="zookeeper" address="192.168.71.19:2181" file="xxx"/>
重启后,内存数据不存在,也无法连接注册中心的情况下,可以通过缓存文件找到注册中心列表和服务提供者列表
配置优先级
消费端方法优先级最高– 服务端提供者最低
dubbo 源码
- 自定义spring中dubbo的xml标签,并加入对应的解析器
- 解析xml标签,初始化实例对象,并在类上实现initializingBean,DisposableBean等接口,对实体对象做代理功能的扩展,如容错或负载均衡
- 服务的发布是通过spring的afterPropertiesSet方法进行发布,通过代理工厂获取对应的Invoker,再通过相应的协议调用invoker,进行发布;服务发布实际是将协议的url和接口的数据保存到注册中心,如zookeeper,则是保存到znode节点下
- 服务的订阅则是通过spring的afterPropertiesSet方法创建代理对象,再通过协议获取InvokerProxy,用户调用service时,会通过调用Invoker的invoke方法发起请求;服务订阅实际是订阅注册中心下的节点数据,获取接口信息,再进行调用