分布式服务框架Dubbo总结-0x01 简介

Dubbo是什么


Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。。

为什么要用Dubbo


当网站变大,业务量飞速增长,需要对应用进行拆分,服务化,以提高项目的可维护性以及开发效率。但是当服务的数量多到一定程度时,服务之间的依赖,地址的配置与管理就变得复杂。服务之间的均衡负载问题也显得尤为突出,这种时候,正需要dubbo来解决

dubbo常见配置解析

官方配置参考手册

要点

Paste_Image.png
  • Provider: 暴露服务的服务提供方。
  • Consumer: 调用远程服务的服务消费方。
  • Registry: 服务注册与发现的注册中心。

    注册中心官方推荐采用zooKeeper,各种注册中心策略点这里

  • Monitor: 统计服务的调用次调和调用时间的监控中心。
  • Container: 服务运行容器。

调用关系说明:

  1. 服务容器负责启动,加载,运行服务提供者。
  2. 服务提供者在启动时,向注册中心注册自己提供的服务。
  3. 服务消费者在启动时,向注册中心订阅自己所需的服务。
  4. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。
  5. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。
  6. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

代码

Demo点我下载

Paste_Image.png

一共分为3大块

  • 服务提供者API接口
  • 服务提供端
  • 服务消费端

服务提供者API接口

IProviderAPI.java

public interface IProviderAPI {
     String dosomething(String json,long logId);
}

服务消费者

Consumer.java

@Service
public class Consumer {
  private Long count=0L;
  @Autowired
  private IProviderAPI providerAPI;

//  @Scheduled(cron = "0-59 * * * * ?")//定时
@Scheduled(fixedDelay = 1000 * 5 , initialDelay = 1000)//心跳更新,5000毫秒执行一次,延迟1秒后才执行
  public void doSomething(){
      System.out.println(providerAPI.dosomething("now is "+count,count++));
  }
}

消费者Runner(用于启动spring容器,加载dubbo,spring配置)

ConsumerRunner.java

public class ConsumerRunner {
    public static void main(String[] args) throws IOException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                new String[]{"consumer.xml", "spring.xml", "dubbo.xml"});
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

consumer.xml配置

<?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-3.0.xsd
         http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
">
    <dubbo:reference id="providerImpl" check="false" timeout="25000" retries="0"
                     interface="com.m1.api.IProviderAPI" />
   
</beans>


服务提供者

ProviderImpl.java

@Service("providerImpl")
public class ProviderImpl implements IProviderAPI {
    public String dosomething(String json, long logId) {
        System.out.println(json);
        return "i finish doing "+json +" where logid=="+logId;
    }
}

提供者Runner(用于启动spring容器,加载dubbo,spring配置)

ProviderRunner.java

public class ProviderRunner {
    public static void main(String[] args) throws IOException {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                new String[] {
                        "provider.xml",
                        "spring.xml",
                        "dubbo.xml" });
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

provider.xml配置

<?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:service interface="com.m1.api.IProviderAPI" ref="providerImpl" />

</beans>

dubbo.xml配置

<?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-3.0.xsd
         http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">
    
    <dubbo:application name="dubbo-test"/>
    <dubbo:registry address="zookeeper://yourIP:yourPort" />
    <dubbo:protocol name="dubbo" port="21101" />
</beans>

spring.xml配置

<?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:task="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 http://www.springframework.org/schema/task/spring-task.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com.m1" /><!--自动扫描-->

    <!--开启这个配置 启用定时 -->
    <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>
    <task:scheduler id="qbScheduler" pool-size="10"/>
</beans>

先运行ProviderRunner.java,后运行ConsumerRunner.java,每隔5秒钟可以看到控制台有不同的输出,从zookeeper运行日志,也能发现访问的ip

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,095评论 19 139
  • 这个方法存在一个弊端,就是当表视图存在导航条的时候,表视图会被导航条遮盖。所以需要添加如下代码
    静花寒阅读 5,371评论 3 7
  • 没有情绪 还是思维太丰富 低沉的耳鸣 是还在沉睡 偶尔 的清醒了 发现了生命的真谛 但为什么 一切都是梦幻一般 又...
    冰辛阅读 2,920评论 1 0

友情链接更多精彩内容