NutzCN Logo
问答 nutz + dubbo 怎么获取消费者
发布于 8天前 作者 qq_9cc84e4e 61 次浏览 复制 上一个帖子 下一个帖子
标签:

弱弱的问一下,
nutz + dubbo 获取不到消费者,代码如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://code.alibabatech.com/schema/dubbo        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
    <dubbo:application name="hello-world-app"  />
 
    <!-- <dubbo:registry address="multicast://224.0.0.0:9999" /> -->
    <dubbo:registry address="zookeeper://127.0.0.1:2181" />
 
    <dubbo:protocol name="dubbo" port="20880" />
    <!-- <dubbo:protocol name="injvm"></dubbo:protocol> -->
 
    <dubbo:service interface="com.test.dub.service.DemoService" ref="demoServiceLocal" />
 
    <dubbo:reference id="demoServiceRemote" interface="com.test.dub.service.DemoService" />
 
</beans>
package com.test.dub.service;

public interface DemoService {
    
    String hi(String name);
}
package com.test.dub.service;

import org.nutz.ioc.loader.annotation.IocBean;

import com.alibaba.dubbo.config.annotation.Service;

@IocBean(name="demoServiceLocal")
@Service
public class DemoServiceImpl implements DemoService {

    public DemoServiceImpl() {}
    
    public String hi(String name) {
        System.out.println("hi, everyone");
        return "hi,"+name;
    }
}
package com.test.dub.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;

import com.alibaba.dubbo.config.annotation.Reference;
import com.test.dub.service.DemoService;


@IocBean
public class TestAction {

//	@Inject
	@Reference(consumer="demoServiceRemote")
	protected DemoService demoService;
	 
	@At
	public void home(HttpServletRequest request, HttpServletResponse response) {
		System.out.println(demoService.hi("hunter"));
	}

	public void setDemoService(DemoService demoService) {
		this.demoService = demoService;
	}

}

-------------------------------------------------------------------
// @Inject
@Reference(consumer="demoServiceRemote")
protected DemoService demoService;
这个必须要用@Inject注入么?

4 回复

补充一下: protected DemoService demoService; 不用@Inject注解的话,其值为null

@Inject("refer:demoServiceRemote")

必须是ioc对象,不能是new出来的对象

搞定,谢谢

dubbo:reference的作用就是声明一个ioc bean,在spring和nutz里面都一样

添加回复
请先登陆
回到顶部