dubbo+zookeeper+springmvc整合,小入门

来源:互联网 发布:乐视电视mac码在哪 编辑:程序博客网 时间:2024/04/28 06:35

1.介绍

百度百科的话:Dubbo是  阿里巴巴公司开源的一个高性能优秀的服务框架,使得应用可通过高性能的 RPC 实现服务的输出和输入功能,可以和Spring框架无缝集成。dubbo的体系架构中,有以下5大组建,分别是:Provider: 暴露服务的服务提供方。Consumer: 调用远程服务的服务消费方。Registry: 服务注册与发现的注册中心。Monitor: 统计服务的调用次调和调用时间的监控中心。Container: 服务运行容器。ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务。个人理解:dubbo是一个服务框架,用来抛出服务,是soa(面向服务的体系架构的一种实现),可以把服务以分布式的理念放到服务器集群中来提供稳定的服务。而dubbo中需要一个服务注册中心,用来注册服务和发现服务调用,官方默认的选择是zookeeper。zookeeper是一个协调服务程序,正好可以用来管理dubbo的服务。如果想都对dubbo的架构深入理解,可以百度一下,这里就不过多赘述了。

2.入门小案例

2.1.先搭建好服务注册中心

其实很简单,在apache上下载好zookeeper后,解压缩,得到一个文件目录。如图:

无需过多配置,直接点击bin/zkServer.cmd后运行zookeeper服务,默认监听在2181。[这里不详细zookeeper的配置了]

2.2搭建dubbo监控环境

dubbo官方提供了一款非常好用的dubbo监控web应用,叫做dubbo-admin。下载war包,安装到tomcat服务器后,配置监控的服务中心,在WEB-INF/dubbo.properties中。
#默认配置的是zookeeper服务注册中心dubbo.registry.address=zookeeper://127.0.0.1:2181dubbo.admin.root.password=rootdubbo.admin.guest.password=guest
启动后,输入http://localhost:8080,用户名密码都是root,就可以进入dubbo-admin系统,来查看zookeeper中的服务了。

2.3.三个maven项目介绍

dubbo.api           | 主要是用来定义要抛出的服务的规范的,只是一堆借口。dubbo.service.impl  | 主要是实现服务接口的[基于dubbo.api],作为dubbo的服务提供者。dubbo.service.call  | 主要是用来调用dubbo共享出来的服务,作为dubbo的服务消费者。下载地址:http://git.oschina.net/mgang/dubbo-demo(gitOsChina上管理)

3.代码详细

3.1.先声明服务接口规范

package com.mg.dubbo.register.service;/** * dubbo服务测试 * @author meigang * */public interface MgService {    public String helloDubbo(String name);    public String cry(String name);}

3.2.实现接口,并注册服务(服务提供者)

package com.mg.dubbo.register.service.impl;import java.util.Date;import com.mg.dubbo.register.service.MgService;public class MgServiceImpl implements MgService{    public String helloDubbo(String name) {        return "hello "+name +";the time is "+new Date().toLocaleString();    }    public String cry(String name) {        return name+" don't cry!";    }}
编写dubboContext.xml文件,向zookeeper注册中心注册服务:
<?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:jee="http://www.springframework.org/schema/jee"    xmlns:tx="http://www.springframework.org/schema/tx" 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-3.1.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd    http://www.springframework.org/schema/jee    http://www.springframework.org/schema/jee/spring-jee-3.1.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-3.1.xsd"    default-lazy-init="false">    <!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->    <dubbo:application name="dubbo_provider"></dubbo:application>    <!-- 使用zookeeper注册中心暴露服务地址 -->    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false" subscribe="false" register=""></dubbo:registry>    <!-- 要暴露的服务接口 -->    <bean id="MgService" class="com.mg.dubbo.register.service.impl.MgServiceImpl"/>    <dubbo:service interface="com.mg.dubbo.register.service.MgService" ref="MgService" /></beans>

配置完成后,启动dubbo.service.impl项目,监听8001端口。在查看dubbo-admin就会看到注册的服务了。如图:

3.3.调用服务,编写服务消费者

就是dubbo.service.call。
package com.mg.dubbo.controller;import java.io.IOException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import com.mg.dubbo.register.service.MgService;@Controller@RequestMapping(value="/")public class AController {    @Autowired    private MgService mgService;    @RequestMapping(value="/index")    public void index(HttpServletRequest req,HttpServletResponse res) throws IOException{        String resString = mgService.helloDubbo("xiaogang");        res.getWriter().print(resString);    }    @RequestMapping(value="/cry")    public void cry(HttpServletRequest req,HttpServletResponse res) throws IOException{        String resString = mgService.cry("xiaoqiang");        res.getWriter().print(resString);    }}

编写dubboContext.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:jee="http://www.springframework.org/schema/jee"    xmlns:tx="http://www.springframework.org/schema/tx" 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-3.1.xsd    http://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd    http://www.springframework.org/schema/jee    http://www.springframework.org/schema/jee/spring-jee-3.1.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-3.1.xsd"    default-lazy-init="false">    <dubbo:application name="dubbo_consumer"></dubbo:application>    <!-- 使用zookeeper注册中心暴露服务地址 -->    <dubbo:registry address="zookeeper://127.0.0.1:2181" check="false"></dubbo:registry>    <!-- 要引用的服务 -->    <dubbo:reference interface="com.mg.dubbo.register.service.MgService" id="MgService"></dubbo:reference></beans>

配置好后,启动dubbo.service.call项目,监听8002端口。再查看dubbo-admin监控,就可以看到服务的消费者了。如图:

3.4.调用controller来查看调用结果

输入http://localhost:8002/index

输入http://localhost:8002/cry

0 0
原创粉丝点击