Dubbo+zookeeper搭配分布式服务的过程详解


Posted in Java/Android onApril 03, 2022

分布式架构: 

1.当垂直应用越来越多,应用之间交互不可避免,将核心业务抽取出来,作为独立的服务,逐渐形成稳定的服务中心,前端应用能更快速的响应多变的市场需求。 
2.此时,用于提高业务复用及整合的 分布式服务框架(RPC) 是关键。

Dubbo 是什么

  • 一款分布式服务框架
  • 高性能和透明化的RPC远程服务调用方案
  • SOA服务治理方案

Dubbo:

作为分布式架构比较后的框架,同时也是比较容易入手的框架,适合作为分布式的入手框架,下面是简单的搭建过程

工具:idea+tomact+zookeeper (知识点:jsp,spring,springmvc,maven)

思想:

Dubbo+zookeeper搭配分布式服务的过程详解

 

依赖:

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <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>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.atchengdu</groupId>
            <artifactId>001-interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

工程分布:

Dubbo+zookeeper搭配分布式服务的过程详解

  

provider实现interface提供服务,constomer消费provider提供的服务

interface:

Dubbo+zookeeper搭配分布式服务的过程详解

package com.atchengdu.serviceinterface;
 
import com.atchengdu.pojo.User;
public interface Userservice {
    //获取user的信息
    User getuserByid(Integer ie);
}
package com.atchengdu.pojo;
import java.io.Serializable;
public class User implements Serializable {
    private Integer id ;
    private String name;
    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }
    public User() {
    public Integer getId() {
        return id;
    public void setId(Integer id) {
    public String getName() {
        return name;
    public void setName(String name) {
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';

 provider:

Dubbo+zookeeper搭配分布式服务的过程详解

package com.atchengdu.Modulserviceimpl;
 
import com.atchengdu.pojo.User;
import com.atchengdu.serviceinterface.Userservice;
public class Userserviceimpl implements Userservice {
    @Override
    public User getuserByid(Integer ie) {
        User user=new User(1,"张三");
        return user;
    }
}
<?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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--声明名称-->
    <dubbo:application name="002-provider"></dubbo:application>
    <!--设置协议和端口号-->
    <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
    <!--使用注册中心-->
    <dubbo:registry address="zookeeper://localhost:2181"></dubbo:registry>
    <!--暴露服务接口-->
    <dubbo:service interface="com.atchengdu.serviceinterface.Userservice" ref="userserviceimpl"></dubbo:service>
    <!--加载业务实实现了-->
    <bean id="userserviceimpl" class="com.atchengdu.Modulserviceimpl.Userserviceimpl"></bean>
</beans>

constomer:

Dubbo+zookeeper搭配分布式服务的过程详解

package com.atchengdu.webcontroller;
 
import com.atchengdu.pojo.User;
import com.atchengdu.serviceinterface.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class Usercontroller {
    @Autowired
    private Userservice userservice;
    @RequestMapping("/user")
    public  String user(Model model,Integer id ){
        User user = userservice.getuserByid(id);
        model.addAttribute("user",user);
        return "user";
    }
}
<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/task"
       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://www.springframework.org/schema/task https://www.springframework.org/schema/task/spring-task.xsd
                           http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
                           http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.atchengdu.webcontroller">
</context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"></property>
        <property name="prefix" value="/"></property>
     </bean>
</beans>
<?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://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
      <dubbo:application name="003-constomer"></dubbo:application>
    <dubbo:registry address="zookeeper://localhost:2181"></dubbo:registry>
    <dubbo:reference id="userservice" interface="com.atchengdu.serviceinterface.Userservice"></dubbo:reference>
</beans>

到此这篇关于Dubbo+zookeeper搭配分布式服务的过程详解的文章就介绍到这了,更多相关Dubbo+zookeeper分布式服务内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Java/Android 相关文章推荐
详解Java实现数据结构之并查集
Jun 23 Java/Android
详解Java线程池是如何重复利用空闲线程的
Jun 26 Java/Android
Spring Boot 整合 Apache Dubbo的示例代码
Jul 04 Java/Android
浅谈spring boot使用thymeleaf版本的问题
Aug 04 Java/Android
spring cloud 配置中心native配置方式
Sep 25 Java/Android
剑指Offer之Java算法习题精讲二叉树专项训练
Mar 21 Java/Android
零基础学java之循环语句的使用
Apr 10 Java/Android
Android使用EventBus发送消息,Fragment中接收消息的方法会执行多次
Apr 24 Java/Android
Java Spring Lifecycle的使用
May 06 Java/Android
Java实现带图形界面的聊天程序
Jun 10 Java/Android
Java代码规范与质量检测插件SonarLint的使用
Aug 05 Java/Android
SpringBoot整合minio快速入门教程(代码示例)
Apr 03 #Java/Android
SpringBoot整合Minio文件存储
Apr 03 #Java/Android
Java中Quartz高可用定时任务快速入门
Apr 03 #Java/Android
Spring Security使用单点登录的权限功能
Spring Boot 底层原理基础深度解析
Java 超详细讲解数据结构中的堆的应用
Java 数据结构七大排序使用分析
You might like
第四节 构造函数和析构函数 [4]
2006/10/09 PHP
php去除重复字的实现代码
2011/09/16 PHP
php 生成签名及验证签名详解
2016/10/26 PHP
PHP正则表达式函数preg_replace用法实例分析
2020/06/04 PHP
JS 容错处理代码, 屏蔽错误信息
2021/03/09 Javascript
延时重复执行函数 lLoopRun.js
2007/05/08 Javascript
javascript页面动态显示时间变化示例代码
2013/12/18 Javascript
JavaScript实现简单图片滚动附源码下载
2014/06/17 Javascript
javascript制作的cookie封装及使用指南
2015/01/02 Javascript
JavaScript编程中window的location与history对象详解
2015/10/26 Javascript
vue响应式系统之observe、watcher、dep的源码解析
2019/04/09 Javascript
js计算两个时间差 天 时 分 秒 毫秒的代码
2019/05/21 Javascript
[02:45]DOTA2英雄敌法师基础教程
2013/11/25 DOTA
[29:23]2014 DOTA2国际邀请赛中国区预选赛 LGD-GAMING VS CIS 第一场1
2014/05/23 DOTA
python实现的解析crontab配置文件代码
2014/06/30 Python
在Python编程过程中用单元测试法调试代码的介绍
2015/04/02 Python
Python中的函数式编程:不可变的数据结构
2018/10/08 Python
python爬虫 urllib模块发起post请求过程解析
2019/08/20 Python
MNIST数据集转化为二维图片的实现示例
2020/01/10 Python
dpn网络的pytorch实现方式
2020/01/14 Python
Django重设Admin密码过程解析
2020/02/10 Python
Python开发之pip安装及使用方法详解
2020/02/21 Python
keras实现多GPU或指定GPU的使用介绍
2020/06/17 Python
Manjaro、pip、conda更换国内源的方法
2020/11/17 Python
欧洲第一中国智能手机和平板电脑网上商店:CECT-SHOP
2018/01/08 全球购物
乌克兰电子和家用电器商店:Foxtrot
2019/07/23 全球购物
学习十八届三中全会精神实施方案
2014/02/17 职场文书
尊老爱亲美德少年事迹材料
2014/08/14 职场文书
2014年置业顾问工作总结
2014/11/17 职场文书
公司股份合作协议书
2014/12/07 职场文书
车间质检员岗位职责
2015/04/08 职场文书
教学督导岗位职责
2015/04/10 职场文书
六一儿童节新闻稿
2015/07/17 职场文书
MySQL令人咋舌的隐式转换
2021/04/05 MySQL
Python-OpenCV教程之图像的位运算详解
2021/06/21 Python
SQL Server2019数据库备份与还原脚本,数据库可批量备份
2021/11/20 SQL Server