• Dubbo学习-4-dubbo简单案例-2-服务提供者和消费者配置


     在上一篇帖子的基础上,开始使用dubbo来实现RPC调用:

    根据dubbo的架构图可知,需要做以下几件事情:

    1.将服务提供者注册到注册中心(暴露服务) 

      (1)引入dubbo依赖, 这里依赖2.6.2版本(版本如果使用zookeeper作为注册中心,那么对应的客户端是curator,不是原来的zkClient)

      (2)注册中心使用的是zookeeper,需要引入操作zookeeper的客户端  2.6.以上版本的dubbo,如果使用zookeeper作为注册中心,那么注册中心客户端使用的是curator,2.6版本之前的dubbo,是使用zkClient操作zookeeper注册中心

    (3)配置服务提供者:在src/main/resources目录下创建一个provider.xml文件:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
     6  
     7     <!-- 1.提供方应用信息,用于计算依赖关系;name属性用来指定当前服务/应用的名字,使用工程名即可 -->
     8     <dubbo:application name="user-service-provider"  />
     9  
    10     <!-- 2.指定注册中心的地址:使用zookeeper作为注册中心暴露服务地址  下面两种方式都可以-->
    11     <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181" /> -->
    12     <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" />
    13  
    14     <!--3.指定通信规则: 用dubbo协议在20880端口暴露服务 即服务的调用者和服务的提供者之间使用端口20880进行通信 -->
    15     <dubbo:protocol name="dubbo" port="20880" />
    16  
    17     <!--4.暴露服务dubbo:service    声明需要暴露的服务接口  ref:指向服务的真正实现对象-->
    18     <dubbo:service interface="com.lch.test.service.UserService" ref="userService" />
    19  
    20     <!-- 和本地bean一样实现服务 -->
    21     <bean id="userService" class="com.lch.test.service.impl.UserServiceImpl" />
    22 </beans>

    (4)测试服务提供者:

    还可以在dubbo管理控制台查看服务提供者的信息:(先要启动dubbo监控中心)

    2.让服务的消费者去注册中心订阅服务提供者的服务地址

    (1)order-service-consumer工程引入dubbo依赖:在order-service-consumer工程的pom文件中添加依赖:

    (2)配置服务的消费者:在order-service-consumer工程的resources目录下创建一个consumer.xml文件,添加如下内容:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
     6  
     7     <!-- 1.消费方应用名-->
     8     <dubbo:application name="order-service-consumer" />
     9  
    10     <!-- 2.指定注册中心地址 -->
    11     <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
    12  
    13     <!-- 生成远程服务代理dubbo:reference:声明需要调用的远程服务的接口 -->
    14     <!-- user-service-provider工程里面暴露了一个名为userService的服务,这里要引用这个服务-->
    15     <dubbo:reference id="userService" interface="com.lch.test.service.UserService" />
    16 </beans>

    3. 服务消费者的实现:

    (1)在上一步,order-service-consumer通过dubbo引用了服务提供者暴露的接口userService,那么在orderService的实现类中,就可以使用Spring的注解来注入userService这个bean:

     1 package com.lch.test.service.impl;
     2 
     3 import java.util.List;
     4 
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Service;
     7 
     8 import com.lch.test.bean.UserAddress;
     9 import com.lch.test.service.OrderService;
    10 import com.lch.test.service.UserService;
    11 
    12 /**
    13  * 1.将服务提供者注册到注册中心 2.让服务消费者去注册中心订阅服务提供者的服务地址
    14  * 
    15  * @author
    16  */
    17 @Service // 这里暂时使用spring的注解
    18 public class OrderServiceImpl implements OrderService {
    19 
    20     /*
    21      * 这里dubbo工程里面只是引入了该接口,而该接口的实现在其他工程里面, 这里就需要远程过程调用才能获取到该接口的实现
    22      */
    23     @Autowired
    24     UserService userService;
    25 
    26     public void initOrder(String userId) {
    27         System.out.println("用户id=" + userId);
    28         // 调用userService 获取用户收货地址
    29         List<UserAddress> addressList = userService.getUserAddressList(userId);
    30         addressList.forEach(address -> {
    31             System.out.println(address);
    32         });
    33     }
    34 
    35 }

    这里使用了注解,consumer.xml配置文件中需要扫描包里的注解:

    (2)服务的消费者测试:

     1 package com.lch.test;
     2 
     3 import java.io.IOException;
     4 
     5 import org.springframework.context.support.ClassPathXmlApplicationContext;
     6 
     7 import com.lch.test.service.OrderService;
     8 
     9 public class MainApplication {
    10 
    11     public static void main(String[] args) throws IOException {
    12         ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:consumer.xml");
    13         OrderService orderService = context.getBean(OrderService.class);
    14         orderService.initOrder("1");
    15         System.out.println("调用完成");
    16         System.in.read();
    17     }
    18 }

    调用结果:

    在dubbo管理控制台查看消费者:

  • 相关阅读:
    实现控件的拖拽
    自定义控件——安卓旋转动画
    MD5简单实例
    TextView来实现跑马灯的效果
    Intent的简单使用
    SharedPreferences的封装
    ViewPager+fragment的使用
    安卓定时器
    2020重新出发,MySql基础,MySql视图&索引&存储过程&触发器
    2020重新出发,MySql基础,MySql表数据操作
  • 原文地址:https://www.cnblogs.com/enjoyjava/p/11184208.html
Copyright © 2020-2023  润新知