dubbo简单环境搭建

原文链接: http://www.cnblogs.com/xpx951201/p/10225253.html

简单环境搭建

搭建工具

jdk1.8+idea+maven+spring

构建多模块

dubbo简单环境搭建_第1张图片

dubbo_par 父模块

  父模块中pom.xml引入dubbo坐标


com.alibaba
dubbo
2.5.6

dubbo_api 服务公共接口

  实体类 简单模拟数据库

public class User implements Serializable {
    private static final long serialVersionUID = -6267484068304517004L;

    private Integer userId;
    private String uname;
} 

  定义服务接口 

public interface IUserService {
  //通过id 查询用户 public User queryUserById(Integer userId); }

dubbo_provider 服务提供方

  pom.xml中引入公共服务接口坐标

 
 
       com.shsxt
       dubbo_demo_api
       1.0-SNAPSHOT
 

  

  配置provider.xml



    
    

    
    
    
    
    
    
    
    
    

  服务接口实现

@Service
public class UserServiceImpl implements IUserService {

    private Map users ;

    public UserServiceImpl() {
        users = new HashMap<>();
        users.put(1,new User(1,"dubbo"));
    }

    @Override
    public User queryUserById(Integer userId) {
        return users.get(userId);
    }
}

  测试提供服务方

public class Publisher {
    public static void main(String args[]) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
        context.start();
        System.in.read();
    }
}

dubbo_consumer 消费服务方

  pom.xml中引入公共服务接口坐标

 
 
       com.shsxt
       dubbo_demo_api
       1.0-SNAPSHOT
 

 

  配置consumer.xml



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


    
    
    
    
    
    interface="com.xpx.api.service.IUserService" id="userService">
    class="com.xpx.consumer.controller.UserController" id="userController">
        
    

  controller

public class UserController {
    private UserService userService;

//    没有配置自动扫描用set注入
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public User querUserById(Integer userId){
        return userService.queryUserById(userId);
    }
}

  测试

public class TestConsumer {
    public static void main(String args[]){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
        UserController userController = (UserController) applicationContext.getBean("userController");
        User user = userController.querUserById(1);
        System.out.println(user);
    }
}

  结果成功

User{id=1, uname='dubbo'}

 

转载于:https://www.cnblogs.com/xpx951201/p/10225253.html

你可能感兴趣的:(dubbo简单环境搭建)