Dubbo入门之hello world(zookeeper做注册中心)

说明:注册中心可以用多种,项目中一般都用zookeeper 注册中心方式

demo1用的multicast广播注册中心方式

会了multicast广播注册中心方式,zookeeper就很简单了

demo2用的zookeeper注册中心方式

Zookeeper安装 https://blog.csdn.net/csdnluolei/article/details/83749859

Zookeeper管理端安装 https://blog.csdn.net/csdnluolei/article/details/84898637


首先要知道Dubbo是什么?用它干什么?用它有什么好处?我们一起来看看

Apache Dubbo 官方解释: http://dubbo.apache.org/en-us/

高性能Java RPC框架

Apache Dubbo(孵化)|dubbo| 是一个高性能,轻量级,基于Java的RPC框架。Dubbo提供三个关键功能,包括基于接口的远程调用容错和负载平衡,以及自动服务注册和发现

Dubbo的框架图:

五种角色说明:

Provider: 暴露服务的服务提供方。

Consumer: 调用远程服务的服务消费方。

Registry: 服务注册与发现的注册中心。

Monitor: 统计服务的调用次调和调用时间的监控中心。

Container: 服务运行容器。

 

关系说明:

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

  • 1.用maven构建项目使用spring和multicast广播注册中心方式实现   Dubbo入门之hello world

1.1生产者provider

pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.dubbo</groupId>

  <artifactId>dubbo-demo-provider</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>war</packaging>

 

    <properties>

       <spring.version>4.2.2.RELEASE</spring.version> 

    </properties>

 

      <!-- 引入依赖 -->

    <dependencies>

       <!-- 日志包 -->

       <dependency>

           <groupId>org.slf4j</groupId>

           <artifactId>slf4j-log4j12</artifactId>

           <version>1.6.4</version>

       </dependency>

      

       <!-- dobbo begin -->

       <dependency>

           <groupId>com.alibaba</groupId>

           <artifactId>dubbo</artifactId>

           <version>2.5.2</version>

           <exclusions>

              <exclusion>

                  <artifactId>spring</artifactId>

                  <groupId>org.springframework</groupId>

              </exclusion>

              <exclusion>

                  <artifactId>netty</artifactId>

                  <groupId>org.jboss.netty</groupId>

              </exclusion>

           </exclusions>

       </dependency>

      

       <!-- dubbo-client -->

       <dependency>

           <groupId>cygps.com</groupId>

           <artifactId>api-dubbo-client</artifactId>

           <version>3.0.0-SNAPSHOT</version>

       </dependency>    

       <!-- dobbo end -->

 

       <!-- spring begain -->

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-tx</artifactId>

           <version>${spring.version}</version>

       </dependency>

       <!-- spring-context -->

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-context</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-beans</artifactId>

           <version>${spring.version}</version>

       </dependency>

       <!-- spring end -->

    </dependencies>

 

</project>

创建一个接口

创建接口实现类

配置文件dubbo-provider.xml

注意:xml配置文件中会出现dubbo标签报错,原因是缺少dubbo.xsd约束文件,可以看我的另一篇博客点击即可跳转

<?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:application name="dubbo-provider" />

 

    <!-- 使用multicast广播注册中心暴漏服务地址-->

    <dubbo:registry address="multicast://224.5.6.7:1234" />

 

    <!-- 声明需要暴漏的服务接口 -->

    <dubbo:service interface="org.student.StudentService" ref="studentService" executes="10" />

 

    <bean id="studentService" class="org.student.impl.StudentServiceImpl" />

   

    <!-- dubbo协议在20880端口暴露服务 -->

    <dubbo:protocol name="dubbo" port="20880" />

 

    </beans>

log4j.properties

log4j.rootLogger=info, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

创建测试类TestProvider

package org.student.impl;

 

import java.io.IOException;

 

import org.springframework.context.support.ClassPathXmlApplicationContext;

 

public class TestProvider {

 

    public static void main(String[] args) throws IOException {

      

       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

              new String[] { "dubbo-provider.xml" });

         context.start();

         System.out.println("dubbo-provider 已开启。。。。。。");

         System.in.read();

 

    }

 

}

 

生产者provider目录结构

创建测试类TestProvider,控制台看到这些,无报错说明启动成功

 

1.2消费者consumer

配置文件dubbo-consumer.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

       xmlns="http://www.springframework.org/schema/beans"

       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:application name="dubbo-consumer" />

 

    <!-- 使用multicast广播注册中心暴漏发现服务地址 -->

    <dubbo:registry address="multicast://224.5.6.7:1234" />

   

    <dubbo:consumer timeout="5000" />

   

    <!-- 生成远程服务代理,可以和本地bean一样使用studentService -->

    <dubbo:reference id="studentService" interface="org.student.StudentService" check="false" />

 

</beans>

测试类TestConsumer

package org.controller;

 

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.student.StudentService;

 

public class TestConsumer {

 

    public static void main(String[] args) {

   

       sayHello();

    }

 

    public static void sayHello() {

      

       ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(

              new String[] { "dubbo-consumer.xml" });

       context.start();

       StudentService studentService = (StudentService) context.getBean("studentService");

 

       System.out.println(studentService.sayHello("world"));

      

    }

   

   

}

 

接口StudentService

 

消费者consumer目录结构

启动测试类TestConsumer

至此测试完成!有没有感觉Dubbo其实挺简单的。。。


  • 2.用maven构建项目使用spring和zookeeper注册中心方式实现   Dubbo入门之hello world

2.1生产者provider

pom文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>com.dubbo</groupId>

  <artifactId>dubbo-demo-provider</artifactId>

  <version>0.0.1-SNAPSHOT</version>

  <packaging>war</packaging>

 

    <properties>

       <spring.version>4.2.2.RELEASE</spring.version> 

    </properties>

 

      <!-- 引入依赖 -->

    <dependencies>

       <!-- 日志包 -->

       <dependency>

           <groupId>org.slf4j</groupId>

           <artifactId>slf4j-log4j12</artifactId>

           <version>1.6.4</version>

       </dependency>

      

       <!-- dobbo begin -->

       <dependency>

           <groupId>com.alibaba</groupId>

           <artifactId>dubbo</artifactId>

           <version>2.5.2</version>

           <exclusions>

              <exclusion>

                  <artifactId>spring</artifactId>

                  <groupId>org.springframework</groupId>

              </exclusion>

              <exclusion>

                  <artifactId>netty</artifactId>

                  <groupId>org.jboss.netty</groupId>

              </exclusion>

           </exclusions>

       </dependency>

      

       <!-- dubbo-client -->

       <dependency>

           <groupId>cygps.com</groupId>

           <artifactId>api-dubbo-client</artifactId>

           <version>3.0.0-SNAPSHOT</version>

       </dependency>    

       <!-- dobbo end -->

 

       <!-- spring begain -->

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-tx</artifactId>

           <version>${spring.version}</version>

       </dependency>

       <!-- spring-context -->

       <dependency>

           <groupId>org.springframework</groupId>

           <artifactId>spring-context</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-beans</artifactId>

           <version>${spring.version}</version>

       </dependency>

       <!-- spring end -->

    </dependencies>

 

<build>

        <plugins>

            <plugin>

                <groupId>org.apache.tomcat.maven</groupId>

               <artifactId>tomcat7-maven-plugin</artifactId>

                <version>2.2</version>

               <configuration>

                   <path>/</path>

                   <port>8080</port>

                    <!--

                    <url>/</url>

                    <server>tomcat</server>

                    <username>tomcat</username>

                    <password>tomcat</password>

                    <uriEncoding>UTF-8</uriEncoding>

                    -->

               </configuration>

            </plugin>

        </plugins>

    </build>

</project>

创建一个接口

创建接口实现类

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:dubbo="http://code.alibabatech.com/schema/dubbo"

    xmlns:context="http://www.springframework.org/schema/context"

    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-4.2.xsd

    http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

 

    <!--  提供方应用信息,用于计算依赖关系-->

    <dubbo:application name="dubbo-provider" />

   

    <!-- 使用zookeeper注册中心暴露服务地址 -->

    <dubbo:registry address="zookeeper://192.168.25.128:2181"
                    check="false" subscribe="false" register="true"></dubbo:registry>

                 

    <!-- dubbo协议在20880端口暴露服务 -->

    <dubbo:protocol name="dubbo" port="20880" />

   

    <!-- dubbo扫描包,rpc时使用 -->

    <dubbo:annotation package="org.student.impl"/>

    <!-- spring-->

    <context:annotation-config/>

    <context:component-scan base-package="org.student.impl" />

   

 

    </beans>

log4j.properties

log4j.rootLogger=info, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    version="2.5">

   

    <!-- 1.加载spring容器 -->

    <context-param>

       <param-name>contextConfigLocation</param-name>

       <param-value>classpath:applicationContext.xml</param-value>

    </context-param>

    <!-- 配置Spring上下文监听器 --> 

    <listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

    </listener>

 

   

    </web-app>

生产者provider目录结构

2.2消费者consumer

log4j.properties

log4j.rootLogger=info, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

配置文件springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

       xmlns="http://www.springframework.org/schema/beans"

       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

       http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd

       http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

 

    <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样-->

    <dubbo:application name="dubbo-consumer" />

 

     <!-- 使用zookeeper注册中心暴露服务地址 -->

     <dubbo:registry address="zookeeper://192.168.25.128:2181" ></dubbo:registry> 

 

    <dubbo:consumer timeout="5000" />

   

    <!-- 扫描包 -->

    <dubbo:annotation package="org.controller"/>

    <context:annotation-config/>

    <context:component-scan base-package="org.controller"/>

   

   

    <!-- 3.视图解析器 -->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

       <property name="prefix" value="/WEB-INF/jsp/" />

       <property name="suffix" value=".jsp" />

    </bean>

 

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"

    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"

    version="2.5">

   

<!--   <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> -->

   

    <!-- 3.springmvc的前端控制器 -->

    <servlet>

       <servlet-name>dubbo-consumer</servlet-name>

       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

       <!-- 3.1 contextConfigLocation不是必须的, 如果不配置 springmvc的配置文件默认在:WEB-INF/servletname+"-servlet.xml"  -->

       <init-param>

           <param-name>contextConfigLocation</param-name>

           <param-value>classpath:springmvc.xml</param-value>

       </init-param>

       <!-- 加载时立即启动 -->

       <load-on-startup>1</load-on-startup>

    </servlet>

    <servlet-mapping>

       <servlet-name>dubbo-consumer</servlet-name>

       <url-pattern>*.action</url-pattern>

    </servlet-mapping>

   

    </web-app>

控制器类StudentController

package org.controller;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import org.student.StudentService;

 

import com.alibaba.dubbo.config.annotation.Reference;

 

//@RestController

@Controller

@RequestMapping("controller")

public class StudentController {

 

    //@Autowired

    @Reference //将远程服务的注入进来

    private StudentService studentService;

   

    @RequestMapping("rpcServer")

    public String rpcServer(Model model){

       String sayHello = studentService.sayHello("world");

       model.addAttribute("sayHello", sayHello);

       System.out.println(sayHello);

       return "success";

    }

   

}

 

接口StudentService

消费者consumer目录结构

success.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset= utf-8">

<title>dubbo入门</title>

</head>

<body>

 

<h1>dubbo入门之:${sayHello } 看到我就证明你成功了</h1>

 

</body>

</html>

测试:

如果不想敲,我这里有源码,以及文档: 点击即可跳转

 

 

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值