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

分布式服务框架——Dobbo

分布式服务框架——Dobbo

Dubbo

Dubbo 最早是 Alibaba 开源的分布式服务框架,它最大的特点是按照分层的方式来架构, 使用这种方式可以使各个层之间解耦合(或者最大限度地松耦合)。从服务模型的角度来看, Dubbo 采用的是一种非常简单的模型,要么是提供方提供服务,要么是消费方消费服务,所 以基于这一点可以抽象出服务提供方(Provider)和服务消费方(Consumer)两个角色。

服务架构角色

Container:服务运行框架,dubbo完全基于spring实现Provider:提供远程调用服务Registry:服务注册和发布中心Consumer:执行远程调用服务Monitor:统计服务调用次数和调用时间的监控中心 服务开启流程

    启动spring容器Container时会把Provider启动然后将Provider相关信息注册到Registry里Consumer从Registry中订阅Provider的信息Registry将Provider的信息发送给ConsumerConsumer根据Registry通知的信息调用Provider中的方法Consumer和Provider将调用次数信息异步发送给Monitor统计
Dubbo支持的协议 Dubbo协议

优点:采用 NIO 复用单一长连接,并使用线程池并发处理请求,减少握手和加大并发效率, 性能较好(推荐使用)缺点:大文件上传时,可能出现问题(不使用 Dubbo 文件上传) RMI(Remote Method Invocation)协议

优点:JDK自带的缺点:偶尔连接失败 Hessian协议

优点:可与原生Hessian互操作,基于HTTP协议
缺点:需hessian.jar支持,http短连接的开销大

Dubbo支持的注册中心 ZooKeeper

优点:支持分布式,周边产品多缺点:受限于ZooKeeper软件的稳定性 Multicast

优点:去中心化,不需单独安装软件缺点:Provider、Consumer、Registry不能跨机房(路由) Redis

优点:支持集群,性能高缺点:要求服务器时间同步,否则可能出现集群失败问题 Simple

优点:标准RPC服务,没有兼容问题缺点:不支持集群 Dubbo应用

应用结构

parent:逻辑Maven工程,在pom文件中引入需要的依赖api:继承parent工程,定义功能接口,以供provider实现,consumer调用provider:继承parent工程,实现api中的接口功能consumer:继承parent工程,调用api中的功能接口 创建Parent工程

pom文件



    4.0.0

    com.lanh
    parent
    pom
    1.0-SNAPSHOT
    
        api
        provider
        consumer
    

    
        5.2.5.RELEASE
        2.7.6
        4.2.0
        2.7.6
        3.1.0
        2.0
        1.2
    

    
        
            
                org.springframework
                spring-context
                ${spring.version}
            

            
                org.springframework
                spring-webmvc
                ${spring.version}
            

            
                org.apache.dubbo
                dubbo
                ${dubbo.version}
            

            
                org.apache.dubbo
                dubbo-registry-zookeeper
                ${registry-zookeeper.version}
            

            
                org.apache.curator
                curator-framework
                ${curator.version}
            

            
                javax.servlet
                javax.servlet-api
                ${servlet.version}
                provided
            

            
                javax.servlet.jsp
                jsp-api
                ${jsp.version}
                provided
            

            
                javax.servlet
                jstl
                ${jstl.version}
            
        
    

    
        
            
                
                    org.apache.tomcat.maven
                    tomcat7-maven-plugin
                    2.2
                
            
        
    


创建api项目

创建功能接口

public interface DemoDubboService {
    String showMsg(String msg);
}
创建provider项目

配置pom文件


     
         com.lanh
         api
         1.0-SNAPSHOT
     

     
         org.springframework
         spring-context
     

     
         org.apache.dubbo
         dubbo
     

     
         org.apache.dubbo
         dubbo-registry-zookeeper
     

     
         org.apache.curator
         curator-framework
     
 

注意:provider项目和consumer项目都要依赖api项目

实现接口

package com.lanh.dubbo.service.impl;

import com.lanh.dubbo.service.DemoDubboService;
import org.apache.dubbo.config.annotation.Service;


@Service
public class DemoDubboServiceImpl implements DemoDubboService {
    public String showMsg(String msg) {
        return "Helle Dubbo "+msg;
    }
}

需要注意的是,这里使用到的注解@Service不是spring中的注解,而是dubbo的注解

编写启动类

package com.lanh.dubbo;

import org.apache.dubbo.container.Main;


public class Start {
    public static void main(String[] args) {
        Main.main(args);
    }
}

配置文件xml




    
    

    
    

    
    





    
    


注意:

该配置文件必须放到resources/meta-INF/spring @Service public class DemoServiceImpl implements DemoService { @Reference private DemoDubboService demoDubboService; public String showInfo(String msg) { return this.demoDubboService.showMsg(msg); } }

注意:

这里使用到的@Service不同于provider,这个是spring框架的注解同时,这里进行注入时,因为这个不是本地的服务,使用的也不再是@Autowire注解,而是dubbo远程调用的@Reference注解

页面跳转

package com.lanh.web.controller;

import com.lanh.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class DemoController {

    @Autowired
    private DemoService demoService;

    @RequestMapping("/getMsg")
    public String getMsg(String str){
        return this.demoService.showInfo(str);
    }
}

配置文件
applicationContext-spring.xml



    
    
    
    
    
    

applicationContext-service.xml



    


springmvc.xml



    
    
    
        
        
    

web.xml




    
        contextConfigLocation
        classpath:spring/applicationContext-*.xml
    

    
        org.springframework.web.context.ContextLoaderListener
    

    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring/springmvc.xml
        
        1
    
    
        springmvc
        /
    

    
    
        encodeFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    
    
        encodeFilter
        

@Service
public class DemoDubboServiceImpl implements DemoDubboService {
    @Override
    public String showMsg(String str) {
        return "Hello Dubbo "+str;
    }
}

再说一次这个是@Service是dubbo的注解

创建启动类
springboot自带了启动类,不需要再使用dubbo的启动类

package com.lanh.springbootdubbo_provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootdubboProviderApplication {

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

}

配置application.yml

#指定服务的名称
dubbo:
  application:
    name: myProvider
  registry:
    address: 192.168.126.128:2181,192.168.126.128:2182,192.168.126.128:2183
    protocol: zookeeper
    timeout: 10000
  #配置服务所使用的协议
  protocol:
    name: dubbo
    port: 2002
  scan:
    base-packages: com.lanh.springbootdubbo_provider.service.impl

创建consumer项目

配置pom依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.0.RELEASE
         
    
    com.lanh
    springbootdubbo_consumer
    0.0.1-SNAPSHOT
    springbootdubbo_consumer
    Demo project for Spring Boot
    
        1.8
    
    
        
            com.lanh
            springbootdubbo_api
            1.0-SNAPSHOT
        

        
            ch.qos.logback
            logback-classic
            1.2.3
        

        
            org.apache.dubbo
            dubbo-spring-boot-starter
            2.7.6
        

        
            org.apache.dubbo
            dubbo-registry-zookeeper
            2.7.6
        

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

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



定义Service接口

package com.lanh.springbootdubbo_consumer.service;

public interface DemoService {
    String getMsg(String str);
}

实现Service接口

package com.lanh.springbootdubbo_consumer.service.impl;

import com.lanh.dubbo.service.DemoDubboService;
import com.lanh.springbootdubbo_consumer.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;



@Service
public class DemoServiceImpl implements DemoService {

    @Reference
    private DemoDubboService demoDubboService;

    @Override
    public String getMsg(String str) {
        return this.demoDubboService.showMsg(str);
    }
}

再说一次:这个@Service是spring中的注解,用@Reference而不用@Autowire

配置页面跳转逻辑

package com.lanh.springbootdubbo_consumer.controller;

import com.lanh.springbootdubbo_consumer.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



@RestController
public class DemoController {

    @Autowired
    private DemoService demoService;

    @RequestMapping("/getMsg")
    public String getMsg(String str){
        return this.demoService.getMsg(str);
    }
}

springboot的启动类

package com.lanh.springbootdubbo_consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootdubboConsumerApplication {

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

配置application.yml

#指定服务的名称
dubbo:
  application:
    name: myConsumer
  registry:
    address: 192.168.126.128:2181,192.168.126.128:2182,192.168.126.128:2183
    protocol: zookeeper
    timeout: 10000
  #配置服务所使用的协议
  protocol:
    name: dubbo

至此结束,欢迎各位小伙伴讨论

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

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

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