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

Dubbo服务者消费者提供者案例实现

时间:2019-05-31 19:54:10      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:tun   实现   使用   客户   自动   协议   frame   部件   jar包   

Dubbo介绍

Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

核心部件功能

Remoting: 网络通信框架,实现了 sync-over-async 和
 request-response 消息机制.
RPC: 一个远程过程调用的抽象,支持负载均衡容灾集群功能
Registry: 服务目录框架用于服务的注册和服务事件发布和订阅
 
工作原理
技术图片
 

特性 

  • 面向接口代理的高性能RPC调用
    提供高性能的基于代理的远程调用能力,服务以接口为粒度,为开发者屏蔽远程调用底层细节。
  • 智能负载均衡
    内置多种负载均衡策略,智能感知下游节点健康状况,显著减少调用延迟,提高系统吞吐量。
  • 服务自动注册与发现
    支持多种注册中心服务,服务实例上下线实时感知。
  • 高度可扩展能力
    遵循微内核+插件的设计原则,所有核心能力如Protocol、Transport、Serialization被设计为扩展点,平等对待内置实现和第三方实现。
  • 运行期流量调度
    内置条件、脚本等路由策略,通过配置不同的路由规则,轻松实现灰度发布,同机房优先等功能。
  • 可视化的服务治理与运维
    提供丰富服务治理、运维工具:随时查询服务元数据、服务健康状态及调用统计,实时下发路由策略、调整配置参数。

案例:

首先下载zookeeper(下载地址https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/)解压到本地。启动Zookeeper注册中心,在解压后zookeeper-3.4.11文件夹bin下启动cmd,输入zkServer.cmd

技术图片

若启动注册中心失败,则需要修改conf目录下zoo.cfg文件,如图。

技术图片

启动dubbo监控中心,通过java -jar jar包名命令启动。jar包下载地址:链接:https://pan.baidu.com/s/1m_4ktW19muxPKWpdKbufVQ 提取码:xy35 

技术图片

通过浏览器访问监控中心,访问时要输入用户名:root与密码:root:

技术图片

1.新建maven项目user-service-gmall,该项目包含了bean类、与接口(OrderService、UserService)

技术图片

 

UserAddress类:

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接口:

public interface OrderService {
    
    /**
     * 初始化订单
     * @param userId
     */
    public List<UserAddress> initOrder(String userId);

}

UserService接口:

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

}

 

2.创建user-service-provider01项目

技术图片

pom文件中添加maven依赖:添加user-service-gmall项目依赖

<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.moon</groupId> <artifactId>user-service-provider01</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.moon</groupId> <artifactId>user-service-gmall</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency> </dependencies> </project>

UserServiceImpl类

public class UserServiceImpl implements UserService {
    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        // TODO Auto-generated method stub
        System.out.println("UserServiceImpl..3.....");
        UserAddress address1 = new UserAddress(1, "陕西省西安市", "1", "dd", "010-56253825", "Y");
        UserAddress address2 = new UserAddress(2, "陕西省咸阳市", "1", "dd", "010-56253825", "N");
        return Arrays.asList(address1,address2);
    }
}

RunClass类

public class MainClass {
    public static void main(String[] args) throws Exception{
        System.out.println("开启服务");
        ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("provider.xml");
        ioc.start();    
        System.in.read();
    }
}

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-provider01"></dubbo:application>
    <!-- 2、指定注册中心的位置 -->
    <!-- <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> -->
    <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
    
    <!-- 3、指定通信规则(通信协议?通信端口) -->
    <dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
    
    
    <bean id="userServiceImpl" class="com.moon.service.UserServiceImpl"></bean>
    <!-- 4、暴露服务   ref:指向服务的真正的实现对象 -->
    <dubbo:service interface="com.moon.user_service_gmall.service.UserService" 
        ref="userServiceImpl" timeout="1000" version="2.0.0">
        <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method>
    
    </dubbo:service>

</beans>

3.创建user-service-consumer项目

技术图片

OrderServiceImpl类

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    UserService userService;
    public List<UserAddress> initOrder(String userId) {
        // TODO Auto-generated method stub
        List<UserAddress> addressList = userService.getUserAddressList(userId);
        for (UserAddress userAddress : addressList) {
            System.out.println(userAddress.getUserAddress()+"---"+userAddress.getId());
        }
        return addressList;
    }
    
}

 

启动类MainClass

@SuppressWarnings("resource")
public class MainClass {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext("consumer.xml");
        c.start();

        OrderService orderService = (OrderService) c.getBean(OrderService.class);
        
        orderService.initOrder("1");
        System.out.println("调用完成....");
        try {
            System.in.read();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

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.moon.service"></context:component-scan> 
    <dubbo:application name="order-service-consumer"></dubbo:application>
    
    <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
    
    <!-- timeout="0" 默认是1000ms-->
    <!-- retries="":重试次数,不包含第一次调用,0代表不重试-->
    <!-- 幂等(设置重试次数)【查询、删除、修改】、非幂等(不能设置重试次数)【新增】 -->
    <dubbo:reference interface="com.moon.user_service_gmall.service.UserService" <!--在user-service-provider中暴露的接口服务 -->
        id="userService" timeout="5000" retries="3" version="*">
        <!-- <dubbo:method name="getUserAddressList" timeout="1000"></dubbo:method> -->
    </dubbo:reference>
</beans>

4.启动服务提供者与消费者,即启动user-service-provider01与user-service-consumer中MainClass中的main方法(先启动服务提供者)。

技术图片

技术图片

访问Dubbo后台监控系统,如图。

技术图片

 

Dubbo服务者消费者提供者案例实现

标签:tun   实现   使用   客户   自动   协议   frame   部件   jar包   

原文地址:https://www.cnblogs.com/menbo/p/10956900.html

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