在resources 目录下新建dubbo 包,在dubbo包中,新建两个xml,一个是提供者provider.xml ,另一个是消费者consumer.xml。在pom.xml文件中添加入maven依赖

在pom.xml加入


<dependency> 
<groupId>com.101tec</groupId>


<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>

zookeeper需要在本地安装好并启动,端口号是2181,dubbo端口号是20880

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
        ">

    <!-- 提供方应用名,用于计算依赖关系,标记这个接口所在应用的名称 -->
    <dubbo:application name="hello-world-app" />

    <!-- 使用zookeeper注册中心暴露服务地址 -->
    <!--<dubbo:registry address="multicast://224.5.6.7:1234" />-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />

    <!-- 使用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service group="jd" version="1.0" timeout="2000" interface="com.jd.service.DubboTestService" ref="demoService" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="demoService" class="com.jd.service.impl.DubboTestServiceImpl" />

    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.jd.service.RegisterService" ref="registerService" />

    <!-- 和本地bean一样实现服务 -->
    <bean id="registerService" class="com.jd.service.impl.RegisterServiceImpl" />

</beans>

consumer.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="dubooTestDemo" group="jd" version="1.0" timeout="2000" interface="com.jd.service.DubboTestService"/>
    <dubbo:reference id="registerService" interface="com.jd.service.RegisterService"/>
</beans>

然后在

在service 包下,新建两个接口类为,DubboTestService,RegisterService,在Impl包下面新建类实现那两个接口DubboTestServiceImpl,RegisterServiceImpl


 ,

package com.jd.service;

/**
 * Created by Administrator on 2017/8/30 0030.
 */
import com.jd.domain.ResponseMessage;
import com.jd.domain.User;

import java.io.IOException;

public abstract interface DubboTestService {
    public abstract void say();

    public abstract String getName();

    public abstract String sayHello(String s);

    public abstract ResponseMessage getUserById(Integer id);

    public abstract ResponseMessage addUser(User user);

    public abstract String getSupportCity(String city) throws IOException;
}


package com.jd.service;

import com.jd.domain.User;

/**
 * Created by Administrator on 2017/9/2 0002.
 */
public interface RegisterService {
    public void registerUser(User user);
}

package com.jd.service.impl;

/**
 * Created by Administrator on 2017/8/30 0030.
 */
import com.jd.domain.ResponseMessage;
import com.jd.domain.User;
import com.jd.service.DubboTestService;
import com.jd.util.HttpClientUtil;
import org.apache.commons.httpclient.HttpClient;

import java.io.IOException;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;

public class DubboTestServiceImpl implements DubboTestService {
    @Override
    public String getSupportCity(String city) throws IOException {
        String url = "";
        String param = "";

        Map<String,String>baseParam = new HashMap<String, String>();
        baseParam.put("url",url);
        baseParam.put("methodName","post");
        baseParam.put("paramBody",param);

        Map<String,String>headerMap = new HashMap<String, String>();
        headerMap.put("Context-Type","text/xml");
        Map<String,String> returnMap = HttpClientUtil.request(baseParam,headerMap);

        String returnBody = returnMap.get("returnBody");
        return returnBody;

    }
    @Override
    public void say() {
        System.out.println("hello");
    }
    @Override
    public String sayHello(String s){
     return s;
    }
    @Override
    public String getName() {
        return "niujinliang";
    }
    @Override
    public ResponseMessage addUser(User user) {
        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setSuccess(true);
        responseMessage.setCode(200);
        responseMessage.setMsg("添加用户成功!");
        return responseMessage;
    }
    @Override
    public ResponseMessage getUserById(Integer id) {
        return getUser(id);
    }

    public ResponseMessage getUser(Integer id) {
        ResponseMessage responseMessage = new ResponseMessage();
        User user = new User();
        if (id == null) {
            responseMessage.setCode(400);
            responseMessage.setSuccess(false);
            responseMessage.setMsg("入参不能为空!");
            return responseMessage;
        }
        switch (id.intValue()) {
            case 0:
                user.setUsername("京");
                user.setEmail("test@j.com");
                break;
            case 1:
                user.setUsername("小强");
                user.setEmail("test@j.com");
                break;
            case 2:
                user.setUsername("测试");
                user.setEmail("test@j.com");
        }

        if (user == null) {
            responseMessage.setCode(400);
            responseMessage.setSuccess(false);
            responseMessage.setMsg("未查询到用户");
        } else {
            responseMessage.setCode(200);
            responseMessage.setSuccess(true);
            responseMessage.setObject(user);
        }
        return responseMessage;
    }



}

package com.jd.service.impl;

import com.alibaba.fastjson.JSON;
import com.jd.domain.User;
import com.jd.service.RegisterService;

/**
 * Created by Administrator on 2017/9/2 0002.
 */
public class RegisterServiceImpl implements RegisterService {
    @Override
    public void registerUser(User user) {
    System.out.print("用户注册成功了:"+JSON.toJSON(user));
    }
}


服务端完成,然后启动服务




Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐