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监控中看到提供者




    4.0.0

    com.cart
    dubboProvider1
    0.0.1-SNAPSHOT
    jar

    dubboProvider1
    http://maven.apache.org

    
        UTF-8
    

    
        
            com.cart
            cartInterface
            0.0.1-SNAPSHOT
        
        
            com.alibaba
            dubbo
            2.8.4
        
        
            com.github.sgroschupf
            zkclient
            0.1
        
    



    
    
    
    
    
    
    
    
    
    
    

[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项目


  4.0.0

  com.cart
  dubboProvider2
  0.0.1-SNAPSHOT
  jar

  dubboProvider2
  http://maven.apache.org


        UTF-8
    

    
        
            com.cart
            cartInterface
            0.0.1-SNAPSHOT
        
        
            com.alibaba
            dubbo
            2.8.4
        
        
            com.github.sgroschupf
            zkclient
            0.1
        
    



    
    
    
    
    
    
    
    
    
    
    

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]


    
    
    
    
    
    
    
    

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,加了标签
            //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

你可能感兴趣的:(Dubbo-模拟实现cart购物车)