NutzCN Logo
问答 dubox如何获取nutz里的Service服务?
发布于 298天前 作者 qq_e3b6dba3 320 次浏览 复制 上一个帖子 下一个帖子
标签:

dubox如何获取nutz里的Service服务?

16 回复

dubox不认识,是当当那个dubbox?

是的,打漏字了,不好意思@wendal

原来用的nutz已经实现业务服务,由于系统的承载问题,在原项目中添加dubbox架构@wendal

不晓得, 做个demo一起探讨一下?

public class CreateData {
	
	public static void main(String[] args) {


		//System.out.println("Hello Wor");
		
		
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*:dubbo-*.xml");
		context.start();
		// dubbox服务启动
		//com.alibaba.dubbo.container.Main.main(null);
		
	
		SecretService smsService = context.getBean(SecretService.class);
		
		smsService.getTemporaryAuthorized("o2nkpwmsl93g6UlK3YuADah5ELUw",2010, 1);
		
	}
<?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-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="eop-service" owner="programmer" organization="dubbox"/>

    <dubbo:registry address="zookeeper://101.200.62.168:2181"/>
    


<dubbo:reference id="bidService" interface="com.alibaba.dubbo.demo.bid.BidService"/>
    <dubbo:reference id="anotherUserRestService" interface="com.alibaba.dubbo.demo.user.facade.AnotherUserRestService"/>
 <dubbo:reference id="SecretService" interface="com.ruyima.box.api.services.SecretService" group="dev" version="1.0.0"/>
    
    
     <!-- 用dubbo协议在20881端口暴露服务 -->  
    <dubbo:protocol name="dubbo" port="20882" /> 
    <dubbo:service interface="com.ruyima.box.api.services.CardService" ref="cardService"  protocol="dubbo" group="dev" version="1.0.0"/>
    <bean id="cardService" class="com.ruyima.box.api.services.impl.CardServiceImpl" />
    
    


    <!-- directly connect to provider to simulate the access to non-dubbo rest services -->
    <!--<dubbo:reference id="anotherUserRestService" interface="com.alibaba.dubbo.demo.user.facade.AnotherUserRestService" url="rest://localhost:8888/services/"/>-->

</beans>
<2018-06-27 21:48:32> <INFO> [com.alibaba.dubbo.config.AbstractConfig] -  [DUBBO] Register dubbo service com.ruyima.box.api.services.CardService url dubbo://169.254.146.208:20882/com.ruyima.box.api.services.CardService?anyhost=true&application=eop-service&dubbo=2.8.4&generic=false&group=dev&interface=com.ruyima.box.api.services.CardService&methods=sayHello&organization=dubbox&owner=programmer&pid=7880&revision=1.0.0&side=provider&timestamp=1530107312867&version=1.0.0 to registry registry://101.200.62.168:2181/com.alibaba.dubbo.registry.RegistryService?application=eop-service&dubbo=2.8.4&organization=dubbox&owner=programmer&pid=7880&registry=zookeeper&timestamp=1530107312832, dubbo version: 2.8.4, current host: 127.0.0.1
Exception in thread "main" com.alibaba.dubbo.rpc.RpcException: Fail to start server(url: dubbo://169.254.146.208:20882/com.ruyima.box.api.services.CardService?anyhost=true&application=eop-service&channel.readonly.sent=true&codec=dubbo&dubbo=2.8.4&generic=false&group=dev&heartbeat=60000&interface=com.ruyima.box.api.services.CardService&methods=sayHello&organization=dubbox&owner=programmer&pid=7880&revision=1.0.0&side=provider&timestamp=1530107312867&version=1.0.0) Failed to bind NettyServer on /169.254.146.208:20882, cause: Failed to bind to: /0.0.0.0:20882
	at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:331)
	at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.openServer(DubboProtocol.java:308)
	at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.export(DubboProtocol.java:258)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:55)
	at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:56)
	at com.alibaba.dubbo.rpc.Protocol$Adpative.export(Protocol$Adpative.java)
	at com.alibaba.dubbo.registry.integration.RegistryProtocol.doLocalExport(RegistryProtocol.java:153)
	at com.alibaba.dubbo.registry.integration.RegistryProtocol.export(RegistryProtocol.java:107)
	at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:53)
	at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:54)
	at com.alibaba.dubbo.rpc.Protocol$Adpative.export(Protocol$Adpative.java)
	at com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:489)
	at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:285)
	at com.alibaba.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:246)
	at com.alibaba.dubbo.config.ServiceConfig.export(ServiceConfig.java:145)
	at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:110)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:96)
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:334)
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:948)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	at com.ruyima.test.CreateData.main(CreateData.java:53)
Caused by: com.alibaba.dubbo.remoting.RemotingException: Failed to bind NettyServer on /169.254.146.208:20882, cause: Failed to bind to: /0.0.0.0:20882
	at com.alibaba.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:74)
	at com.alibaba.dubbo.remoting.transport.netty.NettyServer.<init>(NettyServer.java:63)
	at com.alibaba.dubbo.remoting.transport.netty.NettyTransporter.bind(NettyTransporter.java:33)
	at com.alibaba.dubbo.remoting.Transporter$Adpative.bind(Transporter$Adpative.java)
	at com.alibaba.dubbo.remoting.Transporters.bind(Transporters.java:48)
	at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchanger.bind(HeaderExchanger.java:41)
	at com.alibaba.dubbo.remoting.exchange.Exchangers.bind(Exchangers.java:63)
	at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:329)
	... 22 more
Caused by: org.jboss.netty.channel.ChannelException: Failed to bind to: /0.0.0.0:20882
	at org.jboss.netty.bootstrap.ServerBootstrap.bind(ServerBootstrap.java:272)
	at com.alibaba.dubbo.remoting.transport.netty.NettyServer.doOpen(NettyServer.java:94)
	at com.alibaba.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:68)
	... 29 more
Caused by: java.net.BindException: Address already in use: bind
	at sun.nio.ch.Net.bind0(Native Method)
	at sun.nio.ch.Net.bind(Net.java:433)
	at sun.nio.ch.Net.bind(Net.java:425)
	at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
	at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
	at org.jboss.netty.channel.socket.nio.NioServerBoss$RegisterTask.run(NioServerBoss.java:193)
	at org.jboss.netty.channel.socket.nio.AbstractNioSelector.processTaskQueue(AbstractNioSelector.java:366)
	at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:290)
	at org.jboss.netty.channel.socket.nio.NioServerBoss.run(NioServerBoss.java:42)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at java.lang.Thread.run(Thread.java:748)
<2018-06-27 21:48:34> <INFO> [com.alibaba.dubbo.config.AbstractConfig] -  [DUBBO] Run shutdown hook now., dubbo version: 2.8.4, current host: 127.0.0.1
<2018-06-27 21:48:34> <INFO> [com.alibaba.dubbo.registry.support.AbstractRegistryFactory] -  [DUBBO] Close all registries [], dubbo version: 2.8.4, current host: 127.0.0.1
<2018-06-27 21:48:34> <INFO> [com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol] -  [DUBBO] Unexport service: dubbo://169.254.146.208:20882/com.ruyima.box.api.services.CardService?anyhost=true&application=eop-service&dubbo=2.8.4&generic=false&group=dev&interface=com.ruyima.box.api.services.CardService&methods=sayHello&organization=dubbox&owner=programmer&pid=7880&revision=1.0.0&side=provider&timestamp=1530107312867&version=1.0.0, dubbo version: 2.8.4, current host: 127.0.0.1
<2018-06-27 21:48:34> <INFO> [com.alibaba.dubbo.rpc.protocol.injvm.InjvmProtocol] -  [DUBBO] Unexport service: injvm://127.0.0.1/com.ruyima.box.api.services.CardService?anyhost=true&application=eop-service&dubbo=2.8.4&generic=false&group=dev&interface=com.ruyima.box.api.services.CardService&methods=sayHello&organization=dubbox&owner=programmer&pid=7880&revision=1.0.0&side=provider&timestamp=1530107312867&version=1.0.0, dubbo version: 2.8.4, current host: 127.0.0.1

最后这个是报错信息@wendal

Address already in use: bind 端口问题

按理说应该和dubbo差别不大

package com.ruyima.box.api.services;

public interface SecretService {

public Object getTemporaryAuthorized(String unionid,Integer roomid,Integer good_id);

}

package com.ruyima.box.api.services.impl;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

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

import org.apache.log4j.Logger;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.mvc.Mvcs;

import redis.clients.jedis.Jedis;

import com.alibaba.fastjson.JSONObject;
import com.gson.WeChat;
import com.gson.WeChatFilter;
import com.gson.oauth.Message;
import com.ruyima.box.api.services.SecretService;
import com.ruyima.box.api.services.SmsService;
import com.ruyima.box.api.services.WeixinService;
import com.ruyima.model.BoxCode;
import com.ruyima.model.Tbl_card;
import com.ruyima.model.Tbl_cell_process_log;
import com.ruyima.model.Tbl_room;
import com.ruyima.model.Tbl_sms;
import com.ruyima.model.Tbl_temporary_authorized;
import com.ruyima.model.Tbl_union_weixin;
import com.ruyima.redis.MyRedis;
import com.ruyima.util.AESUtil;
import com.ruyima.util.AjaxReturnUtils;
import com.ruyima.util.Const;
import com.ruyima.util.DbUtil;
import com.ruyima.util.ReturnMap;
import com.ruyima.util.StringUtil;
import com.ruyima.weixin.AppDataSetup;
import com.ruyima.weixin.service.IBindService;
import com.ruyima.weixin.service.ICabinetService;
import com.ruyima.weixin.service.ICellService;
import com.ruyima.weixin.service.IUserService;

public class SecretServiceImpl implements SecretService {
	//private final Logger LOGGER = Logger.getLogger(WeChatFilter.class);

	@Inject
	protected IBindService bindService;

	@Inject
	protected ICabinetService cabinetService;
	@Inject
	protected ICellService cellService;

	public Object getTemporaryAuthorized(String unionid,Integer roomid,Integer good_id){
		ReturnMap returnMap = null;
		String unionjson = null;
	/*	rsp.setHeader("Access-Control-Allow-Origin", "*");
		rsp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
		rsp.setHeader("Access-Control-Max-Age", "3600");
		rsp.setHeader("Content-Type:"," application/json");
		rsp.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
		rsp.setHeader("Access-Control-Allow-Credentials","true");*/
		 
		//Mvcs.getActionContext()
		Map<String,Object> map = null;
		JSONObject returnObj = null;
		Tbl_temporary_authorized authorized = null;
		Tbl_room room=cellService.find_roomid(roomid);
		//log.info("good line 745:unionid="+unionid);
		Tbl_union_weixin tuw = bindService.findUserByUnionId(unionid);
		if(tuw!=null){
			authorized=cabinetService.getAuthorizedByCellIdRoom(room.getCell_id(),roomid, tuw.getIdtbl_union_weixin(),good_id,unionid);
			returnMap = cabinetService.getTemporaryAuthorizedQRCodeByCellIdAndMobile(room.getCell_id(),tuw.getMobile(),authorized);
			if(returnMap.isSucc()){
				returnObj = AjaxReturnUtils.getSuccessObjByMsg("生成临时授权码成功");
				returnObj.put("urlStr", returnMap.getObjContext());
				returnObj.put("pwd", authorized.getPwd());
				returnObj.put("a_id", authorized.getTemporary_authorized_id());
				returnObj.put("end_time",authorized.getEnd_time());
			}else{
				returnObj = AjaxReturnUtils.getFailObjByMsg(returnMap.getMsg());
			}
		}else{
			return AjaxReturnUtils.getFailObjByMsg("查询用户信息失败!");
		}
		return returnObj;
	}
}

这是dubbox里实现的接口服务

public class AppDataSetup implements Setup {
	private java.util.Timer timer = null;
	public static ClassPathXmlApplicationContext context;
	public static Ioc ioc;

	public void destroy(NutConfig nc) {

@@ -25,8 +27,10 @@ public class AppDataSetup implements Setup {
		context = new ClassPathXmlApplicationContext("classpath*:*.xml");
		context.start();
		
		AppDataSetup.ioc = nc.getIoc();
		
		// dubbox服务启动
		com.alibaba.dubbo.container.Main.main(null);
		//com.alibaba.dubbo.container.Main.main(null);
	}

这是增加的方法,public class WeixinServiceImpl implements WeixinService {
private final Logger LOGGER = Logger.getLogger(WeChatFilter.class);
public class WeixinServiceImpl implements WeixinService {
@Override
public void sendWeixin(Tbl_cell_process_log cell_log, String toOpenid) {
LOGGER.info("微信发送:" + toOpenid);

    bindService = AppDataSetup.ioc.get(BindServiceImpl.class,"bindService");
    bindService.findUserByUnionId("");

这是在实现方法里调用nutz容器里的service服务,这样就实现了

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