Java

品优购学习笔记一-Dubbox框架

2019-05-07  本文已影响15人  smallmartial

品优购学习笔记一-Dubbox框架

1.dubbox简介

Dubbox 是一个分布式服务框架,其前身是阿里巴巴开源项目Dubbo ,被国内电商及互联网项目中使用,后期阿里巴巴停止了该项目的维护,当当网便在Dubbo基础上进行优化,并继续维护,为了与原有的Dubbo区分,故将其命名为Dubbox

Dubbox 致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案。简单的说,dubbox就是个服务框架,如果没有分布式的需求,其实是不需要用的,只有在分布式的时候,才有dubbox这样的分布式服务框架的需求,并且本质上是个服务调用的东东,说白了就是个远程服务调用的分布式框架。


1557189928924.png

节点说明:

调用关系说明:

2.注册中心zookeeper

2.1 Zookeeper 介绍

官方推荐使用 zookeeper 注册中心。注册中心负责服务地址的注册与查找,相当于目录服务,服务提供者和消费者只在启动时与注册中心交互,注册中心不转发请求,压力较小。

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

2.2 zookeeper在ubuntu系统下安装

安装步骤:

tar -zxvf zookeeper-3.4.6.tar.gz
cd zookeeper-3.4.6
mkdir data
cd conf
mv zoo_sample.cfg zoo.cfg
#修改 写自己的data目录的路径,进入data目录 pwd查看当前目录路径
dataDir=/root/zookeeper-3.4.6/data
#添加,改成自己服务器的ip
server.1=182.254.227.85:2888:3888
server.2=10.0.40.112:2888:3888
server.3=10.0.42.145:2888:3888

cd data
sudo vim myid
#将相对应的编号写入

1557190779682.png
./zkServer.sh start
1557190846515.png

关闭:

./zkServer.sh stop

查看状态

./zkServer.sh status

2.3Dubbox本地 JAR包部署与安装

Dubbox的jar包并没有部署到Maven的中央仓库中,大家在Maven的中央仓库中可以查找到Dubbo的最终版本是2.5.3 , 阿里巴巴解散了Dubbo团队后由当当网继续维护此项目,并改名为 Dubbox ,坐标不变,版本变更了,但是并没有提交到中央仓库。

我们现在需要手动将Dubbox的jar包安装到我的本地仓库中.

先将dubbo-2.8.4.jar包放到d:\setup, 然后输入命令

mvn install:install-file -Dfile=d:\setup\dubbo-2.8.4.jar -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.8.4 -Dpackaging=jar

资源文件 以及相关配置可以在作者的github仓库查看

3.Demo

项目目录:

1557191531071.png

3.1服务提供者开发

创建maven工程,pom.xml引入

<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>cn.smallmartial.demo</groupId>
  <artifactId>dubboxdemo-service</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <properties>      
        <spring.version>4.2.4.RELEASE</spring.version>
   </properties>
    
    <dependencies>
        <!-- Spring -->
        <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>   

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>            
        </dependency> 
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.4.6</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.8</source>  
                  <target>1.8</target>  
              </configuration>  
          </plugin>  
          <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <configuration>
                    <!-- 指定端口 -->
                    <port>8080</port>
                    <!-- 请求路径 -->
                    <path>/</path>
                </configuration>
          </plugin>
      </plugins>  
    </build>
</project>

在工程目录webapps下创建WEB-INF文件夹,创建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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">  
    
    <!-- 加载spring容器 -->
    <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>

创建业务接口

package cn.smallmartial.demo.service;

public interface UserService {

    public String getName();
}

创建实现类,service需要导入 alibaba提供的

package cn.smallmartial.demo.service.impl;

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

import cn.smallmartial.demo.service.UserService;

@Service
public class UserServiceImpl implements UserService {

    @Override
    public String getName() {
        return "small martial";
    }

}

编译配置文件,写入src/main/resources

<?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="dubboxdemo-service"/>  
    <dubbo:registry address="zookeeper://182.254.227.85:2181"/> 
    <dubbo:annotation package="cn.smallmartial.demo.service.impl" /> 
   
</beans>

运行,引用的是mavean插件,用mavean方式启动

tomcat7:run

3.2服务消费者开发

创建Maven工程(WAR)dubboxdemo-web ,在pom.xml引入依赖,同“dubboxdemo-service”工程。区别就是把tomcat插件的运行端口改为8081 。

pom和service用的一样,复制即可

在工程目录webapps下创建WEB-INF文件夹,创建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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    version="2.5">  
   <!-- 解决post乱码 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>   
    
  <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:springmvc.xml</param-value>
    </init-param>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>


 
    
</web-app>

将“dubboxdemo-service”工程的cn.smallmartial.demo.service包以及下面的接口拷贝至此工程。

编写controller

package cn.smallmartial.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

import cn.smallmartial.demo.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;
    
    @RequestMapping("/showname")
    @ResponseBody
    public String showName() {
        
        return userService.getName();
    }
    
}

编写spring配置文件

<?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">

    
    <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:application name="dubboxdemo-web"/>  
    <dubbo:registry address="zookeeper://182.254.227.85:2181"/> 
    <dubbo:annotation package="cn.smallmartial.demo.controller" /> 

</beans>

运行,引用的是mavean插件,用mavean方式启动

tomcat7:run

运行结果:

1557148668384.png

4.管理端安装

管理端使用

打开浏览器,输入http://192.168.25.132:8080/dubbo-admin/ ,登录用户名和密码均为root 进入首页。 (192.168.25.132:)是我部署的linux主机地址。
1557192563827.png

github地址:https://github.com/smallmartial/springbootdemo/tree/master/dobbox-demo

1557192838851.png

工具地址:

上一篇下一篇

猜你喜欢

热点阅读