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

2017-06-06  本文已影响0人  量产型炮灰工程师

Dubbo是什么


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

为什么要用Dubbo


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

dubbo常见配置解析

官方配置参考手册

要点

Paste_Image.png

调用关系说明:

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

代码

Demo点我下载

Paste_Image.png

一共分为3大块

服务提供者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

上一篇 下一篇

猜你喜欢

热点阅读