4

jshell 如何调用 dubbo 远程服务接口

 1 year ago
source link: https://www.v2ex.com/t/866614
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Java

jshell 如何调用 dubbo 远程服务接口

  873792861 · 5 小时 12 分钟前 · 352 次点击

#如题,背景如下:我目前处于处于软件实施这个岗位,平时会用 python 处理一些数据。所以,对 java 理解甚少。公司的各个系统是用 dubbo 连接各个服务的。但 python 没有可以调 dubbo 协议接口的库,所以我想通过 jshell 来调用 dubbo 接口。 #测试使用的是dubbo 示例中的 dubbo-samples-protobuf ,我把服务和客户端分别放在两台机器上,用 maven 编译测试成功后。把 traget 文件夹下 lib 里的 jar 包复制到 jshell 自定义的 class 文件夹里。然后用 jshell 进行测试,代码如下

import org.springframework.context.support.ClassPathXmlApplicationContext;
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("file:/home/dubbo-consumer.xml");
context.start();

以上代码片段没有报错,接着输入下面代码,回车测试

context.getBean("demoService")

然后报错了:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'demoService': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: org.apache.dubbo.demo.DemoService
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:177)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:103)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1640)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1080)
	at .(#27:1)

然后换成下方代码,再运行同样报错:

public interface DemoService {
    String sayHello(String name);
};
public class DemoServiceImpl implements DemoService {
    public String sayHello(String name) {
        return "Hello " + name;
    }
};
DemoService demoService = (DemoService)context.getBean("demoService");

#恳请广大 V 友告知这是为啥报错呢?如果我想用 jshell 来调用 dubbo 远程服务接口,该怎么办呢?

第 1 条附言  ·  3 小时 56 分钟前

版本为jdk10,以下为xml配置:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

    <dubbo:application name="demo-consumer"/>

    <dubbo:registry address="zookeeper://192.168.1.122:2181" timeout="8000"/>

    <dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>

</beans>

同样的配置用教程编译出来的jar文件来运行是可以的


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK