一分钟入门Dubbo

Dubbo是什么?能做什么?

Dubbo是一个分布式服务框架,以及SOA治理方案。其功能主要包括:高性能NIO通讯及多协议集成,服务动态寻址与路由,软负载均衡与容错,依赖分析与降级等。

Dubbo适用于哪些场景?

当网站变大后,不可避免的需要拆分应用进行服务化,以提高开发效率,调优性能,节省关键竞争资源等。

当服务越来越多时,服务的URL地址信息就会爆炸式增长,配置管理变得非常困难,硬件负载均衡器的单点压力也越来越大。

当进一步发展,服务间依赖关系变得错综复杂,甚至分不清哪个应用要在哪个应用之前启动,架构师都不能完整的描述应用的架构关系。

接着,服务的调用量越来越大,服务的容量问题就暴露出来,这个服务需要多少机器支撑?什么时候该加机器?等等

在遇到这些问题时,都可以用Dubbo来解决。
可参见以下链接http://dubbo.apache.org/books/dubbo-user-book/preface/background.html

Dubbo性能如何?

Dubbo通过长连接减少握手,通过NIO及线程池在单连接上并发包处理消息,通过二进制流压缩数据,比常规HTTP等短连接协议更快,在阿里巴巴内部,每天支撑2000多个服务,30多亿访问量,最大单机支撑每天近1亿访问量。

可参见以下链接http://dubbo.apache.org/books/dubbo-user-book/perf-test.html

Dubbo架构

节点角色说明

节点 角色说明
Provider 暴露服务的服务提供方
Consumer 调用远程服务的服务消费方
Registry 服务注册与发现的注册中心
Monitor 统计服务的调用次数和调用时间的监控中心
Container 服务运行容器


调用关系说明

1.服务容器负责启动、加载、运行服务提供者。

2.服务提供者在启动时,向注册中心注册自己提供的服务

3.服务消费者在启动时,向注册中心订阅自己所需的服务。

4.注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送给消费者。

5.服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

6.服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。

快速入门案例

提供者(Provider)服务代码

新建maven项目引入相关jar

pom.xml 引入spring 和dubbo相关jar

 <properties>
    <spring.version>4.2.4.RELEASE</spring.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>${spring.version}</version>
    </dependency>
    <!--dubbo相关-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>dubbo</artifactId>
      <version>2.5.7</version>
    </dependency>
    <dependency>
      <groupId>org.apache.zookeeper</groupId>
      <artifactId>zookeeper</artifactId>
      <version>3.4.10</version>
    </dependency>
    <dependency>
      <groupId>com.github.sgroschupf</groupId>
      <artifactId>zkclient</artifactId>
      <version>0.1</version>
    </dependency>
    <dependency>
      <groupId>javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.11.0.GA</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <configuration>
          <port>8081</port>
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>

配置web.xml

   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext*.xml</param-value>
    </context-param>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

配置applicatinsContext.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-2.0.xsd
http://code.alibabatech.com/schema/dubbo  
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--dubbo应用中心 通常项目名-->
    <dubbo:application name="dubbo_service"/>
    <!--配置dubbo注册中心 这里zk注册中心,需要提前启动zk-->
    <dubbo:registry address="zookeeper://192.168.16.199:2181"/>
    <!--包扫描-->
    <dubbo:annotation package="com.muzidao.dubbo.service.impl"/>
</beans>

配置服务接口,这个接口需要提供给消费者,可以通过打成jar包或复制到消费者服务代码中

public interface HelloService {
    public String sysHello(String name);
}

配置实现接口类

import com.alibaba.dubbo.config.annotation.Service;
import com.muzidao.dubbo.service.HelloService;

/**
 * 服务实现类
 * @author: astali
 */
 //特别需要注意的地方,这个@Service不再是Spring,而是dubbo
@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sysHello(String name) {
        System.out.println("我是dubbo服务接口实现者。");
        return "Hello 木子道 ,欢迎关注" + name;
    }
}

配置消费者(Coosumer)服务代码

pom.xml跟提供者一样

配置web.xml

  <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:springmvc.xml</param-value>
        </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

配置springmvc.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://code.alibabatech.com/schema/dubbo 
 http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  <mvc:annotation-driven>
     <mvc:message-converters register-defaults="false">
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
             <constructor-arg value="UTF-8"/>
        </bean>
      </mvc:message-converters>
   </mvc:annotation-driven>

  <!--dubbo应用中心 通常项目名-->
  <dubbo:application name="dubbo_consumer"/>
  <!--配置dubbo注册中心  需提前启动zk-->
  <dubbo:registry address="zookeeper://192.168.16.199:2181"/>
  <!--包扫描 -->
  <dubbo:annotation package="com.muzidao.dubbo.controller"/>
</beans>

配置消费者消费代码

package com.muzidao.dubbo.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.muzidao.dubbo.service.HelloService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: astali
 * Created by Administrator on 2018/5/3 17:25
 */
@RestController
@RequestMapping("/dubbo")
public class HelloController {
    //需要注意这里是引用dubbo引入注解
    @Reference
    private HelloService helloService;  
    //这个HelloService其实就是提供者的那个接口,
    这里我这是把接口复制到这个项目中,其实是可以打成jar引入进来。

    @RequestMapping("/hello")
    public String hello(){
        System.out.println("--木子道--正在说 -- Hello" );
        return  helloService.sysHello("dubbo");
    }
}

现在配置基本完成,现在启动看看效果

相关阅读

Zookeeper 集群搭建

为什么使用ActiveMQ集群

玩转ActiveMQ集群

▼长按以下二维码即可关注▼

猜你喜欢

转载自my.oschina.net/u/3758413/blog/1807458