Dubbo环境搭建之二 创建服务

来源:互联网 发布:大数据毕业设计题目 编辑:程序博客网 时间:2024/06/07 14:01

前提条件 Dubbo环境搭建之一环境准备

新建Maven项目

这里写图片描述
下一步输入你定义的group id与artifact id

GroupID是项目组织唯一的标识符,实际对应JAVA的包的结构,是main目录里java的目录结构。ArtifactID就是项目的唯一的标识符,实际对应项目的名称,就是项目根目录的名称。

修改pom.xml

增加以下依赖

<!-- dubbo --> <dependency>    <groupId>com.alibaba</groupId>    <artifactId>dubbo</artifactId>    <version>2.5.3</version></dependency><!-- zookepper --><dependency>  <groupId>org.apache.zookeeper</groupId>  <artifactId>zookeeper</artifactId>  <version>3.3.6</version>  <exclusions>        <exclusion>            <artifactId>jmxri</artifactId>            <groupId>com.sun.jmx</groupId>        </exclusion>        <exclusion>            <artifactId>jms</artifactId>            <groupId>javax.jms</groupId>        </exclusion>        <exclusion>            <artifactId>jmxtools</artifactId>            <groupId>com.sun.jdmk</groupId>        </exclusion>    </exclusions></dependency><dependency>  <groupId>log4j</groupId>  <artifactId>log4j</artifactId>  <version>1.2.17</version></dependency><!-- zookeeper客户端 --><dependency>    <groupId>com.github.sgroschupf</groupId>    <artifactId>zkclient</artifactId>    <version>0.1</version></dependency>

写一个接口与实现

package com.erlizhinian.demo;public interface DemoService {    String sayHello(String name2);}
package com.erlizhinian.demo.provider;import java.util.Random;import com.erlizhinian.demo.DemoService;public class DemoServiceImpl implements DemoService {    public String sayHello(String name) {        //打印当前线程id,可以验证后面伪集群的负载均衡调度.先按照这样写.        try {            Thread.sleep(new Random().nextInt(500));            System.out.println(Thread.currentThread().getId() + "----"+ name);        } catch (InterruptedException e) {            e.printStackTrace();        }        return "Hello " + name;    }}

配置

<?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="hello-world-app"  />    <!-- 使用multicast广播注册中心暴露服务地址 -->    <dubbo:registry address="zookeeper://127.0.0.1:2181" />    <!-- 用dubbo协议在20880端口暴露服务     <dubbo:protocol name="dubbo" port="20882" />    <dubbo:protocol name="http" port="20882" />    -->    <dubbo:protocol name="dubbo" />    <!-- 为了实现单进程多线程启动多个dubbo服务,方便测试,使用不同的端口-->    <bean id="dynamicDubboPortReaderDao" class="com.erlizhinian.config.dubbo.DynamicDubboPortReaderImpl" init-method="init" />    <!-- 声明需要暴露的服务接口 -->    <dubbo:service interface="com.erlizhinian.demo.DemoService" ref="demoService"  cluster="failsafe"  loadbalance="random" />    <!-- 和本地bean一样实现服务 -->    <bean id="demoService" class="com.erlizhinian.demo.provider.DemoServiceImpl" />    <dubbo:monitor protocol="registry"/>  </beans>

启动入口

package com.erlizhinian.main;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Provider {    public static void main(String[] args) throws Exception {        for(int i = 0 ; i < 5;i++){            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"classpath:provider.xml"});            context.start();        }        System.in.read(); // 按任意键退出    }}

整体结构

这里写图片描述

0 0