就是多个独立计算机的集合,这些计算机对于用户来说就像是单个相关系统。
个人理解:传统垂直系统就像是独居,而分布式就是相当于是合居。独居的时候自己买菜、洗菜、做饭。合居的时候就可以对象洗菜的时候自己做菜。大家各干各的,节省了时间还轻松了一些,互相协作为了一个共同的目标——吃饭。希望早日大家都有对象。
发展演变:
Apache Dubbo 是一款高性能、轻量级的开源Java RPC 框架,但是是由阿里巴巴开发的。提供了三大核心功能:
面向接口的远程方法调用智能容错和负载均衡服务自动注册和发现服务提供者(provider):暴露服务的服务提供方,服务提供者启动时,向注册中心注册自己提供的服务。
服务消费者(consumer):调用远程服务消费方,服务消费者在启动时,向注册中心订阅自己所需要的服务;服务消费者从提供者地址列表中,基于负载均衡算法,选择一台提供者进行调用,如果调用失败,再调用一台。失败三次就不再调用了。
注册中心(registry):注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
监控中心(monitor):服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。
调用关系:
服务容器负责启动,加载,运行服务提供者;服务提供者在启动时,向注册中心注册自己的服务;服务消费者在启动时,向注册中心订阅自己所需的服务;注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者;服务消费者从提供者地址列表中,基于负载均衡算法,选择一台提供者进行调用。下载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的值下载: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=truejar包下载完毕之后查看一下application.properties里面的用户名密码和端口等等。
在启动之前需要先启动zookeeper。
然后在target文件下运行jar
java -jar dubbo...jar访问localhost:7001。
默认用户和密码为root。
在系统管理中可以看到已经注册的zookeeper。
跟在windows上面基本是一样。
下载:
https://archive.apache.org/dist/zookeeper/
上传:
解压:
修改配置文件:
跟win一样,将conf目录下的zoo_sample.cfg复制一份改为zoo.cfg。
存储目录和端口根据自己的需求。
启动服务:
zkServer.sh start启动客户端测试:
zkCli.sh跟win一样,这里就不赘述了。
主要是服务器坏了 呜呜呜 。
需求:订单web模块位于服务器A,用户service模块位于服务器B。服务器A可以远程调用B的功能。
结构:
整体项目结构:
实体类(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(); }实现类:
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()); } } }实现类:
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的端口,再新启动一个,就会有多个生产者。
在非注解版本的基础上进行修改。
配置文件:删除了注入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);
实现类:这个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>