Dubbo——Day01

it2025-10-13  10

Dubbo

1. 分布式系统

就是多个独立计算机的集合,这些计算机对于用户来说就像是单个相关系统。

个人理解:传统垂直系统就像是独居,而分布式就是相当于是合居。独居的时候自己买菜、洗菜、做饭。合居的时候就可以对象洗菜的时候自己做菜。大家各干各的,节省了时间还轻松了一些,互相协作为了一个共同的目标——吃饭。希望早日大家都有对象。

发展演变:

2. 什么是Dubbo

Apache Dubbo 是一款高性能、轻量级的开源Java RPC 框架,但是是由阿里巴巴开发的。提供了三大核心功能:

面向接口的远程方法调用智能容错和负载均衡服务自动注册和发现

服务提供者(provider):暴露服务的服务提供方,服务提供者启动时,向注册中心注册自己提供的服务。

服务消费者(consumer):调用远程服务消费方,服务消费者在启动时,向注册中心订阅自己所需要的服务;服务消费者从提供者地址列表中,基于负载均衡算法,选择一台提供者进行调用,如果调用失败,再调用一台。失败三次就不再调用了。

注册中心(registry):注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

监控中心(monitor):服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

调用关系:

服务容器负责启动,加载,运行服务提供者;服务提供者在启动时,向注册中心注册自己的服务;服务消费者在启动时,向注册中心订阅自己所需的服务;注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者;服务消费者从提供者地址列表中,基于负载均衡算法,选择一台提供者进行调用。

3. 搭建Dubbo环境

3.1 Windows

3.1.1 安装zookeeper

下载zookeeper:https://archive.apache.org/dist/zookeeper/

解压之后将conf目录下的zoo_sample.cfg复制一份改为zoo.cfg。

配置文件:

dataDir=./ 临时文件存储的目录

clientPort=2181 端口号

启动 zkService.cmd表示启动本地的zookeeper服务进程。

启动zkCli.md客户端测试:

ls/ 列出zookeeper跟下存储的所有节点 create -e /sxt 123 创建一个/sxt节点值为123 get /sxt 获取节点/sxt的值

3.1.2 安装Dubbo-admin

下载:https://github.com/apache/dubbo-admin/tree/master

是一个SpringBoot项目

解压之后进入dubbo-admin目录下进入cmd界面,运行mvn package导入jar包。

如果需要忽略 PKIX 认证和证书等问题 运行:

mvn package -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true

jar包下载完毕之后查看一下application.properties里面的用户名密码和端口等等。

在启动之前需要先启动zookeeper。

然后在target文件下运行jar

java -jar dubbo...jar

访问localhost:7001。

默认用户和密码为root。

在系统管理中可以看到已经注册的zookeeper。

3.2 Linux

3.2.1 安装zookeeper

跟在windows上面基本是一样。

下载:

https://archive.apache.org/dist/zookeeper/

上传:

解压:

修改配置文件:

跟win一样,将conf目录下的zoo_sample.cfg复制一份改为zoo.cfg。

存储目录和端口根据自己的需求。

启动服务:

zkServer.sh start

启动客户端测试:

zkCli.sh

3.2.2 安装Dubbo-admin

跟win一样,这里就不赘述了。

主要是服务器坏了 呜呜呜 。

4. Hello Dubbo

需求:订单web模块位于服务器A,用户service模块位于服务器B。服务器A可以远程调用B的功能。

结构:

整体项目结构:

4.1 项目依赖(外部的依赖在父项目中应用):

<dependencies> <!-- lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> <!-- dubbo--> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.7</version> </dependency> <!-- zookeeper客户端--> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.11</version> </dependency> <!-- 辅助包--> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>4.1.0</version> </dependency> <!-- netty服务器--> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.32.Final</version> </dependency> </dependencies>

4.2 非注解版本

4.2.1 inter-公共模块

实体类(domain):

/** * @date 2020/10/12 14:04 */ @Data @NoArgsConstructor @AllArgsConstructor public class UserAddress implements Serializable { private Integer id; private String userAddress; private String userId; }

接口:

OrderService:

public interface OrderService { List<UserAddress> initOrder(); }

UserService:

public interface UserService { List<UserAddress> queryAllAddress(); }

4.2.2 消费者:

实现类:

OrderServiceImpl:

/** * @date 2020/10/12 14:08 */ public class OrderServiceImpl implements OrderService { private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } public List<UserAddress> initOrder() { return userService.queryAllAddress(); } }

配置文件:

<?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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 声明应用程序名称--> <dubbo:application name="ego-user-service-consumer"/> <!-- 指定注册中心--> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 生成远程调用对象--> <dubbo:reference id="userService" interface="com.ego.inter.service.UserService"/> <!-- 创建订单对象--> <bean id="orderService" class="com.ego.order.service.impl.OrderServiceImpl"> <property name="userService" ref="userService"/> </bean> </beans>

启动类(测试类):

/** * @date 2020/10/12 14:10 */ public class TestConsumer { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); // 从IOC得到orderServiceImpl OrderService orderService = context.getBean(OrderService.class); List<UserAddress> userAddresses = orderService.initOrder(); for (UserAddress userAddress : userAddresses) { System.out.println(userAddress.getId()+"-==-"+userAddress.getUserAddress()); } } }

4.2.3 生产者:

实现类:

UserServiceImpl:

/** * @date 2020/10/12 11:16 */ public class UserServiceImpl implements UserService { public List<UserAddress> queryAllAddress() { UserAddress userAddress = new UserAddress(1,"山东省日照市东港区","吴彦祖"); UserAddress userAddress1 = new UserAddress(2,"山东省济宁市任城区","吴亦凡"); List<UserAddress> addresses = new ArrayList<UserAddress>(); addresses.add(userAddress); addresses.add(userAddress1); System.out.println("20881"); return addresses; } }

配置文件:

<?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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 声明应用程序名称--> <dubbo:application name="ego-user-service-provider"/> <!-- 指定注册中心--> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 使用dubbo协议,将服务暴露在20880端口--> <dubbo:protocol name="dubbo" port="20881"/> <!-- 声明要暴露的实现类的对象--> <bean id="userService" class="com.ego.user.serivce.impl.UserServiceImpl"/> <!-- 进行服务暴露--> <dubbo:service interface="com.ego.inter.service.UserService" ref="userService"/> </beans>

启动类:

/** * @date 2020/10/12 14:35 */ public class TestProvider { public static void main(String[] args) throws IOException { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); System.out.println("启动服务提供者成功"); // 让程序运行完毕之后不退出 System.in.read(); } }

启动多个生产者的方法:先启动一个生产者A,在修改配置文件中想要暴露的buddo的端口,再新启动一个,就会有多个生产者。

4.3 注解版

在非注解版本的基础上进行修改。

4.3.1 服务提供者

配置文件:删除了注入bean和暴露的操作,新增了扫描包。

<?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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 声明应用程序名称--> <dubbo:application name="ego-user-service-provider"/> <!-- 指定注册中心--> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 使用dubbo协议,将服务暴露在20880端口--> <dubbo:protocol name="dubbo" port="20881"/> <!-- 添加扫描目录--> <dubbo:annotation package="com.ego.user.serivce.impl"/> </beans>

实现类:

在实现类上添加@Service注解(com.alibaba.dubbo.config.annotation.Service);

4.3.2 服务消费者

实现类:这个Service注解是Spring的注解,Reference是dubbo下的。

/** * @date 2020/10/12 14:08 */ @Service public class OrderServiceImpl implements OrderService { @Reference private UserService userService; public List<UserAddress> initOrder() { return userService.queryAllAddress(); } }

修改配置文件:在后面新增两个扫描。

<?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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 声明应用程序名称--> <dubbo:application name="ego-user-service-consumer"/> <!-- 指定注册中心--> <dubbo:registry address="zookeeper://localhost:2181"/> <!-- 扫描--> <!-- 这个是扫描Reference注解--> <dubbo:annotation package="com.ego.order.service.impl"/> <!-- 这个是扫描Spring的Service注解--> <context:component-scan base-package="com.ego.order.service.impl"/> </beans>
最新回复(0)