栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Dubbo 的讲解

Dubbo 的讲解

前言

在dubbo之前,利用SSM/SpringBoot/SSH框架来写代码,
利用的机制(从上至下,controller-service-dao-db),
但承受的人数在几万人左右,如果人数过多,性能会变差,甚至崩溃。

如果人数过亿了,用户量及其大,更适合使用dubbo(分布式架构),里面用的是集群技术,里面有好多服务器,不同的服务器对应着不同的功能。有用来生产,有用来消费 。里面的就组成了对应的Consumer和Provuder。最外面还有个监管者Monitor(监视器)。还有一个传达命令的,就是(Registry)。

附图(官方):

Dubbo 【后端框架】 开源分布式服务框架:
消费者Consumer 提供者 Provider
监视器 monitor 注册中心 zookeeper
Container 服务运行容器 Registry注册中心

1.什么是Dubbo

Dubbo是阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能(通信协议 RPC),可以和Spring框架无缝集成。

Dubbo是一款高性能、轻量级的开源Java RPC框架,
它提供了三大核心能力:

  1. 面向接口的远程方法调用
  2. 智能容错和负载均衡
  3. 以及服务自动注册和发现

详细解析:

C端和p端 两个都是在服务器端的,只是分层

以前的是在一个工程里,相互之间可以依赖,
用maven管理,可以调来调去。
但现在都是单独的工程所以需要用RPC机制进行通信。
他是dubbo提供的 c 和p进行命令传递和相互调用,
用的就是注册中心机制。
zookeeper让c和p端产生关系,定义规则,
p端可以有好多个(集群) c端一般不会变。

举个例子:
1.先利用dubbo实现项目架构:
搞三个服务器,如消费者(c)、注册中心(zookeeper)、提供者(p)
2.假设生产某品牌牛奶:
先找到zookeeper进行配置,制定规则,外包装和盒子的规则,通过这个规则,厂商(p)再进行批量生产这个系列牛奶(一批的接口),再通过消费者(c) 通过zookeeper告诉要喝多少牛奶,然后把需求再发送给p端,根据p对应的需求进行生产,并发送给zookeeper端,再返回给消费者(c)。还有个Monitor端,该意思是监视的作用。有错误只是反馈,不会解决。

zookeeper类似管理者 也是通讯录

2.准备环境

两个物料

  1. apache-tomcat-7.0.94
  2. zookeeper-3.4.9
1.配置并启动zookeeper

(1). 解压zookeeper,修改conf下的配置文件名称为zoo.cfg

(2). 修改配置文件中的数据存储(日志)位置

  1. 先新建一个zookeeper-3.4.9repo
  2. 然后打开zoo.cfg,更改内容
    dataDir=D:xxxzookeeper-3.4.9repo之后直接运行

(dataDir=/tmp/xxx 默认都是linux的)存储日志的作用。

(3). 启动注册中心zkServer.cmd

[sh是linux启动方式 cmd是windows]

问题:
如果打开zookeeper windows闪退。

解决方法:
第一步 bin/zkEnv.cmd

第二步 zoo.cfg
dataDir=D:Axxxxzookeeper-3.4.9repo
路径用双斜杠

上面那个是必开的,如何运行工程的话,

下面的可开可不开。

第三步 然后再启动

2.配置Tomat(monitor搭建坏境)

1. 注意点:
1.monitor 没有可视化界面 ,
所以根据tomcat可视化界面访问monitor

2.每次编译都会重新更新 dubbo 文件
webapps / dubbo

3.tomcat里的dubbo.war 是自己加的

4.conf/server.xml 》改端口

  

2. 启动文件 startup.bat

上面的zookeeper也不能关,搭配用的。

问题:
tomcat启动不起来
解决 方法
bin/setclasspath.bat
set JAVA_HOME=D:Ajavajdk
set JRE_HOME=D:Ajavajre

改为你自己的对应路径

然后就可以了

3. 测试控制台

http://localhost:8099/dubbo/
账户 root 密码 root

说明:
上面页面用的不多 大部分是看代码
上面是monitor 很少用到 tomcat
其中zookeeper 必须启动 c和p相互串联起来。

3.创建工程及目录(分布式框架) 0.附图

1.创建工程(聚合工程)

第一步
创建maven工程,不用打勾,直接进去,并且把src删掉

第二步

  1. springboot工程(名字 consumer)》Java Web
  2. springboot工程(名字 provider)》Java Web
  3. maven工程> quickstart(名字 common)
    实体类 模板 不用web板块

第三步
点开

2.common(公有模块)

(0)pom.xml


  com.github.pagehelper
  pagehelper-spring-boot-starter
  1.4.1

(1)entity 实体类/City

import java.io.Serializable;
//序列化  让计算机能识别到 可序列化的类
public class City implements Serializable {
    private Integer id;
    private String cityName;

   //省略getter和setter方法
}

(2)service 定义标准/ CityService

制定规则

import cn.kgc.entity.City;
import com.github.pagehelper.PageInfo;

import java.util.List;

public interface CityService {
    List findAll();
	PageInfo findAll(Integer pageNo);
}
3.provider 提供者 生产接口

1.pom.xml


        
                    org.springframework.boot
                    spring-boot-starter-web
                

                
                    org.mybatis.spring.boot
                    mybatis-spring-boot-starter
                    2.1.3
                

                
                    mysql
                    mysql-connector-java
                    8.0.21
                
                
                    org.apache.dubbo
                    dubbo-spring-boot-starter
                    2.7.5
                
                
                
                    com.github.sgroschupf
                    zkclient
                    0.1

                

                
                    org.apache.curator
                    curator-framework
                    4.2.0
                    
                

        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.4.1
       
 


        
                    org.apache.zookeeper
                    zookeeper
                    3.4.9
                    
                        
                            slf4j-log4j12
                            org.slf4j
                        
                    
                

                
                    org.apache.curator
                    curator-recipes
                    4.2.0
                
                
                    org.springframework.boot
                    spring-boot-starter-test
                    test
                    
                        
                            org.junit.vintage
                            junit-vintage-engine
                        
                    
                
        
            cn.kgc
            common
            1.0-SNAPSHOT
            compile
        
    

2.application.yml

spring:
  datasource:
    url: jdbc:mysql:///db_dubbo?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&zeroDateTimeBehavior=CONVERT_TO_NULL
    username: root
    password: zjj
    driver-class-name: com.mysql.cj.jdbc.Driver
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
mybatis:
  configuration:
    call-setters-on-nulls: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mapper
//不能用spring的 @service 要用 apache的 不能造对象 @Component来造对象
@Service
@Component
@Transactional
public class CityServiceImpl implements CityService {
    @Resource
    private CityMapper cityMapper;

    @Override
    public List findAll() {
        return cityMapper.findAll();
    }

    @Override
    public PageInfo findAll(Integer pageNo) {
        PageHelper.startPage(pageNo,2);
        return new PageInfo<>(cityMapper.findAll());
    }
}

6.测试
http://localhost:8099/dubbo
服务治理 》 提供者

4.consumer 消费者

1.pom.xml


        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.1.3
        

        
        
            com.github.pagehelper
            pagehelper-spring-boot-starter
             1.4.1
        

        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.5
        
        
        
            com.github.sgroschupf
            zkclient
            0.1

        

        
            org.apache.curator
            curator-framework
            4.2.0
            
        

        
            org.apache.zookeeper
            zookeeper
            3.4.9
            
                
                    slf4j-log4j12
                    org.slf4j
                
            
        
        
            org.springframework.kafka
            spring-kafka
        

        
            org.apache.curator
            curator-recipes
            4.2.0
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            cn.kgc
            common
            1.0-SNAPSHOT
            compile
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

2.application.yml

dubbo:
  application:
    name: consumer
  registry:
    address: zookeeper://127.0.0.1:2181
mybatis:
  configuration:
    call-setters-on-nulls: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

上面是日志 调p端

server:
  port: 8081

3.cn.kgc.controller/CityController

import cn.kgc.entity.City;
import cn.kgc.service.CityService;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

 
@RestController
public class CityController {
    @Reference// rpc协议  c调p 必须用这个注解 其他注解不管用 特有的
    private CityService cityService;

    @RequestMapping("/findAll")
    public List findAll(){
        return cityService.findAll();
    }
     @RequestMapping("/findAllByPage")
     public PageInfo findAllByPage(@RequestParam(defaultValue = "1",required = false) Integer pageNo){
        return cityService.findAll(pageNo);
     }
}

4.static包/findAll.html

consumer/resources/static 里面放jsp

实现前后端分离,不用这个测试,用HBuilder X

5.启动类

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

说明:
消费者没有dao机制 但这个会调数据库 SpringBoot内置的mysql
exclude排除 DataSourceAutoConfiguration.class自动数据源匹配格局
》不用跳自带的mysql 跳第三方的 生产者的数据库

测试1:
http://localhost:8099/dubbo
服务治理 》消费者

测试2:
postman

页面只用c端

数据库信息:

http://localhost:8081/findAll

[
    {
        "id": 1,
        "cityName": "北京"
    },
    {
        "id": 2,
        "cityName": "上海"
    },
    {
        "id": 3,
        "cityName": "广州"
    },
    {
        "id": 4,
        "cityName": "深圳"
    }
]

测试三
HBuilderX

1.导js包

2.demo_dubbo/findAll.html



	
		
		
		    
		
	
	
		
编码 城市名称

3.并且要加注解 跨域问题

consumer/CityController

@CrossOrigin //跨域

4.测试
http://127.0.0.1:8848/demo_dubbo/findAll.html

注意
代码其实可以自动生成,
mybatis生成器
企业定制款的代码生成器
如entitydaoservicecontroller/html等等,
但是只能完成简单业务。

----2021.12.28&29&30&31

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/687935.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号