简单的spring+dubbo提供和消费方

1.在dubbo的提供方配置spring-dubbo.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="hjy_consumer" />  
    <dubbo:registry address="zookeeper://192.168.200.153:2181" />  
    生成远程服务代理,可以像使用本地bean一样使用demoService  
    <dubbo:reference id="testService"  interface="service.TestService"/> -->
    
    <!-- 提供方应用信息,用于计算依赖关系 -->
    <dubbo:application name="hehe_provider" />
    <!-- 使用zookeeper注册中心暴露服务地址   端口是zookeeper 中配置的2181-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- 具体的实现bean -->
    <bean id="userService" class="com.shenqz.service.impl.UserServiceImpl" />
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="service.UserService" ref="userService" />
</beans>  

2.service

package service;

import java.util.List;
import java.util.Map;

import com.shenqz.entity.Role;
import com.shenqz.entity.User;

/**
 * Hello world!
 *
 */
public interface UserService 
{
    public List<User> dirUserList();
    public User getUserByNameAndPassword(User user);
    public List<User> getUserByNameLike(String username);
    public User getUserByName(String username);
    //dubbo
    public String sayHello(String name);
    public List<Role> getRoles();
}

3.实现类

package com.shenqz.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import service.UserService;

import com.shenqz.entity.Role;
import com.shenqz.entity.User;
import com.shenqz.mapper.UserMapper;
@Service(value="userService")
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    

    @Override
    public User getUserByNameAndPassword(User user) {
        // TODO Auto-generated method stub
        return userMapper.getUserByNameAndPassword(user);
    }



    @Override
    public List<User> dirUserList() {
        // TODO Auto-generated method stub
        return userMapper.dirUserList();
    }



    @Override
    public List<User> getUserByNameLike(String username) {
        // TODO Auto-generated method stub
        return userMapper.getUserByNameLike(username);
    }


    /**
     * dubbo
     */
    @Override
    public String sayHello(String name) {
        System.out.println("hello world----------------------------");
        
        return name+"说:hello world";
    }



    @Override
    public User getUserByName(String username) {
        // TODO Auto-generated method stub
        return userMapper.getUserByName(username);
    }



    @Override
    public List<Role> getRoles() {
        // TODO Auto-generated method stub
        return userMapper.getRoles();
    }



    
    

}

4.消费方spring-dubbo.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="hjy_consumer" />  
    <dubbo:registry address="zookeeper://192.168.1.122:2181" />  
   <!--  生成远程服务代理,可以像使用本地bean一样使用demoService   -->
    <dubbo:reference id="userService"  interface="service.UserService"/>
   
</beans>  

 5.消费方的service

package service;

public interface UserService {
    public String sayHello(String name);
}

6.在消费方编写测试类

package test;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.UserService;

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(  
                new String[] { "spring/applicationContext.xml" });  
        context.start();  
  
        UserService TestService = (UserService) context.getBean("userService");  
        String hello = TestService.sayHello("xiaohei");  
        System.out.println(hello);  
    }
}

7.搭建注册中心zookeeper,具体步骤参照以下链接

http://blog.csdn.net/evankaka/article/details/47858707


8.启动zookeeper,运行测试类控制台输出结果:xiaohei说:hello world

原文地址:https://www.cnblogs.com/shenqz/p/8065557.html