Dubbo快速入门

文章目录

  • 一、Apache Dubbo概述
  • 二、服务注册中心Zookeeper
  • 三、Dubbo快速入门
    • 1、安装Zookeeper
      • 1.1:安装jdk
      • 1.2:上传并且解压
      • 1.3:创建data文件
      • 1.4:修改文件名
      • 1.5:修改文件属性
      • 1.6:启动、停止zookeeper
    • 2、服务方提供开发
      • 1.1、创建maven工程(pom.xml文件导入)
      • 1.2、配置web.xml文件
      • 1.3、创建服务接口
      • 1.4、创建服务实现类
      • 1.5、在resource下创建applicationContext.xml文件
      • 1.6、启动服务
    • 3、服务消费方开发
      • 1.1、创建maven工程(pom.xml文件导入)
      • 1.2、配置web.xml文件
      • 1.3、创建服务接口
      • 1.4、编写Controller
      • 1.5、在resource下创建applicationContext.xml文件
      • 1.6、运行测试


一、Apache Dubbo概述

Apache Dubbo是一款高性能的Java RPC框架。其前身是阿里巴巴公司开源的一个高性能、轻量级的开源Java RPC框架,可以和Spring框架无缝集成。

二、服务注册中心Zookeeper

Zookeeper 是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用
Dubbo快速入门_第1张图片

三、Dubbo快速入门

1、安装Zookeeper

下载地址:http://archive.apache.org/dist/zookeeper/

本教程使用的版本为3.6.4版本 云盘地址:点我下载 提取码:ob75

1.1:安装jdk

安装 jdk(略)

1.2:上传并且解压

把 zookeeper 的压缩包(zookeeper-3.4.6.tar.gz)上传到 linux 系统,
并且解压压缩包   tar -zxvf zookeeper-3.4.6.tar.gz	

1.3:创建data文件

进入zookeeper-3.4.6目录
创建data目录    mkdir data

1.4:修改文件名

进入conf目录 ,把zoo_sample.cfg 改名为zoo.cfg
cd conf
mv zoo_sample.cfg zoo.cfg

1.5:修改文件属性

打开zoo.cfg文件,  修改data属性:dataDir=/root/zookeeper-3.4.6/data
vim zoo.cfg

1.6:启动、停止zookeeper

进入Zookeeper的bin目录,启动服务命令
./zkServer.sh start

停止服务命令
./zkServer.sh stop

查看服务状态:
./zkServer.sh status

此步需要启动zookeeper: ./zkServer.sh start

2、服务方提供开发

使用idea(因为IDEA好使啊!!!)

1.1、创建maven工程(pom.xml文件导入)

<properties>
  <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
  <maven.compiler.source>1.8maven.compiler.source>
  <maven.compiler.target>1.8maven.compiler.target>
  <spring.version>5.0.5.RELEASEspring.version>
properties>
<dependencies>
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>${spring.version}version>
  dependency>
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-beansartifactId>
    <version>${spring.version}version>
  dependency>
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-webmvcartifactId>
    <version>${spring.version}version>
  dependency>
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jdbcartifactId>
    <version>${spring.version}version>
  dependency>
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-aspectsartifactId>
    <version>${spring.version}version>
  dependency>
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-jmsartifactId>
    <version>${spring.version}version>
  dependency>
  <dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-context-supportartifactId>
    <version>${spring.version}version>
  dependency>
  
  <dependency>
    <groupId>com.alibabagroupId>
    <artifactId>dubboartifactId>
    <version>2.6.0version>
  dependency>
  <dependency>
    <groupId>org.apache.zookeepergroupId>
    <artifactId>zookeeperartifactId>
    <version>3.4.7version>
  dependency>
  <dependency>
    <groupId>com.github.sgroschupfgroupId>
    <artifactId>zkclientartifactId>
    <version>0.1version>
  dependency>
  <dependency>
    <groupId>javassistgroupId>
    <artifactId>javassistartifactId>
    <version>3.12.1.GAversion>
  dependency>
  <dependency>
    <groupId>com.alibabagroupId>
    <artifactId>fastjsonartifactId>
    <version>1.2.47version>
  dependency>
dependencies>
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.pluginsgroupId>
      <artifactId>maven-compiler-pluginartifactId>
      <version>2.3.2version>
      <configuration>
        <source>1.8source>
        <target>1.8target>
      configuration>
    plugin>
    <plugin>
      <groupId>org.apache.tomcat.mavengroupId>
      <artifactId>tomcat7-maven-pluginartifactId>
      <configuration>
        
        <port>8081port>
        
        <path>/path>
      configuration>
    plugin>
  plugins>
build>

1.2、配置web.xml文件


<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext*.xmlparam-value>
  context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
web-app>

1.3、创建服务接口

package com.xiao.service;
public interface HelloService {
    public String sayHello(String name);
}

1.4、创建服务实现类

package com.xiao.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.service.HelloService;

@Service
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "hello " + name;
    }
}

注意:服务实现类上使用的Service注解是Dubbo提供的,用于对外发布服务

1.5、在resource下创建applicationContext.xml文件


<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	    xmlns:p="http://www.springframework.org/schema/p"
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	    xmlns:mvc="http://www.springframework.org/schema/mvc"
		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.xsd
         http://code.alibabatech.com/schema/dubbo
         http://code.alibabatech.com/schema/dubbo/dubbo.xsd
         http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd">
	
	<dubbo:application name="dubbodemo_provider" />
	
	<dubbo:registry address="zookeeper://192.168.1.1:2181"/>
	
	<dubbo:protocol name="dubbo" port="20881">dubbo:protocol>
	
	<dubbo:annotation package="com.xiao.service.impl" />
beans>

1.6、启动服务

因为我们集成了tomcat,并且设置了端口为8081,在右侧的maven工具栏中,双击 tomcat7:run

3、服务消费方开发

开发步骤

1.1、创建maven工程(pom.xml文件导入)

创建maven工程(打包方式为war)dubbodemo_consumer

<properties>
    
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    
    <maven.compiler.source>1.8maven.compiler.source>
    <maven.compiler.target>1.8maven.compiler.target>
    
    <spring.version>5.0.5.RELEASEspring.version>
  properties>
  <dependencies>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-contextartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-beansartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jdbcartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-aspectsartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-jmsartifactId>
      <version>${spring.version}version>
    dependency>
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-context-supportartifactId>
      <version>${spring.version}version>
    dependency>
    
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>dubboartifactId>
      <version>2.6.0version>
    dependency>
    <dependency>
      <groupId>org.apache.zookeepergroupId>
      <artifactId>zookeeperartifactId>
      <version>3.4.7version>
    dependency>
    <dependency>
      <groupId>com.github.sgroschupfgroupId>
      <artifactId>zkclientartifactId>
      <version>0.1version>
    dependency>
    <dependency>
      <groupId>javassistgroupId>
      <artifactId>javassistartifactId>
      <version>3.12.1.GAversion>
    dependency>
    <dependency>
      <groupId>com.alibabagroupId>
      <artifactId>fastjsonartifactId>
      <version>1.2.47version>
    dependency>
  dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.pluginsgroupId>
        <artifactId>maven-compiler-pluginartifactId>
        <version>2.3.2version>
        <configuration>
          <source>1.8source>
          <target>1.8target>
        configuration>
      plugin>
      <plugin>
        <groupId>org.apache.tomcat.mavengroupId>
        <artifactId>tomcat7-maven-pluginartifactId>
        <configuration>
          
          <port>8082port>
          
          <path>/path>
        configuration>
      plugin>
    plugins>
  build>

1.2、配置web.xml文件


<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>
  <servlet>
    <servlet-name>springmvcservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:applicationContext.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>
  <servlet-mapping>
    <servlet-name>springmvcservlet-name>
    <url-pattern>*.dourl-pattern>
  servlet-mapping>
web-app>

1.3、创建服务接口

package com.xiao.service;
public interface HelloService {
    public String sayHello(String name);
}

1.4、编写Controller

ckage com.xiao.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.xiao.service.HelloService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo")
public class HelloController {
    @Reference
    private HelloService helloService;

    @RequestMapping("/hello")
    @ResponseBody
    public String getName(String name){
        //远程调用
        String result = helloService.sayHello(name);
        System.out.println(result);
        return result;
    }
}

1.5、在resource下创建applicationContext.xml文件


<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	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.xsd
			http://code.alibabatech.com/schema/dubbo
			http://code.alibabatech.com/schema/dubbo/dubbo.xsd
			http://www.springframework.org/schema/context
			http://www.springframework.org/schema/context/spring-context.xsd">

	
	<dubbo:application name="dubbodemo-consumer" />
	
	<dubbo:registry address="zookeeper://192.168.1.1:2181"/>
	
	<dubbo:annotation package="com.itheima.controller" />
beans>

1.6、运行测试

因为我们集成了tomcat,并且设置了端口为8082,在右侧的maven工具栏中,双击 tomcat7:run

编写完成!
在浏览器输入http://localhost:8082/demo/hello.do?name=Jack,查看浏览器输出结果

提示:该笔记是主要学到了某马的dubbo教程,然后想记录下笔记。

你可能感兴趣的:(dubbo,dubbo,java,linux,maven,zookeeper)