原创

Dubbo学习笔记


在这里插入图片描述

zookeeper 注册中心

使用

​ 在服务提供者和服务消费者加入 zookeeper 客户端 依赖

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.3.3</version>
</dependency>

dubbo

  1. 导入dubbo依赖和操作zookeeper的客户端
<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
dubbo依赖
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
操作zookeeper的客户端
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.2.0</version>
</dependency>
  1. 配置服务提供者

引入依赖

<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
dubbo依赖
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
操作zookeeper的客户端
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.2.0</version>
</dependency>

provider.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:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
 
    <!-- 1.指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名) -->
    <dubbo:application name="名字"  />
 
    <!-- 2.指定注册中心的地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <!-- 3.指定通信规则(通信使用的协议、端口) -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 4.暴露哪个服务  ref指向服务的真正的实现对象 -->
    <dubbo:service interface="比如 com.ityang.service.UserService" ref="UserServiceImpl" />
 
    <!-- 服务的实现 -->
    <bean id="UserServiceImpl" class="比如 com.ityang.service.impl.UserServiceImpl" />
</beans>

使用ioc加载我们的配置文件provider.xml

public class Provider {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read(); // 按任意键退出
    }
}
  1. 配置服务消费者

首先引入依赖

<!-- https://mvnrepository.com/artifact/org.apache.dubbo/dubbo -->
dubbo依赖
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.7.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-framework -->
操作zookeeper的客户端
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-framework</artifactId>
    <version>4.2.0</version>
</dependency>

consumer.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:dubbo="http://dubbo.apache.org/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!-- 添加包扫描 -->
    <context:componet-scan base-package="com.ityang.service.impl"></context:componet-scan>
    
    <!-- 1.首先写上应用的名字 -->
    <dubbo:application name="名称"  />
 
    <!-- 2.指定注册中心的地址 -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <!-- 3.声明需要调用的远程服务接口 -->
    <dubbo:reference id="UserService" interface="比如说 这是提供者暴露的 com.ityang.service.UserService" />
</beans>

测试:

public class Consumer {
    public static void main(String[] args) throws Exception {
       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        DemoService demoService = (DemoService)context.getBean("UserService"); // 获取远程服务代理
        User user = demoService.sayHello("比如说 findById"); // 执行远程方法
        System.out.println( hello ); // 显示调用结果
    }
}

Dubbo和SpringBoot整合

  1. 导入dubbo的依赖

  2. 服务提供者导入dubbo的其他依赖

    <!-- https://mvnrepository.com/artifact/com.alibaba.boot/dubbo-spring-boot-starter -->
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>
    

    spring-boot只导入这一个依赖就行了

  3. 服务提供者的 application.properties中配置

    dubbo.application.name="应用的名字"
    #要连向注册中心地址
    dubbo.registry.address="127.0.0.1:2181"  
    #协议
    dubbo.registry.protocol=zookepper
    #通信协议
    dubbo.protocol.name=dubbo
    #端口
    dubbo.propocol.port=20880
    #连接监控中心
    dubbo.monitor.protocol=registry
    
  4. 服务提供者需要暴露的服务上添加注解

    @Service dubbo下的注解 作用是暴露服务

    @Service spring中的

    两个都添加

  5. 服务提供者主程序中开启基于注解的dubbo功能

    @EnableDubbo  //在主程序中开启基于注解的dubbo功能
    @SpringBootApplication
    public class dubboProviderTest{
        public static void main(String[] agrs){
            SpringApplication.run(dubboProviderTest.class,agrs);
        }
    }
    
  6. 服务消费者导入依赖

    <!-- https://mvnrepository.com/artifact/com.alibaba.boot/dubbo-spring-boot-starter -->
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>
    
  7. 服务消费者的 application.properties中配置

    dubbo.application.name="应用的名字"
    #要连向注册中心地址
    dubbo.registry.address="127.0.0.1:2181" 
    #连接监控中心
    dubbo.monitor.protocol=registry
    
  8. 服务消费者使用提供者的服务

    @Reference  //帮我们远程引入服务
    UserService userService;
    
  9. 服务消费者主启动类

    @EnableDubbo  //在主程序中开启基于注解的dubbo功能
    @SpringBootApplication
    public class dubboConsumerTest{
        public static void main(String[] agrs){
            SpringApplication.run(dubboConsumerTest.class,agrs);
        }
    }
    
Java
spring

评论