分布式框架之:Dubbo + Zookeeper (二)

分布式框架之:Dubbo + Zookeeper (二),第1张

分布式框架之:Dubbo + Zookeeper (二) 分布式框架之:Dubbo + Zookeeper (二)

目录:
  • 分布式框架之:Dubbo + Zookeeper (二)
    • 1.建立聚合工程
          • dubbo-zookeeper [pom.xml]
          • dubbo-api [pom.xml]
          • dubbo-provider [pom.xml]
          • dubbo-customer [pom.xml]
    • 2.项目结构
          • application.yml
          • application.yml
    • 启动

1.建立聚合工程 dubbo-zookeeper [pom.xml]


    4.0.0
    
        dubbo-zookeeper
        com.dahuilang
        0.0.1-SNAPSHOT
    
    
    com.dahuilang
    dubbo-provider
    0.0.1-SNAPSHOT
    dubbo-provider
    Demo project for Spring Boot
    
        1.8
        3.4.13
        0.2.0
    

    


        
            org.springframework.boot
            spring-boot-starter
        


        
            com.alibaba.boot
            dubbo-spring-boot-starter
            ${dubbo.version}
        

        
            org.apache.zookeeper
            zookeeper
            ${zookeeper.version}
            
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    log4j
                    log4j
                
            
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
            com.dahuilang
            dubbo-api
            0.0.1-SNAPSHOT
            compile
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



dubbo-api [pom.xml]


    4.0.0
    
        dubbo-zookeeper
        com.dahuilang
        0.0.1-SNAPSHOT
    
    com.dahuilang
    dubbo-api
    0.0.1-SNAPSHOT
    dubbo-api
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




dubbo-provider [pom.xml]


    4.0.0
    
        dubbo-zookeeper
        com.dahuilang
        0.0.1-SNAPSHOT
    

    com.dahuilang
    dubbo-provider
    0.0.1-SNAPSHOT
    dubbo-provider
    Demo project for Spring Boot
    
        1.8
        3.4.13
        0.2.0
    

    


        
            org.springframework.boot
            spring-boot-starter
        


        
            com.alibaba.boot
            dubbo-spring-boot-starter
            ${dubbo.version}
        

        
            org.apache.zookeeper
            zookeeper
            ${zookeeper.version}
            
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    log4j
                    log4j
                
            
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
            com.dahuilang
            dubbo-api
            0.0.1-SNAPSHOT
            compile
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



dubbo-customer [pom.xml]


    4.0.0
    
        dubbo-zookeeper
        com.dahuilang
        0.0.1-SNAPSHOT
    
    com.dahuilang
    dubbo-customer
    0.0.1-SNAPSHOT
    dubbo-customer
    Demo project for Spring Boot
    
        1.8
        3.4.13
        0.2.0
    

    

        
            com.dahuilang
            dubbo-api
            0.0.1-SNAPSHOT
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.alibaba.boot
            dubbo-spring-boot-starter
            ${dubbo.version}
        

        
            org.apache.zookeeper
            zookeeper
            ${zookeeper.version}
            
                
                    org.slf4j
                    slf4j-log4j12
                
                
                    log4j
                    log4j
                
            
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2.项目结构


package com.dahuilang.dubboapi.service;

public interface HelloService {
    String sayHello(String name);
}

package com.dahuilang.dubboprovider.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.dahuilang.dubboapi.service.HelloService;

@Service // 注册服务
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

application.yml
server:
  port: 8091

dubbo:
  application:
    name: dubbo-provider
  protocol:
    name: dubbo
    port: 20880
  registry:
    address: zookeeper://127.0.0.1:2181

package com.dahuilang.dubbocustomer.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.dahuilang.dubboapi.service.HelloService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Reference //dubbo引用服务
    private HelloService helloService;

    @GetMapping("/sayHello")
    private String sayHello(@RequestParam String name){

        System.out.println("调用sayHello成功了..." + " name:" + name);

        return helloService.sayHello(name);
    }


}
application.yml
server:
  port: 8090

dubbo:
  application:
    name: dubbo-customer
  registry:
    address: zookeeper://127.0.0.1:2181
启动


http://localhost:8090/sayHello?name=dahuilang

[zk: localhost:2181(CONNECTED) 18] ls /dubbo
[com.dahuilang.dubbo.service.TestService, com.dahuilang.dubboapi.service.HelloService, mapping]

拓展
set path data [version] 命令 修改 *** 作.每次修改完以后版本号会变
delete path [version] 删除节点数据,可以带版本号,实现乐观锁机制

顺序获取

	
	@Override
    public void lock() {
        // 如果获取不到锁,阻塞等待
        if (!tryLock()) {
            // 没获得锁,阻塞自己
            waitForLock();
            // 再次尝试
            lock();
        }
    }

    // 尝试加锁
    @Override
    public boolean tryLock() {
        // 创建临时顺序节点
        if (this.currentPath == null) {
            // 在lockPath节点下面创建临时顺序节点
            currentPath = this.client.createEphemeralSequential(LockPath + "/", "lockpath");
        }
        // 获得所有的子节点
        List children = this.client.getChildren(LockPath);

        // 排序list
        Collections.sort(children);

        // 判断当前节点是否是最小的,如果是最小的节点,则表明此这个client可以获取锁
        if (currentPath.equals(LockPath + "/" + children.get(0))) {
            return true;    
        } else {
            // 如果不是当前最小的sequence,取到前一个临时节点
            // 1.单独获取临时节点的顺序号
            // 2.查找这个顺序号在children中的下标
            // 3.存储前一个节点的完整路径
            int curIndex = children.indexOf(currentPath.substring(LockPath.length() + 1));
            beforePath = LockPath + "/" + children.get(curIndex - 1);
        }
        return false;
    }
	
	//
    private void waitForLock() {
        CountDownLatch cdl = new CountDownLatch(1);
        // 注册watcher
        IZkDataListener listener = new IZkDataListener() {
            @Override
            public void handleDataDeleted(String dataPath) throws Exception {
                System.out.println("监听到前一个节点被删除了");
                cdl.countDown();
            }
            @Override
            public void handleDataChange(String dataPath, Object data) throws Exception {
            
			}
        };

        // 监听前一个临时节点
        client.subscribeDataChanges(this.beforePath, listener);

        // 前一个节点还存在,则阻塞自己
        if (this.client.exists(this.beforePath)) {
            try {
                // 直至前一个节点释放锁,才会继续往下执行
                cdl.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        // 醒来后,表明前一个临时节点已经被删除,此时客户端可以获取锁 && 取消watcher监听
        client.unsubscribeDataChanges(this.beforePath, listener);
    }


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5678852.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存