码迷,mamicode.com
首页 > 其他好文 > 详细

dubbo简单环境搭建

时间:2019-01-06 15:00:04      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:exce   图片   通过   imp   hash   scan   5.6   package   ext   

简单环境搭建

搭建工具

jdk1.8+idea+maven+spring

构建多模块

技术分享图片

dubbo_par 父模块

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

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.6</version>
</dependency>

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中引入公共服务接口坐标

 <!--引入公共接口坐标-->
 <dependency>
       <groupId>com.shsxt</groupId>
       <artifactId>dubbo_demo_api</artifactId>
       <version>1.0-SNAPSHOT</version>
 </dependency>

  

  配置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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       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
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!--自动扫描-->
    <context:component-scan base-package="com.xpx.provider"></context:component-scan>

    <!--dubbo配置-->
    <!--配置名称-->
    <dubbo:application name="dubbo_provider"></dubbo:application>
    <!--multicast 注册中心 address="multicast://224.5.6.7:1234" 广播地址 接受广播来实现服务注册-->
    <dubbo:registry address="multicast://224.5.6.7:1234"></dubbo:registry>
    <!--配置协议 20880dubbo的默认端口-->
    <dubbo:protocol name="dubbo" port="20880"></dubbo:protocol>
    <!--配置注册服务-->
    <dubbo:service interface="com.xpx.api.service.IUserService" ref="userServiceImpl"></dubbo:service>
</beans>

  服务接口实现

@Service
public class UserServiceImpl implements IUserService {

    private Map<Integer,User> 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中引入公共服务接口坐标

 <!--引入公共接口坐标-->
 <dependency>
       <groupId>com.shsxt</groupId>
       <artifactId>dubbo_demo_api</artifactId>
       <version>1.0-SNAPSHOT</version>
 </dependency>

 

  配置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应用名称-->
    <dubbo:application name="dubbo_consumer"></dubbo:application>
    <!--注册中心 address="multicast://224.5.6.7:1234" 广播地址 接受广播来实现服务注册-->
    <dubbo:registry address="multicast://224.5.6.7:1234"></dubbo:registry>
    <!--配置订阅的服务-->
    <dubbo:reference interface="com.xpx.api.service.IUserService" id="userService"></dubbo:reference>
    <bean class="com.xpx.consumer.controller.UserController" id="userController">
        <property name="userService" ref="userService"></property>
    </bean>
</beans>

  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‘}

 

dubbo简单环境搭建

标签:exce   图片   通过   imp   hash   scan   5.6   package   ext   

原文地址:https://www.cnblogs.com/xpx951201/p/10225253.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!