Dubbo学习之简单环境搭建

 

Dubbo服务的发展和作用:

  •   首先,看下一般网站架构随着业务的发展,逻辑越来越复杂,数据量越来越大,交互越来越多之后的常规方案演进历程。

      

  •   其次,当服务越来越多之后,我们需要做哪些服务治理?

       

  •   最后,是dubbo的架构图

       
  注册中心的选择


  dubbo支持多种类型的注册中心:

  •   Multicast注册中心
  •   Zookeeper注册中心
  •   Redis注册中心
  •   Simple注册中心

 

dubbo的简单环境搭建

1、安装zookeeper

  zookeeper下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/,下载完成解压即可运行。注意,zookeeper目录下的conf文件夹中并没有zookeeper的配置文件,不过有一个zoo_sample.cfg文件,将其命令为zoo.cfg即可使用默认配置。

  点击zkServer.cmd启动zookeeper:

 

2、安装dubbo-admin.war

  安装dubbo-admin.war这一步是非必需的,不过使用dubbo-admin可以方便查看dubbo的运行状态和数据,便于管理。下载完成后放入到tomcat的webapps目录下,启动tomcat访问dubbo即可,登录之后就可以看到如下页面(登录用户名和密码在dubbo.properties文件中查看):

 

3、dubbo-server端配置

  新建dubbo-server工程,下载所需的jar包,编写提供服务的接口和实现类,然后通过zookeeper注册中心暴露服务。

(3.1) 在maven文件中添加所需jar包的依赖:

<!-- dubbo -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.2</version>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.6</version>
</dependency>
<!--zkclient-->
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>

<!-- log relation -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

<!-- spring relation -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>

(3.2) 工程spring文件(applicationContext.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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">

    <bean id="demoService" class="com.luoxn28.dubbo.impl.DemoServiceImpl"/>

    <!-- 提供方应用消息 -->
    <dubbo:application name="demo-provider"/>

    <!-- 使用zookeeper注册中心暴露服务 -->
    <dubbo:registry address="zookeeper://192.168.1.100:2181" />
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 对外服务 -->
    <dubbo:service interface="com.luoxn28.dubbo.DemoService" ref="demoService"/>

</beans>

(3.3) 对外服务接口及其实现类:

/**
 * DemoService
 */
public interface DemoService {
    String sayHello(String name);
}

/**
 * DemoServiceImpl
 */
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hi " + name + ", I am dubbo service";
    }
}

(3.4) 启动类:

/**
 * ServiceStart
 */
public class ServiceStart {
    public static void main(String[] args) {
        // 加载applicationContext.xml
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        System.out.println("start dubbo");
        while (true) {
            Thread.yield();
        }
    }
}

整个dubbo-server工程结构如下:

 

4、dubbo-client端配置

  新建dubbo-client工程,下载所需的jar包,编写获取服务的接口,然后通过zookeeper注册中心暴露的服务来获取。

(4.1) 在maven文件中添加所需jar包的依赖:

<!-- dubbo -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.5.2</version>
</dependency>
<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.4.6</version>
</dependency>
<!--zkclient-->
<dependency>
    <groupId>com.github.sgroschupf</groupId>
    <artifactId>zkclient</artifactId>
    <version>0.1</version>
</dependency>

<!-- log relation -->
<dependency>
    <groupId>commons-logging</groupId>
    <artifactId>commons-logging</artifactId>
    <version>1.2</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

<!-- spring relation -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-beans</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>

(4.2) 工程spring文件(applicationContext.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: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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">

    <!-- 提供方应用消息 用于计算依赖关系 -->
    <dubbo:application name="demo-client"/>
    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <dubbo:registry address="zookeeper://192.168.1.100:2181" />

    <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService -->
    <dubbo:reference id="demoService"
                     interface="com.luoxn28.dubbo.DemoService" check="false"/>
</beans>

(4.3) 待获取服务的接口:

public interface DemoService {
    String sayHello(String name);
}

(4.4) 启动类

/**
 * ClientStart
 */
public class ClientStart {
    public static void main(String[] args) {
        System.out.println("hello dubbo");

        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        DemoService service = (DemoService) context.getBean("demoService");

        String response = service.sayHello("luoxn28");
        System.out.println(response);
    }
}

  整个dubbo-server工程结构如下:

 

  运行ClientStart类之后,输出如下:

 

注意:

  首先运行zookeeper,然后启动dubbo-server程序,最后运行dubbo-client程序。dubbo-client中的服务接口路径需要和dubbo-server的一致,也就是说一个类在dubbo-server中的url和在dubbo-client中的url要一样,否则运行会出现Forbid consumer异常。

 

参考:

  1、windows下 zookeeper dubbo 安装+配置+demo 详细图文教程

  2、从头开始搭建一个dubbo+zookeeper平台

  3、dubbo-demo示例代码

posted @ 2017-01-02 19:55  luoxn28  阅读(8339)  评论(0编辑  收藏  举报