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

SpringCloudAlibaba 部署 Dubbo

SpringCloudAlibaba 部署 Dubbo

Apache Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用;智能容错和负载均衡;以及服务自动注册和发现。

Dubbo是阿里巴巴开源的一款高性能的服务框架,使得应用可通过高性能的RPC实现服务的输出和输入功能,可以和Spring框架无缝集成。Dubbo框架是基于Spring容器运行的。
官方网站:https://dubbo.apache.org/

registry

注册中心:是用于发布和订阅服务的一个平台,用于替换SOA结构体系框架中的ESB服务总线的。

发布
开发服务端代码完毕后,将服务信息发布出去,实现一个服务的公开。
订阅
客户端程序,从服务端下载服务内容,这个过程是订阅。
订阅服务时,会将发布的服务所有信息,一次性下载到客户端。
客户端也可以自定义,修改部分服务配置信息。如:超长的时间,调用的重试次数等。

Consumer
服务端的消费者,就是服务的客户端。消费者必须使用Dubbo技术开发部分代码,基本上都是配置文件定义。
Provider
服务提供者。服务端使用Dubbo技术开发部分代码,以配置文件为主。
container
容器,Dubbo技术的服务端(Provider),在启动执行时,必须依赖Spring容器才能正常启动。
monitor(Dubbo Admin)
监控中心,是Dubbo提供的一个jar工程。
主要功能是监控服务端(Provider)和消费端(Consumer)的使用数据的。如服务端有多少个接口、多少方法、调用次数,压力信息等。客户端有多少,调用过哪些服务端,调用次数等。

执行流程
start 启动:启动Spring容器时,自动启动 Dubbo的 Provider

register 注册:Dubbo的Provider在启动后自动会去注册中心进行注册
注册内容包括:1.Provider的IP、端口、访问协议;2.对外提供的接口列表、方法、接口类;3.Dubbo的版本。

subscribe 订阅:当Consumer启动时,会自动去Registry注册中心获取已注册的服务的信息。

notify 通知:当Provider的信息发生变化时,自动由Registry向Consumer推送通知。

invoke 调用:Consumer调用Provider中的方法。同步请求:消耗一定性能。但是必须是同步请求,因为需要接收调用方法后的结果。

count 次数:每隔2分钟,provider和consumer自动向Monitor发送访问次数,Monitor进行统计。
协议
Dubbo协议
优点:底层采用Netty,采用NIO复用单一长连接,并使用线程池并发处理请求,减少握手和加大并发效率,性能较好。
缺点:上传大文件时,不建议使用Dubbo文件上传。

RMI(Remote Method Invocation)协议
优点:JDK自带的能力,可与原生RMI互操作,基于TCP协议。
缺点:偶尔连接失败。

Hessian 协议
优点:可与原生Hessian互操作,基于Http协议。
缺点:需hessian.jar支持,http短连接开销大。
构建服务接口

创建子模块alm-common,新增 NotifyService

package com.alm.cloud.common.service;

public interface NotifyService{
    void send();
}
构建服务接口提供方

创建子模块alm-common,新增 NotifyService

    provider
    com.alm.cloud.provider
    1.0-SNAPSHOT

    
        1.8
        UTF-8
    

    
        
            com.example.cloud3
            common
            1.0-SNAPSHOT
            compile
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            com.alibaba.cloud
            spring-cloud-starter-dubbo
            2021.1
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2021.1
        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
            2021.1
        
    
实现具体接口
package com.example.cloud3.provider.service;

import com.example.cloud3.common.service.SmsService;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class SmsServiceImpl implements SmsService {

    private int count = 0;
    @Override
    public void send(){
        System.out.println("[Provider] send a msg..."+ (++count));
    }
}



@EnableDiscoveryClient //此注解可以省略
@SpringBootApplication
public class Provider_8031_Application {
    public static void main(String[] args) {
        SpringApplication.run(Provider_8031_Application.class, args);
    }
}

Consumer
    
        
            com.example.cloud3
            common
            1.0-SNAPSHOT
            compile
        

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

        
            com.alibaba.cloud
            spring-cloud-starter-dubbo
            2021.1
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            2021.1
        

        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-config
            2021.1
        
    

server:
  port: 8021

spring:
  application:
    name: consumer-8021
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
      discovery:
        enabled: true
        register-enabled: true
        server-addr: 127.0.0.1:8848

dubbo:
  protocol:
    name: dubbo
    port: -1
  registry:
    address: "spring-cloud://localhost"
  cloud:
    subscribed-services: provider-8031
  consumer:
    check: false

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class Consumer_8012_Application {
    public static void main(String[] args) {
        SpringApplication.run(Consumer_8012_Application.class, args);
    }
}



import com.example.cloud3.common.service.SmsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SmsController {
    @DubboReference private SmsService smsService;
    @RequestMapping("/sendMsg")
    public String sendMsg(){
        smsService.send();
        System.out.println("[Consumer] send success!");
        return "success";
    }
}

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

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

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