4、Dubbo-工程实践

4、实践测试

4.1)、提出需求

某个电商系统,订单服务需要调用用户服务获取某个用户的所有地址;
我们现在 需要创建两个服务模块进行测试 

测试预期结果:
订单服务web模块在A服务器,用户服务模块在B服务器,A可以远程调用B的功能。

 

TEST..........................

 接口以及需要用的bean进行提取为一个新的工程

 

UserAddress.java

public class UserAddress {
     private Integer id;
     private String userAddress;//地址
     private String userId;//用户id
     private String consignee;//收件人
     private String idDefault;//是否默认地址
     private String phoneNum;//电话
...
}

OrderService.java

//用户服务
public interface UserService {
     
     //按照用户id返回所有的收货地址
     public List<UserAddress> getUserAddressList(String  userId);
}
此时的工程只有bean实例和功能实现的接口
实现相关都在其他工程中进行实现
实现分离的目的
 

服务的提供者:

依赖关系的配置:

    <dependencies>
      <dependency>
              <groupId>com.cr</groupId>
                <artifactId>user-interface</artifactId>
              <version>1.0</version>
      </dependency>
  </dependencies>

UserServiceImpl.java

public class UserServiceImpl  implements UserService {
     @Override
     public List<UserAddress> getUserAddressList(String userId)  {
           UserAddress address1 = new UserAddress(1, "安徽合肥蜀山区", "2", "程老师", "", "12345");
           UserAddress address2 = new UserAddress(2, "安徽合肥包河区", "2", "程老师", "", "12345");
           List<UserAddress> list = new  ArrayList<UserAddress>();
           list.add(address2);
           list.add(address1);
           return list;
     }
}

服务消费者 

 

引入依赖:

  <dependencies>
      <dependency>
              <groupId>com.cr</groupId>
                <artifactId>user-interface</artifactId>
              <version>1.0</version>
      </dependency>
  </dependencies>
OrderServiceImpl.java
public class OrderServiceImpl implements OrderService{
     UserService userService;
     @Override
     public void initOrder(String userId) {
           
     
           //查询用户的收货地址
           List<UserAddress> addressList =  userService.getUserAddressList(userId);
           System.out.println(addressList);
     }
}

此时需要使用Dubbo进行远程调用消息服务者的接口进行实现功能!!!

 改造的步骤:

1.1.导入dubbo依赖(2.6.2)

 <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>dubbo</artifactId>
         <version>2.6.2</version>
     </dependency>
     
           <!-- 注册中使用的是zookeeper,需要引入操作zookeeper的客户端 -->
           <!-- 由于我们使用zookeeper作为注册中心,所以需要操作zookeeper
           dubbo 2.6以前的版本引入zkclient操作zookeeper
           dubbo 2.6及以后的版本引入curator操作zookeeper
           -->
     <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>2.12.0</version>
     </dependency>

1.2.配置服务提供者

<?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://dubbo.apache.org/schema/dubbo"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbohttp://dubbo.apache.org/schema/dubbo/dubbo.xsd">
              
     
     <!-- 1、指定当前服务/应用的名字(同样的服务名字,不要和别的服务同名 -->
     <dubbo:application  name="user-service-provider"></dubbo:application>         
            
     <!-- 2、指定注册中心的位置 -->    
     <!-- 两种写法 -->
     <!-- dubbo:registry protocol="zookeeper"  address="10.20.153.10:2181" -->   
     <dubbo:registry address="zookeeper://127.0.0.1:2181" />    
            
     <!-- 3、指定通信规则(通信协议&通信端口 -->     
     
     <dubbo:protocol name="dubbo" port="20080"></dubbo:protocol>       
     
     
     <!-- 4、暴露服务 -->   
     <!-- interface是只想接口,ref属性是指定接口的实现 -->   
     <dubbo:service interface="com.cr.service.UserService"  ref="userServiceImpl"></dubbo:service>      
     <!-- 服务的实现 -->
     <bean id="userServiceImpl"   class="com.cr.service.impl.UserServiceImpl"></bean>       
              
</beans>
注册中心的配置
详情可以参考官方的配置

http://dubbo.apache.org/zh-cn/docs/user/references/registry/zookeeper.html

1.3、启动服务

public class MainApplication {
     public static void main(String[] args) {
           
           ClassPathXmlApplicationContext ioc = new
                     ClassPathXmlApplicationContext("provider.xml");
           ioc.start();
           try {
                System.in.read();
           } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
           }
     }
}

此时可以在dubbo-admin管理控制台

 

2、让服务消费者这到注册中心订阅服务提供者的服务地址

 

<?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://dubbo.apache.org/schema/dubbo"
     xmlns:context="http://www.springframework.org/schema/context"
     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
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.3.xsd">
              
          <context:component-scan  base-package="com.cr.service.impl"></context:component-scan>
     <!-- 1、指定当前服务/应用的名字(同样的服务名字,不要和别的服务同名 -->
     <dubbo:application  name="user-order-consumer"></dubbo:application>         
            
     <!-- 2、指定注册中心的位置 -->    
     <dubbo:registry address="zookeeper://127.0.0.1:2181" />    
            
     <!-- 3、声明 需要调用的远程服务接口:生成远程服务代理-->
     <!-- userService注册到容器中 -->
     <dubbo:reference interface="com.cr.service.UserService"  id="userService"></dubbo:reference>
            
</beans>

OrderServiceImpl.java

//import org.springframework.stereotype.Service;
@Service
public class OrderServiceImpl implements OrderService{
     @Autowired
     UserService userService;
     @Override
     public void initOrder(String userId) {
           
           System.out.println(userService);
           System.out.println("用户id:"+ userId);
           //查询用户的收货地址
           List<UserAddress> addressList =  userService.getUserAddressList(userId);
           
           for(UserAddress address : addressList){
                System.out.println(address.getUserAddress());
           }
     }
}

此时的 UserService userService 是注册中心中找到

MainApplication.java
public class MainApplication {
     public static void main(String[] args) {
           ClassPathXmlApplicationContext app = new
                     ClassPathXmlApplicationContext("consumer.xml");
           
           OrderService service =   app.getBean(OrderService.class);
           
           service.initOrder("2");
     
           System.out.println("调用结束...");
           try {
                System.in.read();
           } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
           }
     }
}

此时运行得到的结果如下:

com.alibaba.dubbo.common.bytecode.proxy0@1c39680d
用户id:2
安徽合肥包河区
安徽合肥蜀山区
调用结束...

 

 

原文地址:https://www.cnblogs.com/Mrchengs/p/10473412.html