dubbo 从入门到精通 服务注册与调用(四)

1、公共模块(声明公共接口、实体类)

一、工程目录

二、UserAddress.java

package com.atguigu.gmall.bean;

import java.io.Serializable;

/**
 * 用户地址 
 *
 */
public class UserAddress implements Serializable {
    
    private Integer id;
    private String userAddress; //用户地址
    private String userId; //用户id
    private String consignee; //收货人
    private String phoneNum; //电话号码
    private String isDefault; //是否为默认地址    Y-是     N-否
    
    public UserAddress() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public UserAddress(Integer id, String userAddress, String userId, String consignee, String phoneNum,
            String isDefault) {
        super();
        this.id = id;
        this.userAddress = userAddress;
        this.userId = userId;
        this.consignee = consignee;
        this.phoneNum = phoneNum;
        this.isDefault = isDefault;
    }
    
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getUserAddress() {
        return userAddress;
    }
    public void setUserAddress(String userAddress) {
        this.userAddress = userAddress;
    }
    public String getUserId() {
        return userId;
    }
    public void setUserId(String userId) {
        this.userId = userId;
    }
    public String getConsignee() {
        return consignee;
    }
    public void setConsignee(String consignee) {
        this.consignee = consignee;
    }
    public String getPhoneNum() {
        return phoneNum;
    }
    public void setPhoneNum(String phoneNum) {
        this.phoneNum = phoneNum;
    }
    public String getIsDefault() {
        return isDefault;
    }
    public void setIsDefault(String isDefault) {
        this.isDefault = isDefault;
    }
    
    


}

三、OrderService.java(消费者接口)

package com.atguigu.gmall.service;

import java.util.List;

import com.atguigu.gmall.bean.UserAddress;

public interface OrderService {
    

    public void initOrder(String userId);

}

四、UserService.java(提供者接口)

package com.atguigu.gmall.service;

import java.util.List;

import com.atguigu.gmall.bean.UserAddress;

public interface UserService {
    
    /**
     * 按照用户id返回所有的收货地址
     * @param userId
     * @return
     */
    public List<UserAddress> getUserAddressList(String userId);

}

五、pom.xml(没有导入依赖包)

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.atguigu.gmall</groupId>
  <artifactId>gmall-interface</artifactId>
  <version>0.0.1-SNAPSHOT</version>
</project>

2、服务提供者

一、工程目录

二、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.atguigu.gmall</groupId>
  <artifactId>order-service-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <dependencies>
  <!-- 公共依赖 -->
   <dependency>
       <groupId>com.atguigu.gmall</groupId>
       <artifactId>gmall-interface</artifactId>
       <version>0.0.1-SNAPSHOT</version>
   </dependency>
  <!-- dubbo 框架 -->
      <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.2</version>
     </dependency>
      <!-- 操作zookeeper  -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>
</project>

三、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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!-- 提供方应用信息,用于计算依赖关系  -->
    <!-- 1、指定当前服务/应用的名字(同样的服务名字相同,不要和别的服务同名)  -->
    <dubbo:application name="user-service-provider"  />
 
    <!--指定注册中心的地址  -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <!-- 用dubbo协议在20880端口暴露服务 -->
    <dubbo:protocol name="dubbo" port="20880" />
 
    <!-- 声明需要暴露的服务接口 -->
    <dubbo:service interface="com.atguigu.gmall.service.UserService" ref="userServiceImpl" />
 
    <!-- 和本地bean一样实现服务 -->
    <bean id="userServiceImpl" class="com.atguigu.gmall.service.impl.UserServiceImpl" />
    
</beans>

四、UserServiceImpl.java

package com.atguigu.gmall.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.UserService;

public class UserServiceImpl implements UserService {

    public List<UserAddress> getUserAddressList(String userId) {
        UserAddress address1 = new UserAddress(1, "湖南省长沙市岳麓区", "1", "李同学", "010-56253825", "Y");
        UserAddress address2 = new UserAddress(2, "湖南省常德市武陵区", "1", "王同学", "010-56253825", "N");        

        List<UserAddress> list=new ArrayList<>();
        
        list.add(address1);
        list.add(address2);

       return list;
    }

}

五、启动类(MainApplication.java)

package com.atguigu.gmall;

import java.io.IOException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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

3、服务消费者

一、工程目录

二、pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.atguigu.gmall</groupId>
  <artifactId>order-service-consumer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  
  <dependencies>
  <!-- 公共依赖 -->
   <dependency>
       <groupId>com.atguigu.gmall</groupId>
       <artifactId>gmall-interface</artifactId>
       <version>0.0.1-SNAPSHOT</version>
   </dependency>
  <!-- dubbo 框架 -->
      <dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
    <version>2.6.2</version>
     </dependency>
      <!-- 操作zookeeper  -->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>2.12.0</version>
        </dependency>
    </dependencies>
</project>

三、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://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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <context:component-scan base-package="com.atguigu.gmall.service.Impl"></context:component-scan>


    <dubbo:application name="order-service-consumer"></dubbo:application>
    
    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
    
    <!--  配置本地存根-->
    
    <!--声明需要调用的远程服务的接口;生成远程服务代理  -->
    <!-- 
        1)、精确优先 (方法级优先,接口级次之,全局配置再次之)
        2)、消费者设置优先(如果级别一样,则消费方优先,提供方次之)
    -->
    <dubbo:reference interface="com.atguigu.gmall.service.UserService" 
        id="userService" >
        <!-- <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method> -->
    </dubbo:reference>
        
    <!-- 配置当前消费者的统一规则:所有的服务都不检查 -->
    <dubbo:consumer check="false" timeout="5000"></dubbo:consumer>

    
    <!-- <dubbo:monitor address="127.0.0.1:7070"></dubbo:monitor> -->
    
</beans>

四、OrderServiceImpl.java

package com.atguigu.gmall.service.Impl;

import java.util.List;

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

import com.atguigu.gmall.bean.UserAddress;
import com.atguigu.gmall.service.OrderService;
import com.atguigu.gmall.service.UserService;

@Service
public class OrderServiceImpl implements OrderService{
    
    @Autowired
    UserService userService;
    

    public void initOrder(String userId) {
        

        System.out.println("用户id:"+userId);
        userService.getUserAddressList(userId)
                   .stream()
                   .forEach((o)->{
                     System.out.println(o.getUserAddress());
                   });
    }



}

五、启动类(MainApplication.java)

package com.atguigu.gmall;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.atguigu.gmall.service.OrderService;

public class MainApplication {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("consumer.xml");
        OrderService o=  (OrderService) ioc.getBean(OrderService.class);
        o.initOrder("200002");
        System.out.println("========调用完成===================");
        System.in.read();
    }
    
}

4、测试

一、启动服务提供者

二、进入dubbo管理控制台查看 

三、开启消费者消费

三、进入dubbo管理控制台查看 

 

 

 

 

posted @ 2021-06-13 13:26  KwFruit  阅读(137)  评论(0编辑  收藏  举报