Dubbo环境搭建之二 创建服务

前提条件 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hassen2010

你的鼓励我能输出跟多的好文章

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值