创建Maven项目下的Dubbo+Zookeeper框架

来源:互联网 发布:金蝶软件标准版破解 编辑:程序博客网 时间:2024/05/21 06:16

引言:


互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的。现在核心业务抽取出来,作为独立的服务,使前端应用能更快速和稳定的响应。


第一:Dubbo背景





大规模服务化,应用使用RMI或Hessian等工具,通过配置服务URL地址调用,通过F5等硬件进行负载均衡。

当然了,在系统越来越复杂的时候我们就面临三个问题:


(1) 当服务越来越多时,服务URL配置管理变得非常困难,F5硬件负载均衡器的单点压力也越来越大。


此时需要一个服务注册中心,动态的注册和发现服务,使服务的位置透明。并通过在消费方获取服务提供方地址列表,实现软负载均衡和Failover,降低对F5硬件负载均衡器的依赖,也能减少部分成本。


(2) 当进一步发展,服务间依赖关系变得错踪复杂,甚至分不清哪个应用要在哪个应用之前启动,架构师都不能完整的描述应用的架构关系。


这时,需要自动画出应用间的依赖关系图,以帮助架构师理清理关系。


(3) 接着,服务的调用量越来越大,服务的容量问题就暴露出来,这个服务需要多少机器支撑?什么时候该加机器?


为了解决这些问题,第一步,要将服务现在每天的调用量,响应时间,都统计出来,作为容量规划的参考指标。

其次,要可以动态调整权重,在线上,将某台机器的权重一直加大,并在加大的过程中记录响应时间的变化,直到响应时间到达阀值,记录此时的访问量,再以此访问量乘以机器数反推总容量。


Dubbo就是基于Hessian实现了自己Hessian协议,可以直接通过配置的Dubbo内置的其他协议,在服务消费方进行远程调用,也就是说,服务调用方需要使用Java语言来基于Dubbo调用提供方服务,限制了服务调用方。


我们需要一个这样的界面:




第二:Dubbo简介


Dubbo的原理图:




节点角色说明:


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

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

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

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

Container: 服务运行容器。


调用关系说明:


0. 服务容器负责启动,加载,运行服务提供者。

1. 服务提供者在启动时,向注册中心注册自己提供的服务。

2. 服务消费者在启动时,向注册中心订阅自己所需的服务。

3. 注册中心返回服务提供者地址列表给消费者,如果有变更,注册中心将基于长连接推送变更数据给消费者。

4. 服务消费者,从提供者地址列表中,基于软负载均衡算法,选一台提供者进行调用,如果调用失败,再选另一台调用。

5. 服务消费者和提供者,在内存中累计调用次数和调用时间,定时每分钟发送一次统计数据到监控中心。


 Dubbo提供了很多协议,Dubbo协议、RMI协议、Hessian协议,我们查看Dubbo源代码,有各种协议的实现,如图所示:





我们之前没用Dubbo之前时,大部分都使用Hessian来使用我们服务的暴露和调用,利用HessianProxyFactory调用远程接口。


上面是参见Github官网,接下来我们来介绍DubboZookeeper整合使用。


第三:Duboo与Zookeeper整合:


1、搭建Dubbo环境


1)、在Linux系统上安装JDK(使用1.7版本的jdk-7u25-linux-x64.tar.gz)

安装按照百度经验:

Linux JDK安装及配置 (tar.gz)


2)、安装Zookeeper

按照博客:

centoszookeeper安装配置


3)、安装Tomcat

按照博客:

CentOS-6.3安装配置Tomcat-7


4)、配置Dubbo-admin管理页面

按照博客:

DubboZookeeperSpringMVC整合和使用(负载均衡、容错)


这里我们需要注意一个地方:


 下载dubbo-admin-2.4.1.war包,在Linuxtomcat部署,先将tomcatwebapps/Root文件夹下载的所有的文件删除,把dubbo-admin-2.4.1放在tomcatwebapps/Root下,然后进行解压:


#jar -xvf dubbo-admin-2.4.1.war

第一开始的时候,我直接将dubbo-admin-2.4.1.war包解压到Root下面,结果,不能访问了。因此大家在这里注意一下。


注:大家要记得关闭防火墙:


systemctl stop firewalld.service #停止


2、创建manve项目下面的Dubbo+ZK项目:


目录:




1)、parent项目为pom项目


pom文件:


<span style="font-size:18px;"><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>qmx.dubbo</groupId>  <artifactId>qmx-dubbo-parent</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>pom</packaging>    <modules>        <module>../qmx-dubbo-api</module>        <module>../qmx-dubbo-provider</module>        <module>../qmx-dubbo-consumer</module>    </modules>       <properties>        <spring.version>4.2.4.RELEASE</spring.version>        <dubbo.version>2.5.3</dubbo.version>        <zookeeper.version>3.4.7</zookeeper.version>        <zkclient.version>0.7</zkclient.version>    </properties>        <build>        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <version>3.3</version>                <configuration>                    <encoding>UTF-8</encoding>                </configuration>            </plugin>        </plugins>    </build>  </project></span>


Parent只有一个pom文件。


2)、创建api项目




api的pom文件:


<span style="font-size:18px;"><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>    <parent>       <groupId>qmx.dubbo</groupId>   <artifactId>qmx-dubbo-parent</artifactId>       <version>0.0.1-SNAPSHOT</version>       <relativePath>../qmx-dubbo-parent/pom.xml</relativePath>    </parent>    <groupId>qmx.dubbo</groupId>  <artifactId>qmx-dubbo-api</artifactId>  <version>0.0.1-SNAPSHOT</version></project></span>

DemoQmxService:


<span style="font-size:18px;">package com.tgb.qmx.demo.dubbo.api;import java.util.List;public interface DemoQmxService {String sayHello(String name);public List<User> getUsers();}</span>

User:


<span style="font-size:18px;">package com.tgb.qmx.demo.dubbo.api;import java.io.Serializable;public class User implements Serializable {private static final long serialVersionUID = 1L;private int age;private String name;private String sex;...}</span>

3)、provider:




pom:


<span style="font-size:18px;"><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">  <parent>       <groupId>qmx.dubbo</groupId>   <artifactId>qmx-dubbo-parent</artifactId>       <version>0.0.1-SNAPSHOT</version>       <relativePath>../qmx-dubbo-parent/pom.xml</relativePath>    </parent>    <modelVersion>4.0.0</modelVersion>  <groupId>qmx.dubbo</groupId>  <artifactId>qmx-dubbo-provider</artifactId>  <version>0.0.1-SNAPSHOT</version>     <dependencies>        <dependency>            <groupId>qmx.dubbo</groupId>            <artifactId>qmx-dubbo-api</artifactId>            <version>0.0.1-SNAPSHOT</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>${dubbo.version}</version>            <exclusions>                <exclusion>                    <groupId>org.springframework</groupId>                    <artifactId>spring</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>com.101tec</groupId>            <artifactId>zkclient</artifactId>            <version>${zkclient.version}</version>            <exclusions>                <exclusion>                    <groupId>javax.mail</groupId>                    <artifactId>mail</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>${zookeeper.version}</version>        </dependency>    </dependencies>  </project></span>


DemoQmxServiceImpl:


<span style="font-size:18px;">package com.tgb.qmx.demo.dubbo.provider;import java.util.ArrayList;import java.util.List;import com.tgb.qmx.demo.dubbo.api.DemoQmxService;import com.tgb.qmx.demo.dubbo.api.User;public class DemoQmxServiceImpl implements DemoQmxService {public String sayHello(String name) {return "Hello " + name;}public List getUsers() {List<User> list = new ArrayList<User>();User u1 = new User();u1.setName("jack");u1.setAge(20);u1.setSex("m");list.add(u1);return list;}}</span>


Provider:


<span style="font-size:18px;">package com.tgb.qmx.demo.dubbo.provider;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {public static void main(String[] args) throws Exception {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });context.start();System.out.println("启动成功");System.in.read(); // 为保证服务一直开}}</span>


applicationContext:


<span style="font-size:18px;"><?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        "><!-- 具体的实现bean --><bean id="demoQmxService" class="com.tgb.qmx.demo.dubbo.provider.DemoQmxServiceImpl" /><!-- 提供方应用信息,用于计算依赖关系 --><dubbo:application name="demo_provider" /><!-- 使用multicast广播注册中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234" /> --><!-- 使用zookeeper注册中心暴露服务地址 --><dubbo:registry address="zookeeper://192.168.33.10:2181" /><!-- 用dubbo协议在20880端口暴露服务 --><dubbo:protocol name="dubbo" port="20881" /><!-- 声明需要暴露的服务接口 --><dubbo:service interface="com.tgb.qmx.demo.dubbo.api.DemoQmxService"ref="demoQmxService" /></beans></span>

log4j:


<span style="font-size:18px;">log4j.rootLogger=info, stdoutlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n</span>


4)、customer


api:


<span style="font-size:18px;"><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>qmx.dubbo</groupId>  <artifactId>qmx-dubbo-consumer</artifactId>  <version>0.0.1-SNAPSHOT</version>     <parent>       <groupId>qmx.dubbo</groupId>   <artifactId>qmx-dubbo-parent</artifactId>       <version>0.0.1-SNAPSHOT</version>       <relativePath>../qmx-dubbo-parent/pom.xml</relativePath>    </parent>   <dependencies>        <dependency>            <groupId>qmx.dubbo</groupId>            <artifactId>qmx-dubbo-api</artifactId>            <version>0.0.1-SNAPSHOT</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring.version}</version>        </dependency>        <dependency>            <groupId>com.alibaba</groupId>            <artifactId>dubbo</artifactId>            <version>${dubbo.version}</version>            <exclusions>                <exclusion>                    <groupId>org.springframework</groupId>                    <artifactId>spring</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>com.101tec</groupId>            <artifactId>zkclient</artifactId>            <version>${zkclient.version}</version>            <exclusions>                <exclusion>                    <groupId>javax.mail</groupId>                    <artifactId>mail</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.apache.zookeeper</groupId>            <artifactId>zookeeper</artifactId>            <version>${zookeeper.version}</version>        </dependency>    </dependencies>  </project></span>

Consumer:


<span style="font-size:18px;">package com.tgb.qmx.demo.dubbo.consume;import java.util.List;import java.util.concurrent.Callable;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.tgb.qmx.demo.dubbo.api.DemoQmxService;import com.tgb.qmx.demo.dubbo.api.User;public class Consumer {    public static void main(String[] args) throws Exception {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(                new String[]{"applicationContext.xml"});        context.start();        final DemoQmxService demoService = (DemoQmxService) context.getBean("demoQmxService");        //测试        String hello = demoService.sayHello("Tom");        System.out.println(hello);    }}</span>

applicationContext:


<span style="font-size:18px;"><?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="demo_consumer" /><!-- 使用zookeeper注册中心暴露服务地址 --><!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --><dubbo:registry address="zookeeper://192.168.33.10:2181" /><!-- 生成远程服务代理,可以像使用本地bean一样使用demoService --><dubbo:reference id="demoQmxService"interface="com.tgb.qmx.demo.dubbo.api.DemoQmxService" /></beans></span>


log4j:


同Provider中的log4j文件。


总结:


从网上找的程序大致都是这样写的,但是,这个里面也是可以进行很多架构上的优化:


1、前台上应用spring MVC。

2、配置文件,可以使用spring来加载,当然如果优化性能的话,我们可以直接写一个main方法,在系统启动的时候,将文件进行调用。

3、我们可以将provider和consumer的配置文件进行保留,但是,在代码中,不需要将他们进行物理上的区别。


参考博客:

http://blog.csdn.net/congcong68/article/details/41113239




1 0