2.Spring&SpringBoot整合Dubbo


目录
  • Dubbo
  • Dubbo实现RPC原理
  • Spring整合Dubbo
    • 创建Maven父工程
      • 创建接口项目
    • 创建服务提供项目(生产者)
      • POM
      • XML
      • 接口实现方法
      • 生产者启动
    • 创建服务调用项目(消费者)
      • POM
      • XML
      • 消费者启动
    • Spring整合代码下载
  • SpringBoot整合Dubbo
    • 创建服务提供项目(生产者)
      • POM
      • YML
      • 接口实现方法
      • 开启Dubbo支持
      • 启动服务
    • 创建服务调用项目(消费者)
      • POM
      • YML
      • 开启Dubbo支持
      • 发起请求
    • SpringBoot整合代码下载

Dubbo

Apache Dubbo 是?款?性能、轻量级的开源服务框架。

Apache Dubbo 提供了六?核?能?:?向接?代理的?性能RPC调?,智能容错 和负载均衡,服务?动注册和发现,?度可扩展能?,运?期流量调度,可视化的服务治理 与运维

Dubbo实现RPC原理

服务消费者去注册中?订阅到服务提供者的信息。然后通过dubbo进?远程调?。

相当于dubbo自定义了通信方式以及方法的代理,由实现的代理、注册等代码都交给了Dubbo

image-20220402212441683

Spring整合Dubbo

首先启动Zookeeper,如何启动见此处

创建Maven父工程

创建接口项目

package com.rb.myInterface;

/**
 * 定义一个功能接口
 */
public interface HelloWordService {
    String helloWord(String parameter);
}

创建服务提供项目(生产者)

POM

<?xml version="1.0" encoding="UTF-8"?>

    
        dubbo-by-xml-demo
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    my-producer
    
        
            org.slf4j
            slf4j-api
            1.8.0-alpha2
        
        
        
            org.apache.dubbo
            dubbo
            2.7.3
        
        
        
            org.apache.curator
            curator-framework
            5.2.1
        
        
        
            org.apache.curator
            curator-recipes
            5.2.1
        
        
            org.example
            my-interface
            1.0-SNAPSHOT
        
    


XML

producer.xml

<?xml version="1.0" encoding="UTF-8"?>

    
    
    
    
    
    
    
    
    
    

接口实现方法

package com.rb.impl;

import com.rb.myInterface.HelloWordService;

/**
 * 对接口的实现
 */
public class HelloWordImpl implements HelloWordService {
    public String helloWord(String s) {
        return "HelloWordImpl:"+s;
    }
}

生产者启动

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class ProducerRun {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"producer.xml"});
        context.start();
        System.in.read();
    }
}

创建服务调用项目(消费者)

POM

<?xml version="1.0" encoding="UTF-8"?>

    
        dubbo-by-xml-demo
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    my-customer
    
        
            org.slf4j
            slf4j-api
            1.8.0-alpha2
        
        
        
            org.apache.dubbo
            dubbo
            2.7.3
        
        
        
            org.apache.curator
            curator-framework
            5.2.1
        
        
        
            org.apache.curator
            curator-recipes
            5.2.1
        
        
            org.example
            my-interface
            1.0-SNAPSHOT
        

    


XML

<?xml version="1.0" encoding="UTF-8"?>


    

    
    
    

消费者启动

import com.rb.myInterface.HelloWordService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CustomerRun {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"customer.xml"});
        context.start();
        //RPC 开始
        //获取?个代理,(类似手写RPC框架中ProxyFactory.java,只不过代理交给Dubbo了)的代理服务提供者内提供的bean
        HelloWordService helloWordService = (HelloWordService)context.getBean("helloWordService");
        //调?代理对象的getName?法。通过代理对象调到服务提供者内的bean
        String result = helloWordService.helloWord("rb");
        System.out.println(result);
        //RPC 结束
    }
}

Spring整合代码下载

下载

SpringBoot整合Dubbo

创建服务提供项目(生产者)

POM


        
            org.example
            rpc-interface
            1.0-SNAPSHOT
        
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            3.0.6
        

        
        
            org.apache.dubbo
            dubbo-registry-zookeeper
            3.0.6
        

YML

dubbo:
  application:
    name: hello-word-service #服务名称
  registry:
    address: zookeeper://192.168.0.104:2181,192.168.0.104:2182,192.168.0.104:2183,192.168.0.104:2184 #zk地址
  protocol:
    port: 20881 #配置当前这个服务在dubbo容器中的端?号,每个dubbo容器内部的服务的端?号唯一

接口实现方法

添加@DubboService

package com.rb.producer.impl;


import myinterface.HelloWord;
import org.apache.dubbo.config.annotation.DubboService;

/**
 * 对接口的实现
 */
//添加Dubbo注解
@DubboService
public class HelloWordImpl implements HelloWord {
    public String helloWord(String s) {
        return "HelloWordImpl:"+s;
    }
}

开启Dubbo支持

添加@EnableDubbo

package com.rb.producer;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class ProducerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProducerApplication.class, args);
    }

}

启动服务

创建服务调用项目(消费者)

POM

 
        
            org.example
            rpc-interface
            1.0-SNAPSHOT
        
        
        
            org.apache.dubbo
            dubbo-spring-boot-starter
            3.0.6
        

        
        
            org.apache.dubbo
            dubbo-registry-zookeeper
            3.0.6
        

YML

server:
  port: 8081

dubbo:
  application:
    name: hello-word-consumer
  registry:
    address: zookeeper://192.168.0.104:2181,192.168.0.104:2182,192.168.0.104:2183,192.168.0.104:2184 #zk地址

开启Dubbo支持

package com.rb.customer;

import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class CustomerApplication {

    public static void main(String[] args) {
        SpringApplication.run(CustomerApplication.class, args);
    }

}

发起请求

@DubboReference

  • 服务启动去zk中订阅提供HelloWord服务的URL
  • 将IOC中的代理对象注入
package com.rb.customer;

import com.alibaba.dubbo.config.annotation.Reference;
import myinterface.HelloWord;
import org.apache.dubbo.config.annotation.DubboReference;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class CustomerApplicationTests {
    //通过Dubbo引入代理对象
    @DubboReference
    private HelloWord helloWord;

    @Test
    void contextLoads() {
        //执行代理方法
        System.out.println(helloWord.helloWord("RB"));
    }

}

SpringBoot整合代码下载

下载