• Dubbo学习笔记


    一、说明:

    本次学习Dubbo,分别针对三种情况组合写个三个Demo,每个Demo都提供了两个程序:Providerconsumerprovider就是服务的提供方,对外开放接口,consumer是服务的消费方,调用provider提供的服务(通过RPC的方式调用的);

    Providerconsumer程序都公用一个相同的接口(包名和类型必须完全相同,否则调用会报错的),这个接口可以放到一个公共的api包里面,然后providerconsumer都要依赖这个包;我在这里就偷了个懒,直接把接口定义到程序里面了;这三个Demo里面使用的公共接口定义如下:

    1 public interface TestIntf {
    2     String sayHello();
    3 }

    此次将学习时搭建的三个Demo记录下来,方便以后查阅;

    二、SpringMVC+Zookeeper+Dubbo的搭建:

    1、先搭建一个SpringMVC工程;

    2、POM依赖:

     1 <properties>
     2     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     3     <maven.compiler.source>1.7</maven.compiler.source>
     4     <maven.compiler.target>1.7</maven.compiler.target>
     5     <spring.version>4.2.6.RELEASE</spring.version>
     6   </properties>
     7 
     8   <dependencies>
     9 
    10     <dependency>
    11       <groupId>org.springframework</groupId>
    12       <artifactId>spring-beans</artifactId>
    13       <version>${spring.version}</version>
    14     </dependency>
    15 
    16     <!-- dubbo相关 -->
    17     <dependency>
    18       <groupId>com.alibaba</groupId>
    19       <artifactId>dubbo</artifactId>
    20       <version>2.5.6</version>
    21     </dependency>
    22     <dependency>
    23       <groupId>org.apache.zookeeper</groupId>
    24       <artifactId>zookeeper</artifactId>
    25       <version>3.4.12</version>
    26     </dependency>
    27 
    28     <dependency>
    29       <groupId>com.github.sgroschupf</groupId>
    30       <artifactId>zkclient</artifactId>
    31       <version>0.1</version>
    32     </dependency>
    33 
    34     <dependency>
    35       <groupId>org.springframework</groupId>
    36       <artifactId>spring-web</artifactId>
    37       <version>${spring.version}</version>
    38     </dependency>
    39     <dependency>
    40       <groupId>org.springframework</groupId>
    41       <artifactId>spring-webmvc</artifactId>
    42       <version>${spring.version}</version>
    43     </dependency>
    44     <dependency>
    45       <groupId>org.springframework</groupId>
    46       <artifactId>spring-context</artifactId>
    47       <version>${spring.version}</version>
    48     </dependency>
    49   </dependencies>

    3、applicationContext.xml配置文件的定义:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
     5        xmlns:context="http://www.springframework.org/schema/context"
     6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7                            http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     8     
     9     <context:component-scan base-package="com.demo.dubbo.provider" />
    10 
    11     <dubbo:application name="dubbo-provider"/>
    12     <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    13     <dubbo:protocol name="dubbo" port="20880" />
    14     <dubbo:annotation package="com.demo.dubbo.provider.service.impl" />
    15 </beans>

    4、服务实现类:

    1 import com.alibaba.dubbo.config.annotation.Service;
    2 
    3 @Service
    4 public class TestIntfImpl implements TestIntf {
    5     @Override
    6     public String sayHello() {
    7         return "hello, this is a dubbo demo project";
    8     }
    9 }

    注意:这里引用的@Service注解必须是dubbo包里面定义的,不能是Spring里面定义的那个,否则报错信息会莫名其妙;

    5、同样可以在provider包里面实现Controller并调用刚才的服务接口:

     1 @Controller
     2 public class IndexController {
     3 
     4     @Resource
     5     private TestIntf testIntf;
     6 
     7     @RequestMapping(value = "/home")
     8     @ResponseBody
     9     public String index() {
    10         return testIntf.sayHello();
    11     }
    12 }

    6、建立一个SpringMVC工程consumerPOM.xml文件的定义和provider工程的一样;

    7、Dubbo.xml文件定义:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
     5        xsi:schemaLocation="http://www.springframework.org/schema/beans
     6      http://www.springframework.org/schema/beans/spring-beans.xsd
     7      http://code.alibabatech.com/schema/dubbo
     8      http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
     9 
    10     <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
    11     <dubbo:application name="dubbo-consumer"  />
    12 
    13     <!--&lt;!&ndash; 使用multicast广播注册中心暴露发现服务地址 &ndash;&gt;-->
    14     <dubbo:registry address="zookeeper://127.0.0.1:2181" />
    15 
    16     <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
    17     <dubbo:reference id="testIntf" interface="com.demo.dubbo.TestIntf" />
    18 </beans>

    8、dispatcher-servlet.xml文件引入dubbo.xml文件:

    <import resource="dubbo.xml"/>

    9、Controller中调用RPC服务:

     1 @Controller
     2 public class IndexController {
     3     @Resource
     4     private TestIntf testIntf;
     5 
     6     @RequestMapping("/home")
     7     @ResponseBody
     8     public String index() {
     9         return testIntf.sayHello();
    10     }
    11 }

    三、SpringBoot+Zookeeper+Dubbo的搭建:

    1、新建立两个SpringBoot工程providerconsumer

    2、POM.xml文件引入依赖(providerconsumer工程引入的依赖是一样的):

     1 <properties>
     2         <java.version>1.8</java.version>
     3     </properties>
     4 
     5     <dependencies>
     6         <dependency>
     7             <groupId>org.springframework.boot</groupId>
     8             <artifactId>spring-boot-starter-web</artifactId>
     9         </dependency>
    10         <dependency>
    11             <groupId>com.alibaba.spring.boot</groupId>
    12             <artifactId>dubbo-spring-boot-starter</artifactId>
    13             <version>2.0.0</version>
    14         </dependency>
    15         <dependency>
    16             <groupId>com.101tec</groupId>
    17             <artifactId>zkclient</artifactId>
    18             <version>0.3</version>
    19         </dependency>
    20         <dependency>
    21             <groupId>org.apache.zookeeper</groupId>
    22             <artifactId>zookeeper</artifactId>
    23             <version>3.4.6</version>
    24         </dependency>
    25     </dependencies>

    3、dubbo.properties文件配置:

    1 spring.dubbo.application.name=provider_boot
    2 spring.dubbo.registry.protocol=zookeeper
    3 spring.dubbo.registry.address=127.0.0.1:2181
    4 spring.dubbo.protocol.name=dubbo
    5 spring.dubbo.protocol.port=20880
    6 spring.dubbo.scan=com.demo.dubbo.provider

    4、dubbo.xml文件配置:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
     4        xsi:schemaLocation="http://www.springframework.org/schema/beans
     5     http://www.springframework.org/schema/beans/spring-beans.xsd
     6     http://code.alibabatech.com/schema/dubbo
     7     http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
     8 
     9     <bean id="testIntf" class="com.demo.dubbo.provider.service.impl.TestIntfImpl" />
    10 
    11     <!-- 提供方应用信息,用于计算依赖关系 -->
    12     <dubbo:application name="provider_boot" />
    13     <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
    14     <!-- 暴露服务 -->
    15     <dubbo:protocol name="dubbo" port="20880" />
    16     <dubbo:service interface="com.demo.dubbo.TestIntf"
    17                    ref="testIntf" retries="0" timeout="6000" />
    18 </beans>

    5、配置类DubboConfig.java

    1 @Configuration
    2 @PropertySource("/dubbo.properties")
    3 @ImportResource({"/dubbo.xml"})
    4 public class DubboConfig {
    5 }

    6、服务实现类:

    1 import com.alibaba.dubbo.config.annotation.Service;
    2 
    3 @Service(version = "1.0.0")
    4 public class TestIntfImpl implements TestIntf {
    5     @Override
    6     public String sayHello() {
    7         return "this is a provider Demo for SpringBoot and Dubbo";
    8     }
    9 }

    7、Consumer程序的dubbo.properties文件定义:

    1 spring.dubbo.application.name=consumer_boot
    2 spring.dubbo.registry.protocol=zookeeper
    3 spring.dubbo.registry.address=127.0.0.1:2181
    4 spring.dubbo.protocol.name=dubbo
    5 spring.dubbo.protocol.port=20880
    6 spring.dubbo.scan=com.demo.dubbo.consumer

    8、Consumer程序的dubbo.xml文件定义:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
     4        xsi:schemaLocation="http://www.springframework.org/schema/beans
     5     http://www.springframework.org/schema/beans/spring-beans.xsd
     6     http://code.alibabatech.com/schema/dubbo
     7     http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
     8 
     9     <!-- 提供方应用信息,用于计算依赖关系 -->
    10     <dubbo:application name="consumer_boot" />
    11     <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
    12     <!-- 暴露服务 -->
    13     <dubbo:protocol name="dubbo" port="20880" />
    14     <dubbo:reference id="testIntf" interface="com.demo.dubbo.TestIntf" />
    15 </beans>

    如果consumerprovider程序是在一台机器上运行的,那么这里就要在application.properties文件里面更改下端口;

    9、配置文件DubboConfig.java文件定义:

    1 @Configuration
    2 @PropertySource("/dubbo.properties")
    3 @ImportResource({"/dubbo.xml"})
    4 public class DubboConfig {
    5 }

    10、RPC服务调用的Controller的调用实现:

     1 @RestController
     2 @RequestMapping("/consumer")
     3 public class IndexController {
     4 
     5     @Resource
     6     private TestIntf testIntf;
     7 
     8     @GetMapping("/home")
     9     public String index() {
    10         return testIntf.sayHello();
    11     }
    12 }

    四、SpringBoot+nacos+Dubbo的搭建:

    1、新建立两个SpringBoot工程providerconsumer

    2、POM.xml文件引入依赖(consumer工程的依赖是一样的):

     1 <properties>
     2     <java.version>1.8</java.version>
     3     <nacos-config-spring-boot.version>0.2.1</nacos-config-spring-boot.version>
     4     <netty-all.version>4.0.35.Final</netty-all.version>
     5 </properties>
     6 
     7 <dependencies>
     8     <!--springboot有关-->
     9     <dependency>
    10         <groupId>org.springframework.boot</groupId>
    11         <artifactId>spring-boot-starter-actuator</artifactId>
    12     </dependency>
    13     <dependency>
    14         <groupId>org.springframework.boot</groupId>
    15         <artifactId>spring-boot-starter</artifactId>
    16     </dependency>
    17     <dependency>
    18         <groupId>org.springframework.boot</groupId>
    19         <artifactId>spring-boot-starter-web</artifactId>
    20     </dependency>
    21     <dependency>
    22         <groupId>org.springframework.boot</groupId>
    23         <artifactId>spring-boot-starter-test</artifactId>
    24         <scope>test</scope>
    25     </dependency>
    26 
    27     <!--dubbo有关-->
    28     <dependency>
    29         <groupId>com.alibaba</groupId>
    30         <artifactId>dubbo-registry-nacos</artifactId>
    31         <version>2.6.6</version>
    32     </dependency>
    33     <dependency>
    34         <groupId>com.alibaba.nacos</groupId>
    35         <artifactId>nacos-client</artifactId>
    36         <version>0.6.2</version>
    37     </dependency>
    38     <dependency>
    39         <groupId>com.alibaba</groupId>
    40         <artifactId>dubbo-common</artifactId>
    41         <version>2.6.6</version>
    42     </dependency>
    43     <dependency>
    44         <groupId>com.alibaba</groupId>
    45         <artifactId>dubbo-registry-api</artifactId>
    46         <version>2.6.6</version>
    47     </dependency>
    48     <dependency>
    49         <groupId>com.alibaba</groupId>
    50         <artifactId>dubbo</artifactId>
    51         <version>2.6.6</version>
    52     </dependency>
    53     <dependency>
    54         <groupId>com.alibaba.boot</groupId>
    55         <artifactId>dubbo-spring-boot-starter</artifactId>
    56         <version>0.2.1.RELEASE</version>
    57     </dependency>
    58     <dependency>
    59         <groupId>com.alibaba.boot</groupId>
    60         <artifactId>dubbo-spring-boot-autoconfigure</artifactId>
    61         <version>0.2.1.RELEASE</version>
    62     </dependency>
    63     <dependency>
    64         <groupId>io.netty</groupId>
    65         <artifactId>netty-all</artifactId>
    66         <version>${netty-all.version}</version>
    67     </dependency>
    68     <!-- rest有关 -->
    69     <dependency>
    70         <groupId>org.jboss.resteasy</groupId>
    71         <artifactId>jaxrs-api</artifactId>
    72         <version>3.0.12.Final</version>
    73     </dependency>
    74     <dependency>
    75         <groupId>org.jboss.resteasy</groupId>
    76         <artifactId>resteasy-client</artifactId>
    77         <version>3.0.12.Final</version>
    78     </dependency>
    79 </dependencies>

    3、Application.yml配置文件定义:

     1 dubbo:
     2   application:
     3     name: provider-nacos-demo
     4     id: provider-nacos-demo
     5   registry:
     6     address: nacos://127.0.0.1:8848
     7   protocols:
     8     dubbo:
     9       name: dubbo
    10       port: -1
    11       server: tomcat
    12 spring:
    13   application:
    14     name: provider-nacos-demo
    15   main:
    16     allow-bean-definition-overriding: true
    17 server:
    18   port: 8080

    4、接口实现类:

     1 import com.alibaba.dubbo.config.annotation.Service;
     2 
     3 @Service
     4 public class TestIntfImpl implements TestIntf {
     5 
     6     @Override
     7     public String sayHello() {
     8         return "这是provider程序远程调用返回的结果";
     9     }
    10 }

    5、应用程序类ProviderApplication需要添加@EnableDubbo注解;consumer工程的应用程序类也要添加这个注解;

    6、Consumer工程的application.yml配置如下:

     1 dubbo:
     2   application:
     3     name: consumer-nacos-demo
     4     id: consumer-nacos-demo
     5   registry:
     6     address: nacos://127.0.0.1:8848
     7   protocols:
     8     dubbo:
     9       name: dubbo
    10       port: -2
    11       server: tomcat
    12 spring:
    13   application:
    14     name: consumer-nacos-demo
    15   main:
    16     allow-bean-definition-overriding: true
    17 server:
    18   port: 8081

    7、Controller中调用provider中的RPC服务代码:

     1 import com.alibaba.dubbo.config.annotation.Reference;
     2 import org.springframework.web.bind.annotation.GetMapping;
     3 import org.springframework.web.bind.annotation.RequestMapping;
     4 import org.springframework.web.bind.annotation.RestController;
     5 
     6 @RestController
     7 @RequestMapping("/consumer")
     8 public class IndexController {
     9 
    10     @Reference
    11     private TestIntf testIntf;
    12 
    13     @GetMapping("/home")
    14     public String getUser(){
    15         String result = testIntf.sayHello();
    16         return result;
    17     }
    18 }
  • 相关阅读:
    安装和使用 PyInstaller 遇到的问题
    Mininet 系列实验(七)
    Mininet 系列实验(六)
    Mininet 系列实验(五)
    Mininet 系列实验(三)
    Mininet 系列实验(一)
    Linux进程间通信(消息队列/信号量+共享内存)
    bugku 输入密码查看flag
    bugku 头等舱
    bugku 你必须让他停下
  • 原文地址:https://www.cnblogs.com/laoxia/p/11553264.html
Copyright © 2020-2023  润新知