Spring整合Dubbo/Dubbo总结(二)

该篇博客手把手教你,如何使用Dubbo搭起来一个简单的分布式应用

关于配套课程

分享一下自己淘到的配套课程,尚硅谷Dubbo核心精讲实战

链接:https://pan.baidu.com/s/17wed_pfxxLcY-TyKmG5w3A   提取码:vvlb

关于zookeeper

zookeeper   win免安装版

链接:https://pan.baidu.com/s/1RngLtnt-nEl9361E84n03g   提取码:0est

使用方式,解压后进入bin目录,双击zkServer.cmd即可

 

环境搭建

一、控制台

链接:https://pan.baidu.com/s/1AXDTzRB_5jsds5nN8a_Lig   提取码:2w00

1、运行项目

记得先把本机的zookeeper启起来

控制台即dubbo-admin,链接解压后可以看到一个dubbo-admin的目录,

这个目录其实就是上一篇总结,所说的dubbo控制中心。dubbo-admin

这个目录本身就是一个SpringBoot项目,所以直接将它在IDEA中打开即可,然后运行DubboAdminApplication

(如果不知道如何打开本地springboot项目,请在文章下方留言或私信)

2、访问端口

dubbo-admin项目启动起来后,访问 localhost:7001

用户名密码,均为root

3、登录成功

登录成功后就可以看到这样一个界面,这就表示控制台搭建成功

二、整体架构

关于为什么要设计成API,生产者,消费者这样的模式,视频已经介绍的很清楚了,这里就只详细记录关于实现的部分

1、新建一个SpringBoot项目,artifactId为demo

具体操作步骤(https://blog.csdn.net/qq_44868502/article/details/104163353

2、搭建整体架构

如上,项目整体架构图

右击demo——>new——>module

后续的步骤和新建SpringBoot项目一致,

只需要把artifactId改为API,consumer,provider即可,这样项目整体架构就搭建起来了

三、API

1、API 图

pom文件不需要引入其他依赖,改动处只有红框部分

2、Student实体类

实体类要实现 Java序列化接口

public class Student implements Serializable {

    private String id;
    private String name;
    private Integer age;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3、StudentService接口

public interface StudentService {

    Student getStudentById(String id);

}

四、生产者

 

1、引入依赖

Pom文件中引入Dubbo和Zookeeper客户端依赖

 <!-- dubbo依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.6.2</version>
        </dependency>

        <!-- zookeeper客户端 -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>

引入API的依赖

<dependency>
            <groupId>com.example</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

2、StudentServiceImpl

public class StudentServiceImpl implements StudentService {
    
    /*
    * 1、将服务提供者注册到注册中心
    * 1.1导入dubbo的依赖(2.6.2)/操作zookeeper的客户端(curator)
    *1.2配置服务提供者
    * */

    @Override
    public Student getStudentById(String id) {

        Student student = new Student();
        student.setId(id);
        student.setName("小明");
        student.setAge(10);

        return student;
    }
}

3、provider.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       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="student-service-provider" />
    <!-- 2、指定注册中心位置-->
    <!--<dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>-->
     <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
     <!--3、指定通信规则(通信协议,通信端口)-->
     <dubbo:protocol name="dubbo" port="20880"/>
     <!--4、暴露服务,指向服务的真正实现对象-->
     <dubbo:service interface="com.example.api.service.StudentService" ref="studentServiceImpl"/>

     <!--服务的实现  -->
    <bean id="studentServiceImpl" class="com.example.provider.serviceImpl.StudentServiceImpl"></bean>
</beans>

4、MainApplication

public class MainApplication {
    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
        ioc.start();

        System.in.read();
    }
}

最后看一眼整体架构

五、消费者

1、引入依赖

跟生产者一致,不再赘述

2、StudentController

@Service
public class StudentController {

    @Resource
    StudentService studentService;

    public void getStudent(){
        Student student = studentService.getStudentById("1");
        System.out.println(student);
    }
}
 
3、consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.example.consumer01.controller"/>
    <!--1、指定当前服务的名字-->
    <dubbo:application name="student-consumer"/>
    
    <!--2、指定注册中心的位置-->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"/>
    
    <!--3、声明需要调用的远程服务接口:生成远程服务代理-->
    <dubbo:reference check="false" interface="com.example.api.service.StudentService" id="studentService"/>
    
</beans>
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==

4、ConsumerApplication

public class ConsumerApplication {

    public static void main(String[] args) throws IOException {

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
        context.start();
        StudentController bean = context.getBean(StudentController.class);
        
        bean.getStudent();
        
    }
}

未完待续✿✿ヽ(°▽°)ノ✿✿✿ヽ(°▽°)ノ✿✿✿ヽ(°▽°)ノ✿

发布了343 篇原创文章 · 获赞 151 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_44868502/article/details/104177111