Dubbo-模拟实现cart购物车

[root@localhost bin]# ifconfig
eth3      Link encap:Ethernet  HWaddr 00:0C:29:B4:EF:91  
          inet addr:192.168.161.136  Bcast:192.168.161.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:feb4:ef91/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:148484 errors:0 dropped:0 overruns:0 frame:0
          TX packets:40261 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:208627623 (198.9 MiB)  TX bytes:3475164 (3.3 MiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:346 errors:0 dropped:0 overruns:0 frame:0
          TX packets:346 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:28588 (27.9 KiB)  TX bytes:28588 (27.9 KiB)

package cart.service;

public interface CartService {
    public String findCartById(Long userId);
}

创建dubboProvider1项目

1创建mavenproject,quick start
2依赖cartInterface
3添加dubbo,zookeeper依赖
4写实现类
5为eclipse添加xsd文件,写xml时有自动提示
6添加提供者配置文件applicationContext-provider.xml
7通过spring框架启动dubbo提供者
8在zk客户端中看到提供者
9.在dubbo监控中看到提供者



<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.cart</groupId>
    <artifactId>dubboProvider1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>dubboProvider1</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.cart</groupId>
            <artifactId>cartInterface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>
</project>
<?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-2.5.xsd
    http://code.alibabatech.com/schema/dubbo 
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--微服务提供者配置文件,作用是把提供者的端口,实现类发给服务注册中心zookeeper-->
    <!--1.设置应用(微服务)名称  -->
    <dubbo:application name="provider                           -of-cart"/>
    <!--2.设置zookeeper的ip地址和端口号  -->
    <dubbo:registry address="zookeeper://192.168.161.136:2181" timeout="10000"/>
    <!--3.设置微服务的端口号  -->
    <dubbo:protocol port="20883" name="dubbo"/>
    <!--4.设置实现类  -->
    <bean class="cart.service.CartServiceImpl" id="CartService"/>
    <!--5.设置客户端访问服务的地址,地址是接口名,像servlet注册 -->
    <dubbo:service interface="cart.service.CartService" ref="CartService" timeout="10000"/>
</beans>
[zk: localhost:2181(CONNECTED) 0] ls /
[dubbo, zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /dubbo
[cart.service.CartService]
[zk: localhost:2181(CONNECTED) 2] ls /dubbo/cart.service.CartService/providers
[dubbo%3A%2F%2F176.114.22.47%3A20883%2Fcart.service.CartService%3Fanyhost%3Dtrue%26application%3Dprovider1-of-cart%26dubbo%3D2.8.4%26generic%3Dfalse%26interface%3Dcart.service.CartService%26methods%3DfindCartById%26pid%3D6520%26side%3Dprovider%26timestamp%3D1540436777370]
[zk: localhost:2181(CONNECTED) 3] quit
Quitting...
2018-10-24 20:19:00,195 [myid:] - INFO  [main:ZooKeeper@684] - Session: 0x166a552de6c000f closed
2018-10-24 20:19:00,197 [myid:] - INFO  [main-EventThread:ClientCnxn$EventThread@519] - EventThread shut down for session: 0x166a552de6c000f

创建dubboProvider2项目

<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.cart</groupId>
  <artifactId>dubboProvider2</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>dubboProvider2</name>
  <url>http://maven.apache.org</url>

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.cart</groupId>
            <artifactId>cartInterface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dubbo</artifactId>
            <version>2.8.4</version>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
    </dependencies>
</project>
<?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-2.5.xsd
    http://code.alibabatech.com/schema/dubbo 
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--微服务提供者配置文件,作用是把提供者的端口,实现类发给服务注册中心zookeeper-->
    <!--1.设置应用(微服务)名称  -->
    <dubbo:application name="provider-of-cart"/>
    <!--2.设置zookeeper的ip地址和端口号  -->
    <dubbo:registry address="zookeeper://192.168.161.136:2181" timeout="10000"/>
    <!--3.设置微服务的端口号  -->
    <dubbo:protocol port="20884" name="dubbo"/>
    <!--4.设置实现类  -->
    <bean class="cart.service.CartServiceImpl" id="CartService"/>
    <!--5.设置客户端访问服务的地址,地址是接口名,像servlet注册 -->
    <dubbo:service interface="cart.service.CartService" ref="CartService" timeout="10000"/>
</beans>
package cart;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        //启动spring框架,也就是启动了提供者
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-provider.xml");
        context.start();
        System.out.println("启动提供者2...");
        while(true)
        {
            
        }
    }
}
package cart.service;

public class CartServiceImpl implements CartService {

    @Override
    public String findCartById(Long userId) {
        System.out.println("CartServiceImpl.findCartById()-P2");
        return "第二个提供者:cart";
    }

}
[zk: localhost:2181(CONNECTED) 0] ls /dubbo
[cart.service.CartService]
[zk: localhost:2181(CONNECTED) 1] ls /dubbo/cart.service.CartService/providers
[dubbo%3A%2F%2F176.114.22.47%3A20883%2Fcart.service.CartService%3Fanyhost%3Dtrue%26application%3Dprovider1-of-cart%26dubbo%3D2.8.4%26generic%3Dfalse%26interface%3Dcart.service.CartService%26methods%3DfindCartById%26pid%3D6520%26side%3Dprovider%26timestamp%3D1540436777370, dubbo%3A%2F%2F176.114.22.47%3A20884%2Fcart.service.CartService%3Fanyhost%3Dtrue%26application%3Dprovider2-of-cart%26dubbo%3D2.8.4%26generic%3Dfalse%26interface%3Dcart.service.CartService%26methods%3DfindCartById%26pid%3D6512%26side%3Dprovider%26timeout%3D10000%26timestamp%3D1540438302201]
<?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-2.5.xsd
    http://code.alibabatech.com/schema/dubbo 
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
    <!--微服务提供者配置文件,作用是把提供者的端口,实现类发给服务注册中心zookeeper-->
    <!--1.设置应用(微服务)名称  -->
    <dubbo:application name="consumer-of-cart"/>
    <!--2.设置zookeeper的ip地址和端口号  -->
    <dubbo:registry address="zookeeper://192.168.161.136:2181" timeout="90000"/>
    <!--3.配置哪个接口的对象由dubbo管理 check=false 消费者启动时不会检查提供者是否启动-->
    <!--cartController @Autowired CartService cartservice-->
    <dubbo:reference interface="cart.service.CartService" id="cartService" timeout="90000" check="false"/>
</beans>
package com.cart.controller;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import cart.service.CartService;

public class CartController {
    public static void main(String[] args) {
        try {
            //启动spring框架
            ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext-consumer.xml");
            //从spring框架取cartService对象
            //以前是从spring容器中取对象,对象是当前项目接口实现类的对象
            //用来dubbo,加了<reference 指定了接口名CartService>标签
            //1,连接zookeeper,得到提供者信息
            //2,网络连接一个提供者,由提供者执行实现类,返回结果
            CartService cartService=(CartService) context.getBean("cartService");
            //调用提供者
            while(true)
            {
                System.out.println("===================");
                String cart=cartService.findCartById(90L);
                System.out.println("消费者:"+cart);
                Thread.currentThread();
                Thread.sleep(1000);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

运行结果

===================
消费者:第一个提供者:cart
===================
消费者:第二个提供者:cart
===================
消费者:第二个提供者:cart
===================
消费者:第一个提供者:cart
===================
消费者:第一个提供者:cart
===================
消费者:第二个提供者:cart
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容