· CAP原则图解

 

 

一、dubbo概述

1.Apache Dubbo简介

  Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

  官网:http://dubbo.apache.org/

2.什么叫RPC

 

  RPCRemote Procedure Call】是指远程过程调用,是一种进程间通信方式,他是一种技术的思想,而不是规范。

  它允许程序调用另一个地址空间(通常是共享网络的另一台机器上)的过程或函数,而不用程序员显式编码这个远程调用的细节。

  即程序员无论是调用本地的还是远程的函数,本质上编写的调用代码基本相同。

3.dubbo的主要角色

 

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

 

  • 服务消费者(Consumer: 调用远程服务的服务消费方,服务消费者在启动时,向注册中心订阅自己所需的服务,服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

 

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

 

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

二、dubbo下载安装以及环境搭建

略。

三、dubbo跨项目调用初步demo(Java项目)

· Producer

1.pom.xml

<?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.sdkj</groupId>
    <artifactId>dubbo-ego-producer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.7</version>
        </dependency>

        <dependency>
            <groupId>com.101tec</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.11</version>
        </dependency>
        <!-- curator-framework -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.32.Final</version>
        </dependency>

    </dependencies>

</project>

2.applicationContext.xml

<?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://dubbo.apache.org/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.xsd
        http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <!-- 声明应用的名称 -->
    <dubbo:application name="ego-user-service-producer"/>
    <!--声明注册中心的地址-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!--把我们的服务发布到指定端口-->
    <dubbo:protocol name="dubbo" port="20881"/>
    <!-- 原生方式 -->
    <!-- 声明要发布的实现类对象
    <bean id="userService" class="com.sdkj.service.impl.UserServiceImpl"/>
    &lt;!&ndash; 进行服务发布,指向实现类 &ndash;&gt;
    <dubbo:service interface="com.sdkj.service.UserService" ref="userService"/>-->

    <!-- 注解方式注入,需要在类上面加入dubbo包的@Service -->
    <dubbo:annotation package="com.sdkj.service.impl"/>


</beans>

3.UserAddress

 1 package com.sdkj.pojo;
 2 
 3 import lombok.Data;
 4 
 5 import java.io.Serializable;
 6 
 7 /**
 8  * @Author wangshuo
 9  * @Date 2022/5/14, 13:46
10  * Please add a comment
11  */
12 @Data
13 public class UserAddress implements Serializable {
14 
15     private Integer id;
16     private String userAddress;
17     private String userId;
18 
19     public UserAddress() {
20     }
21 
22     public UserAddress(Integer id, String userAddress, String userId) {
23         this.id = id;
24         this.userAddress = userAddress;
25         this.userId = userId;
26     }
27 
28     public Integer getId() {
29         return id;
30     }
31 
32     public void setId(Integer id) {
33         this.id = id;
34     }
35 
36     public String getUserAddress() {
37         return userAddress;
38     }
39 
40     public void setUserAddress(String userAddress) {
41         this.userAddress = userAddress;
42     }
43 
44     public String getUserId() {
45         return userId;
46     }
47 
48     public void setUserId(String userId) {
49         this.userId = userId;
50     }
51 }

4.UserService

 1 package com.sdkj.service;
 2 
 3 import com.sdkj.pojo.UserAddress;
 4 
 5 import java.util.List;
 6 
 7 /**
 8  * @Author wangshuo
 9  * @Date 2022/5/14, 13:44
10  * Please add a comment
11  */
12 public interface UserService {
13 
14     /*
15         查询用户所有的地址
16      */
17     public List<UserAddress> queryAllAddress(String userid);
18 }

5.UserServiceImpl

 1 package com.sdkj.service.impl;
 2 
 3 import com.alibaba.dubbo.config.annotation.Service;
 4 import com.sdkj.pojo.UserAddress;
 5 import com.sdkj.service.UserService;
 6 
 7 import java.util.ArrayList;
 8 import java.util.List;
 9 
10 /**
11  * @Author wangshuo
12  * @Date 2022/5/14, 13:50
13  * Please add a comment
14  */
15 @Service
16 public class UserServiceImpl implements UserService {
17 
18     private static List<UserAddress> userAddresses = new ArrayList<>();
19     static {
20         userAddresses.add(new UserAddress(1,"北京二环","17"));
21         userAddresses.add(new UserAddress(2,"北京三环","17"));
22     }
23 
24     @Override
25     public List<UserAddress> queryAllAddress(String userid) {
26         return userAddresses;
27     }
28 }

6.TestStart

 1 package com.sdkj.controller;
 2 
 3 import org.springframework.context.ApplicationContext;
 4 import org.springframework.context.support.ClassPathXmlApplicationContext;
 5 
 6 import java.io.IOException;
 7 
 8 /**
 9  * @Author wangshuo
10  * @Date 2022/5/14, 15:32
11  * Please add a comment
12  */
13 public class TestStart {
14 
15     public static void main(String[] args) {
16 
17         ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
18         try {
19             System.out.println("加载成功");
20             System.in.read();
21         } catch (IOException e) {
22             e.printStackTrace();
23         }
24     }
25 }

· consumer

1.pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.sdkj</groupId>
 8     <artifactId>dubbo-ego-producer</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <properties>
12         <maven.compiler.source>8</maven.compiler.source>
13         <maven.compiler.target>8</maven.compiler.target>
14     </properties>
15 
16     <dependencies>
17 
18         <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
19         <dependency>
20             <groupId>org.projectlombok</groupId>
21             <artifactId>lombok</artifactId>
22             <version>1.18.16</version>
23             <scope>provided</scope>
24         </dependency>
25 
26         <dependency>
27             <groupId>com.alibaba</groupId>
28             <artifactId>dubbo</artifactId>
29             <version>2.6.7</version>
30         </dependency>
31 
32         <dependency>
33             <groupId>com.101tec</groupId>
34             <artifactId>zkclient</artifactId>
35             <version>0.11</version>
36         </dependency>
37         <!-- curator-framework -->
38         <dependency>
39             <groupId>org.apache.curator</groupId>
40             <artifactId>curator-framework</artifactId>
41             <version>4.1.0</version>
42         </dependency>
43         <dependency>
44             <groupId>io.netty</groupId>
45             <artifactId>netty-all</artifactId>
46             <version>4.1.32.Final</version>
47         </dependency>
48 
49     </dependencies>
50 
51 </project>

2.applicationContext.xml

<?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://dubbo.apache.org/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.xsd
        http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.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://127.0.0.1:2181"/>


    <!-- 原生方式 -->
    <!--生成远程调用对象-->
    <!--<dubbo:reference id="userService" interface="com.sdkj.service.UserService"/>-->
    <!--创建订单服务对象 指向远程通信对象 -->
    <!--<bean id="orderService" class="com.sdkj.service.impl.OrderServiceImpl">
        <property name="userService" ref="userService"/>
    </bean>-->


    <!-- 注解方式,需要在类上面加入dubbo包的@Service 以及 @Reference引入接口 -->
    <dubbo:annotation package="com.sdkj.service.impl"/>
    <context:component-scan base-package="com.sdkj.service.impl"/>

</beans>

3.UserAddress

 1 package com.sdkj.pojo;
 2 
 3 import lombok.Data;
 4 
 5 import java.io.Serializable;
 6 
 7 /**
 8  * @Author wangshuo
 9  * @Date 2022/5/14, 13:46
10  * Please add a comment
11  */
12 @Data
13 public class UserAddress implements Serializable {
14 
15     private Integer id;
16     private String userAddress;
17     private String userId;
18 
19     public UserAddress() {
20     }
21 
22     public UserAddress(Integer id, String userAddress, String userId) {
23         this.id = id;
24         this.userAddress = userAddress;
25         this.userId = userId;
26     }
27 
28     public Integer getId() {
29         return id;
30     }
31 
32     public void setId(Integer id) {
33         this.id = id;
34     }
35 
36     public String getUserAddress() {
37         return userAddress;
38     }
39 
40     public void setUserAddress(String userAddress) {
41         this.userAddress = userAddress;
42     }
43 
44     public String getUserId() {
45         return userId;
46     }
47 
48     public void setUserId(String userId) {
49         this.userId = userId;
50     }
51 }

4.OrderService

 1 package com.sdkj.service;
 2 
 3 import com.sdkj.pojo.UserAddress;
 4 
 5 import java.util.List;
 6 
 7 /**
 8  * @Author wangshuo
 9  * @Date 2022/5/14, 13:58
10  * Please add a comment
11  */
12 public interface OrderService {
13 
14     public List<UserAddress> initOrder(String userid);
15 }

5.UserService

 1 package com.sdkj.service;
 2 
 3 import com.sdkj.pojo.UserAddress;
 4 
 5 import java.util.List;
 6 
 7 /**
 8  * @Author wangshuo
 9  * @Date 2022/5/14, 13:44
10  * Please add a comment
11  */
12 public interface UserService {
13 
14     /*
15         查询用户所有的地址
16      */
17     public List<UserAddress> queryAllAddress(String userid);
18 }

6.OrderServiceImpl

 1 package com.sdkj.service.impl;
 2 
 3 import com.alibaba.dubbo.config.annotation.Reference;
 4 import com.alibaba.dubbo.config.annotation.Service;
 5 import com.sdkj.pojo.UserAddress;
 6 import com.sdkj.service.OrderService;
 7 import com.sdkj.service.UserService;
 8 
 9 import java.util.List;
10 
11 /**
12  * @Author wangshuo
13  * @Date 2022/5/14, 14:46
14  * Please add a comment
15  */
16 @Service
17 public class OrderServiceImpl implements OrderService {
18 
19     //@Autowired
20     @Reference
21     private UserService userService;
22 
23     public void setUserService(UserService userService) {
24         this.userService = userService;
25     }
26 
27     @Override
28     public List<UserAddress> initOrder(String userid) {
29         return userService.queryAllAddress(userid);
30     }
31 }

7.TestStart

 1 package com.sdkj.controller;
 2 
 3 import com.sdkj.pojo.UserAddress;
 4 import com.sdkj.service.OrderService;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 import java.io.IOException;
 9 import java.util.List;
10 
11 /**
12  * @Author wangshuo
13  * @Date 2022/5/14, 15:51
14  * Please add a comment
15  */
16 public class TestStart {
17 
18     public static void main(String[] args) {
19         ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
20         //获取Order
21         OrderService bean = context.getBean(OrderService.class);
22         List<UserAddress> addresses = bean.initOrder("张三");
23         for (UserAddress address : addresses) {
24             System.out.println(address);
25         }
26         try {
27             System.in.read();
28         } catch (IOException e) {
29             e.printStackTrace();
30         }
31     }
32 }