要调用本地服务,可以使用Dubbo的Injvm协议。下面是一个示例代码:
injvm协议:<dubbo:protocol name="injvm" />
@Service注解,并指定protocol为injvm:@Service(protocol = "injvm")
public class DemoServiceImpl implements DemoService {
//...
}
injvm协议:<dubbo:protocol name="injvm" />
@Reference注解,并指定protocol为injvm:@Reference(protocol = "injvm")
public interface DemoService {
//...
}
public class DemoConsumer {
public static void main(String[] args) {
// 创建Spring上下文
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取本地服务代理
DemoService demoService = context.getBean(DemoService.class);
// 调用本地服务方法
String result = demoService.sayHello("Dubbo");
// 打印结果
System.out.println(result);
}
}
这样就可以通过Dubbo调用本地服务了。