zoukankan      html  css  js  c++  java
  • Dubbo框架应用之(四)--Dubbo基于Zookeeper实现分布式实例

          上三篇文章主要是攻克了概念性的补充和学习,充分结合实战来深入理解  


    入门实例解析

    第一:provider-提供服务和对应的接口


    创建DemoService接口

    package com.unj.dubbotest.provider;
    
    import java.util.List;
    
    /**
     * 定义服务接口。该接口须要单独打包,在服务提供方和消费方共享
     * 
     * @author lishehe-2015年6月22日
     *
     */
    public interface DemoService {
    	/*
    	 * sayHello方法
    	 */
    	String sayHello(String name);
    
    	/*
    	 * 获取用户信息方法
    	 */
    	public List getUsers();
    
    }


    创建本接口的实现类

    package com.unj.dubbotest.provider.impl;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.unj.dubbotest.provider.DemoService;
    /*
     * 实现类DemoServiceImpl-李社河-2015年6月22日
     */
    public class DemoServiceImpl implements DemoService {
    	//声明sayHello方法
    	public String sayHello(String name) {
    		return "Hello " + name;
    	}
    /**
     * 获取用户信息getUsers-李社河-2015年6月22日
     */
    	public List getUsers() {
    		List list = new ArrayList();
    		User u1 = new User();
    		//jack信息
    		u1.setName("jack");
    		u1.setAge(20);
    		u1.setSex("m");
    		
    		//tom信息
    		User u2 = new User();
    		u2.setName("tom");
    		u2.setAge(21);
    		u2.setSex("m");
    
    		//rose信息
    		User u3 = new User();
    		u3.setName("rose");
    		u3.setAge(19);
    		u3.setSex("w");
    
    		list.add(u1);
    		list.add(u2);
    		list.add(u3);
    		return list;//返回数据集合
    	}
    }
    

    创建provider.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: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="demoService" class="com.unj.dubbotest.provider.impl.DemoServiceImpl" /> <!-- 提供方应用信息,用于计算依赖关系,李社河 --> <dubbo:application name="xixi_provider" /> <!-- 使用multicast广播注冊中心暴露服务地址 <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <!-- 使用zookeeper注冊中心暴露服务地址 --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明须要暴露的服务接口 --> <dubbo:service interface="com.unj.dubbotest.provider.DemoService" ref="demoService" /> </beans>



    创建启动类

    public class Provider {
    
    	public static void main(String[] args) throws Exception {
    		//启动spring容器,把server启动之后注冊到Zookeeper
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
    				new String[] { "applicationContext.xml" });
    		context.start();
    		System.in.read(); // 为保证服务一直开着。利用输入流的堵塞来模拟
    	}
    }



    创建dubboconsumer(消费者)

     注意 provider项目中的DemoService接口打包demo-service-api.jar放在class path中


    创建consumer.xml配置文件


    applicationContext.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: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="hehe_consumer" /> <!-- 使用zookeeper注冊中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,能够像使用本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.unj.dubbotest.provider.DemoService" /> </beans>



    创建consumer启动类

    package com.alibaba.dubbo.demo.pp;
    
    import java.util.List;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.unj.dubbotest.provider.DemoService;
    /**
     * Consumer运行起始类
     * @author 李社河-2015年6月22日
     *
     */
    public class Consumer {
    
    	public static void main(String[] args) throws Exception {
    		//初始化Consumer中的spring容器
    		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
    				new String[] { "applicationContext.xml" });
    		context.start();
    		// 获取远程服务代理
    		DemoService demoService = (DemoService) context.getBean("demoService");
    		//运行远程方法
    		String hello = demoService.sayHello("tom");
    		//信息打印
    		System.out.println(hello);
    
    		//相同运行远程方法,打印相关信息
    		List list = demoService.getUsers();
    		if (list != null && list.size() > 0) {
    			for (int i = 0; i < list.size(); i++) {
    				System.out.println(list.get(i));
    			}
    		}
    		System.in.read();// 为保证服务一直开着,利用输入流的堵塞来模拟
    	}
    
    }

    启动好zookeeper、tomcat之后我们运行运行Provider.class、Consumer.class

    成功调到远程服务-----运行Consumer之后结果



    我们在看管理后台的信息

    提供者


    消费者



    实例升级--Dubbo服务集群容错实践

           手机应用是以聊天室为基础的,我们须要收集用户的操作行为,然后计算聊天室中在线人数,并实时在手机应用端显示人数,整个系统的架构如图所看到的:


       上图中,主要包含了两大主要流程:日志收集并实时处理流程、调用读取实时计算结果流程。我们使用基于Dubbo框架开发的服务来提供实时计算结果读取聊天人数的功能。

    上图中,实际上业务接口server集群也能够基于Dubbo框架构建服务,就看我们想要构建什么样的系统来满足我们的须要。

       假设不使用注冊中心,服务消费方也能够直接调用服务提供方公布的服务,这样须要服务提供方将服务地址暴露给服务消费方,并且也无法使用监控中心的功能,这样的方式成为直连。

       假设我们使用注冊中心。服务提供方将服务公布到注冊中心。而服务消费方能够通过注冊中心订阅服务,接收服务提供方服务变更通知,这样的方式能够隐藏服务提供方的细节,包含server地址等敏感信息,而服务消费方仅仅能通过注冊中心来获取到已注冊的提供方服务,而不能直接跨过注冊中心与服务提供方直接连接。这样的方式的优点是还能够使用监控中心服务,能够对服务的调用情况进行监控分析,还能使用Dubbo服务管理中心,方便管理服务,我们在这里使用的是这样的方式。也推荐使用这样的方式。使用注冊中心的Dubbo分布式服务相关组件结构。例如以下图所看到的:



    总结

            层层深入继续努力吧,随着项目的进行不断的和大家分享……

  • 相关阅读:
    .net core读取appsettings.config中文乱码问题
    vs2017错误:当前页面的脚本发生错误
    VS Code中无法识别npm命令
    Visual Studio报错/plugin.vs.js,行:1074,错误:缺少标识符、字符串或数字
    记录一次在生成数据库服务器上出现The timeout period elapsed prior to completion of the operation or the server is not responding.和Exception has been thrown by the target of an invocation的解决办法
    Java集合框架
    java hash表
    Java Dictionary 类存储键值
    java数据结构 栈stack
    java封装
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7243564.html
Copyright © 2011-2022 走看看