zoukankan      html  css  js  c++  java
  • Dubbo入门微服务框架学习

    Dubbo入门笔记

    Dubbo官网:http://dubbo.apache.org/zh-cn/

    Dubbo官方推荐使用Zookeeper作为服务注册中心,Zookeeper 是 Apache Hadoop 的子项目,是一个树型的目录服务,支持变更推送,适合作为 Dubbo 服务的注册中心,工业强度较高,可用于生产环境,并推荐使用 。

    安装Zookeeper

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

    安装步骤:

    第一步:安装 jdk(略)
    第二步:把 zookeeper 的压缩包(zookeeper-3.4.6.tar.gz)上传到 linux 系统
    第三步:解压缩压缩包
    ​ tar -zxvf zookeeper-3.4.6.tar.gz
    第四步:进入zookeeper-3.4.6目录,创建data目录
    ​ mkdir data
    第五步:进入conf目录 ,把zoo_sample.cfg 改名为zoo.cfg
    ​ cd conf
    ​ mv zoo_sample.cfg zoo.cfg
    第六步:打开zoo.cfg文件, 修改data属性:dataDir=/root/zookeeper-3.4.6/data

    启动、停止Zookeeper

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

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

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

    Dubbo快速入门

    Dubbo作为一个RPC框架,其最核心的功能就是要实现跨网络的远程调用。本小节就是要创建两个应用,一个作为服务的提供方,一个作为服务的消费方。通过Dubbo来实现服务消费方远程调用服务提供方的方法。

    服务提供方开发

    开发步骤:

    (1)创建maven工程(打包方式为war)dubbodemo_provider,在pom.xml文件中导入如下坐标

    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
      <spring.version>5.0.5.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.6.0</version>
      </dependency>
      <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.7</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.12.1.GA</version>
      </dependency>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.47</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.8</source>
            <target>1.8</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>
    

    (2)配置web.xml文件

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      <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>
    </web-app>
    
    

    (3)创建服务接口

    package cn.blogsx.service;
    public interface HelloService {
        public String sayHello(String name);
    }
    

    (4)创建服务实现类

    packagecn.blogsx.service.impl;
    import com.alibaba.dubbo.config.annotation.Service;
    import cn.blogsx.service.HelloService;
    
    @Service
    public class HelloServiceImpl implements HelloService {
        public String sayHello(String name) {
            return "hello " + name;
        }
    }
    

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

    (5)在src/main/resources下创建applicationContext-service.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: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" />
    	<!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
    	<dubbo:registry address="zookeeper://192.168.134.129:2181"/>
    	<!-- 注册  协议和port   端口默认是20880 -->
    	<dubbo:protocol name="dubbo" port="20881" />
    	<!-- 扫描指定包,加入@Service注解的类会被发布为服务  -->
    	<dubbo:annotation package="cn.blogsx.service.impl" />
    </beans>
    

    (6)启动服务

    tomcat7:run

    服务消费方开发

    开发步骤:

    (1)创建maven工程(打包方式为war)dubbodemo_consumer,pom.xml配置和上面服务提供者相同,只需要将Tomcat插件的端口号改为8082即可

    (2)配置web.xml文件

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 指定加载的配置文件 ,通过参数contextConfigLocation加载 -->
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext-web.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
      </servlet-mapping>
    </web-app>
    

    (3)将服务提供者工程中的HelloService接口复制到当前工程

    (4)编写Controller

    package cn.blogsx.controller;
    import com.alibaba.dubbo.config.annotation.Reference;
    import cn.blogsx.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;
        }
    }
    

    注意:Controller中注入HelloService使用的是Dubbo提供的@Reference注解

    (5)在src/main/resources下创建applicationContext-web.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: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" />
    	<!-- 连接服务注册中心zookeeper ip为zookeeper所在服务器的ip地址-->
    	<dubbo:registry address="zookeeper://192.168.134.129:2181"/>
    	<!-- 扫描的方式暴露接口  -->
    	<dubbo:annotation package="cn.blogsx.controller" />
    </beans>
    

    (6)运行测试

    tomcat7:run启动

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

    Dubbo管理控制台

    我们在开发时,需要知道Zookeeper注册中心都注册了哪些服务,有哪些消费者来消费这些服务。我们可以通过部署一个管理中心来实现。其实管理中心就是一个web应用,部署到tomcat即可。

    安装

    安装步骤:

    (1)将资料中的dubbo-admin-2.6.0.war文件复制到tomcat的webapps目录下

    (2)启动tomcat,此war文件会自动解压

    (3)修改WEB-INF下的dubbo.properties文件,注意dubbo.registry.address对应的值需要对应当前使用的Zookeeper的ip地址和端口号

    ​ dubbo.registry.address=zookeeper://192.168.134.129:2181
    ​ dubbo.admin.root.password=root
    ​ dubbo.admin.guest.password=guest

    (4)重启tomcat

    使用

    操作步骤:

    (1)访问http://localhost:8080/dubbo-admin-2.6.0/,输入用户名(root)和密码(root)

    代码仓库: https://gitee.com/sixudev/dubbo_demo

  • 相关阅读:
    fake data
    template 的简单使用
    computed what time passage pushed-
    drag And drop
    threeJs(1)
    使用babel
    PHP海补知识(2)-- 复合赋值操作
    PHP海补知识(1)-- 可变变量
    一个裸的Ubuntu系统,搭建LAMP需要配置这些东西
    Ubuntu Server 12.04 U盘启动盘打包
  • 原文地址:https://www.cnblogs.com/sxblog/p/13538235.html
Copyright © 2011-2022 走看看