会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132600个问题

老师,一直报找不到这个结点,但是结点确实是存在的呀

UsersService

package com.bjsxt.service;

import java.rmi.RemoteException;

public interface UsersService {
    String findUsers(String str);
}

ClitentDemo

package com.bjsxt;

import com.bjsxt.service.UsersService;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;
import java.rmi.Naming;
import java.rmi.NotBoundException;

public class ClientDemo implements Watcher {
    public static void main(String[] args) throws IOException, KeeperException, InterruptedException, NotBoundException {
        //链接Zookeeper
        ZooKeeper zooKeeper = new ZooKeeper("192.168.88.101:2181,192.168.88.101:2182,192.168.88.101:2183", 150000, new ClientDemo());
        //将Zookeeper当成注册中心从来里面取出来的
        byte[] bytes = zooKeeper.getData("/bjsxt/service", new ClientDemo(), null);
        //将字节数组转换成String类型
        String url = new String(bytes);
        System.out.println(url);
        //找对象
        UsersService usersService = (UsersService) Naming.lookup(url);
        //通过拿到的对象调用方法
        String result = usersService.findUsers("北京尚学堂");
        System.out.println(result);
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
            System.out.println("链接成功!!!");
        }
    }
}

试了很多次了,不是时间的问题

image.png

image.png


服务端

UsersService

package com.bjsxt.service;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface UsersService extends Remote {

    String findUsers(String str) throws RemoteException;

}

UsersServiceImpl

package com.bjsxt.service.impl;

import com.bjsxt.service.UsersService;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class UsersServiceImpl extends UnicastRemoteObject implements UsersService {

    //只保留一个无参数的构造方法即可    将protect改为public
    public UsersServiceImpl() throws RemoteException {

    }

    @Override
    public String findUsers(String str) throws RemoteException {
        return "Hello Zookeeper " + str;
    }
}

serviceDemo

package com.bjsxt;

import com.bjsxt.service.UsersService;
import com.bjsxt.service.impl.UsersServiceImpl;
import org.apache.zookeeper.*;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class serverDemo implements Watcher {
    public static void main(String[] args) throws IOException, AlreadyBoundException, KeeperException, InterruptedException {
        //实例化对外暴露的对象
        UsersService usersService = new UsersServiceImpl();
        //将对象绑定到本地的注册表中     创建一个本地的注册表监听8888端口
        LocateRegistry.createRegistry(8888);
        String url = "rmi://localhost:8888/user";
        //url为绑定标识  完成绑定
        Naming.bind(url, usersService);
        //将url放到Zookeeper的结点中
        ZooKeeper zooKeeper = new ZooKeeper("192.168.88.101:2181,192.168.88.101:2182,192.168.88.101:2183",15000,new serverDemo());
        //创建结点      并且将url放到Znode中
        zooKeeper.create("/bjsxt/service", url.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("服务发布成功!!!");
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
            System.out.println("链接成功!!!");
        }
    }
}

服务端是没问题的    求老师帮忙看看

image.png

JAVA 全系列/第十一阶段:分布式RPC调用和分布式文件存储/Zookeeper 26932楼
JAVA 全系列/第五阶段:JavaWeb开发/Web实战案例 26933楼

package com.bjsxt.dom;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class BookDefaultHandler extends DefaultHandler {
    //重写1
    /**解析xml文档开始时调用*/
    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        System.out.println("解析xml开始");
    }
    /**解析xml文档结束时调用*/
    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
        System.out.println("解析结束");
    }
    /**解析xml中节点时调用*/
    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
       // System.out.println("解析xml中节点时调用");
        //判断如果是book节点,就获取节点的属性和属性值
        if ("book".equals(qName)){
            //获取所有属性
            int count = attributes.getLength();//=个数
            for (int i = 0;i < count ;i++){
                String attName = attributes.getQName(i);
                String attValue = attributes.getValue(i);
                System.out.println("属性名称:"+attName+"\t值为:"+attValue);
            }
        }else if (!"book".equals(qName) && !"books".equals(qName)){
            System.out.println("节点名称:"+qName);
        }
    }
    /**解析xml中节点结束时调用*/
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
       //System.out.println("解析xml中节点结束时调用");
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        String value = new String(ch, start, length);
        if ("".equals(value.trim())){
            System.out.println(value);
        }
    }
}

在这个重写的characters里面,我的这个value并没有值,并且还是有空白。这是什么原因呢?


image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 26935楼

报的异常是这样的

image.png

源码:

package com.bjsxt;

import com.bjsxt.service.UsersService;
import com.bjsxt.service.impl.UsersServiceImpl;
import org.apache.zookeeper.*;

import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class serverDemo implements Watcher {
    public static void main(String[] args) throws IOException, AlreadyBoundException, KeeperException, InterruptedException {
        //实例化对外暴露的对象
        UsersService usersService = new UsersServiceImpl();
        //将对象绑定到本地的注册表中     创建一个本地的注册表监听8888端口
        LocateRegistry.createRegistry(8888);
        String url = "rmi://localhost:8888/user";
        //url为绑定标识  完成绑定
        Naming.bind(url, usersService);
        //将url放到Zookeeper的结点中
        ZooKeeper zooKeeper = new ZooKeeper("192.168.88.101:2181,192.168.88.101:2182,192.168.88.101:2183",15000,new serverDemo());
        //创建结点      并且将url放到Znode中
        zooKeeper.create("/bjsxt/service", url.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("服务发布成功!!!");
    }

    @Override
    public void process(WatchedEvent watchedEvent) {
        if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
            System.out.println("链接成功!!!");
        }
    }
}


JAVA 全系列/第十一阶段:分布式RPC调用和分布式文件存储/Zookeeper 26936楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 26937楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637