使用nutzboot和dubbo服务的时候,注入对象出错,报错如下:
org.nutz.ioc.IocException: IocBean[dubboManager -> dubboAnnotationBean -> payInterServiceImpl] throw Exception when creating
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:153)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:241)
at org.nutz.ioc.impl.NutIoc.getByType(NutIoc.java:457)
at org.nutz.ioc.impl.NutIoc.getByType(NutIoc.java:426)
at org.nutz.integration.dubbo.AnnotationBean._init(AnnotationBean.java:73)
at org.nutz.integration.dubbo.AnnotationBean$FM$_init$dac3e00c3bc87ef1b026b7003e9d0058.invoke(AnnotationBean.java)
at org.nutz.ioc.impl.ObjectMakerImpl$2.trigger(ObjectMakerImpl.java:181)
at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:89)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:141)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:241)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:271)
at org.nutz.integration.dubbo.DubboManager.init(DubboManager.java:31)
at org.nutz.integration.dubbo.DubboManager._init(DubboManager.java:24)
at org.nutz.integration.dubbo.DubboManager$FM$_init$dac3e00c3bc87ef1b026b7003e9d0058.invoke(DubboManager.java)
at org.nutz.ioc.impl.ObjectMakerImpl$2.trigger(ObjectMakerImpl.java:181)
at org.nutz.ioc.weaver.DefaultWeaver.onCreate(DefaultWeaver.java:89)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:141)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:241)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:271)
at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:161)
at org.nutz.boot.starter.dubbo.DubboStarter.start(DubboStarter.java:17)
at org.nutz.boot.AppContext.startServers(AppContext.java:310)
at org.nutz.boot.NbApp.execute(NbApp.java:210)
at org.nutz.boot.NbApp.run(NbApp.java:182)
at com.tv.cps.pay.MainLauncher.main(MainLauncher.java:66)
Caused by: java.lang.RuntimeException: IocBean[payInterServiceImpl] fail at field=[payHttp]
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:40)
at org.nutz.ioc.weaver.DefaultWeaver.fill(DefaultWeaver.java:67)
at org.nutz.ioc.impl.ObjectMakerImpl.make(ObjectMakerImpl.java:138)
... 24 more
Caused by: org.nutz.ioc.IocException: IocBean[class:com.tv.cps.common.Http] none ioc bean match class=com.tv.cps.common.Http
at org.nutz.ioc.impl.NutIoc.getByType(NutIoc.java:461)
at org.nutz.ioc.val.ReferTypeValue.get(ReferTypeValue.java:66)
at org.nutz.ioc.weaver.FieldInjector.inject(FieldInjector.java:32)
... 26 more
注入代码段
@IocBean
@Service(interfaceClass = PayInterService.class)
public class PayInterServiceImpl implements PayInterService {
@Inject protected Http payHttp;
@Override
public NutMap sendPay(...) throws ApiException{
payHttp.post(...);
}
}
http类
public class Http {
private static final Log log = Logs.get();
private static final String FORM_CONTENT = "application/x-www-form-urlencoded";
private static final String JSON_CONTENT = "application/json";
private static final String XML_CONTENT = "application/xml";
/**
* Http名称,用于区分Http的用途
*/
private String name;
/**
* 错误次数,用于管理错误指标及熔断
*/
private int errNum;
/**
* 超时次数,用于管理超时指标及熔断
*/
private int timeoutNum;
/**
* 错误次数熔断指标,0代表不熔断
*/
private int errDownNum;
/**
* 超时次数熔断指标,0代表不熔断
*/
private int timeoutDownNum;
/**
* 熔断后是否自动恢复
*/
private boolean autoReply;
/**
* 熔断自动恢复周期 - 单位:秒
*/
private int downCycle;
/**
* 连接超时时间
*/
private int connectTimeout;
/**
* 等待超时时间
*/
private int readTimeout;
/**
* 处理Http异常
*
* @param e
*/
private void executeException(String url, Throwable e) {
log.infof(e.getMessage());
if (e.getMessage().contains("connect timed out")) {
log.infof("[httpObject:%s] [%s]连接超时...", this.name, url);
this.timeoutNum++;
} else if (e.getMessage().contains("Read timed out")) {
log.infof("[httpObject:%s] [%s] 读取超时...", this.name, url);
this.timeoutNum++;
} else {
System.out.println(e.getMessage());
}
}
/**
* 设置Http名称
*
* @param name
*/
public void setName(String name) {
Sender.setup(null);
this.name = name;
}
/**
* 设置错误次数 - 非200状态的请求返回,Http类会自己管理错误次数
*
* @param errNum
*/
public void setErrNum(int errNum) {
this.errNum = errNum;
}
/**
* 设置超时次数 - 超时情况下会自己计数,一般情况下不用自己设置
* 当readTime小于100时,不会计次,用于某些接口不等待返回值的情况
*
* @param timeoutNum
*/
public void setTimeoutNum(int timeoutNum) {
this.timeoutNum = timeoutNum;
}
/**
* 设置错误熔断次数 - 当错误次数达到上限后,则自动熔断,熔断效果为所有接口返回null
*
* @param errDownNum
*/
public void setErrDownNum(int errDownNum) {
this.errDownNum = errDownNum;
}
/**
* 设置超时熔断次数 - 当超时次数达到上限后,则自动熔断,熔断效果为所有接口返回null
*
* @param timeoutDownNum
*/
public void setTimeoutDownNum(int timeoutDownNum) {
this.timeoutDownNum = timeoutDownNum;
}
/**
* 设置熔断后是否自动恢复
*
* @param autoReply
*/
public void setAutoReply(boolean autoReply) {
this.autoReply = autoReply;
}
/**
* 设置熔断自动恢复周期(秒)
*
* @param downCycle
*/
public void setDownCycle(int downCycle) {
this.downCycle = downCycle;
if (downCycle > 0) {
Tasks.scheduleAtFixedRate(() -> {
if (autoReply) {
clearAllNum();
}
}, this.downCycle);
}
}
/**
* 清空错误和超时计次,用于恢复熔断
*/
public void clearAllNum() {
this.errNum = 0;
this.timeoutNum = 0;
}
/**
* 设置连接超时时间
*
* @param connectTimeout
*/
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
/**
* 设置请求等待超时时间
*
* @param readTimeout
*/
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
public Http() {
this.errDownNum = 0;
this.timeoutDownNum = 0;
this.autoReply = false;
this.downCycle = 0;
this.connectTimeout = 1000;
this.readTimeout = 5000;
}
private String get(String url, NutMap map, String enc) {
try {
Stopwatch sw = Stopwatch.begin();
Response response = Sender.create(Request.get(url).setParams(map).setEnc(enc)).setConnTimeout(connectTimeout).setTimeout(readTimeout).send();
response.setEncode(enc);
String result = response.getContent();
sw.stop();
log.infof("[httpObject:%s] method:%s url:%s params:%s result:%s postTimes: %sms", this.name, "getData", url, nutMapToKV_UTF8(map), result, sw.getDuration());
return response.getContent();
} catch (Throwable e) {
executeException(url, e.getCause());
log.errorf("[httpObject:%s] getData error! message is : %s", this.name, e.getCause().getMessage());
return "";
}
}
private String post(String url, String data, String contentType) {
try {
Stopwatch sw = Stopwatch.begin();
Request req = Request.create(url, Request.METHOD.POST);
req.setData(data);
req.getHeader().set("Content-Type", contentType);
Response response = Sender.create(req).setConnTimeout(connectTimeout).setTimeout(readTimeout).send();
String result = response.getContent();
sw.stop();
log.infof("[httpObject:%s] method:%s content-type:%s url:%s params:%s result:%s postTimes: %sms", this.name, "postData", contentType, url, data, result, sw.getDuration());
return result;
} catch (Throwable e) {
executeException(url, e.getCause());
log.errorf("[httpObject:%s] postData error! message is : %s", this.name, Lang.getStackTrace(e));
return "";
}
}
public Response postGetResponse(String url, String data, String contentType) {
try {
Stopwatch sw = Stopwatch.begin();
Request req = Request.create(url, Request.METHOD.POST);
req.setData(data);
req.getHeader().set("Content-Type", contentType);
Response response = Sender.create(req).setConnTimeout(connectTimeout).setTimeout(readTimeout).send();
return response;
} catch (Throwable e) {
executeException(url, e.getCause());
log.errorf("[httpObject:%s] postData error! message is : %s", this.name, Lang.getStackTrace(e));
return null;
}
}
/**
* NutMap转KV形式
*
* @param map map对象
* @param encoding 字符集编码
* @return
*/
private String nutMapToKV(NutMap map, String encoding) {
if (map == null) return "";
List<String> dataList = new ArrayList();
for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
try {
String key = stringObjectEntry.getKey();
Object valueObj = stringObjectEntry.getValue();
String value= Lang.isEmpty(valueObj) ? "" : URLEncoder.encode(stringObjectEntry.getValue().toString(), encoding);
dataList.add(String.format("%s=%s", key, value));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return Strings.join("&", dataList);
}
/**
* http get请求UTF8字符集获取信息
*
* @param url url
* @param map map对象
* @return
*/
public String getData_UTF8(String url, NutMap map) {
return get(url, map, "UTF-8");
}
/**
* http get请求GBK字符集获取信息
*
* @param url url
* @param map map对象
* @return
*/
public String getData_GBK(String url, NutMap map) {
return get(url, map, "GBK");
}
/**
* NutMap转KV形式,使用UTF-8字符集encode
*
* @param map map对象
* @return
*/
public String nutMapToKV_UTF8(NutMap map) {
return nutMapToKV(map, "UTF-8");
}
/**
* NutMap转KV形式,使用GBK字符集encode
*
* @param map
* @return
*/
public String nutMapToKV_GBK(NutMap map) {
return nutMapToKV(map, "GBK");
}
/**
* 发送Form表单的post请求
*
* @param url url
* @param data 发送的数据 k=v&k=v形式
* @return
*/
public String postFormData(String url, String data) {
return post(url, data, FORM_CONTENT);
}
/**
* 发送json形式的post请求
*
* @param url url
* @param data 发送的数据 {"a":1,"b":"c"} json格式数据
* @return
*/
public String postJsonData(String url, String data) {
return post(url, data, JSON_CONTENT);
}
/**
* 发送xml形式的post请求
*
* @param url url
* @param data 发送的数据 xml 格式
* @return
*/
public String postXmlData(String url, String data) {
return post(url, data, XML_CONTENT);
}
/**
* 发送自定义形式的post请求
*
* @param url
* @param body
* @param header
* @return
*/
public String postData(String url, Object body, Header header) {
try {
Stopwatch sw = Stopwatch.begin();
Response response = org.nutz.http.Http.post3(url, body, header, readTimeout, connectTimeout);
String result = response.getContent();
sw.stop();
log.infof("[httpObject:%s] method:%s url:%s params:%s result:%s postTimes: %sms", this.name, "postCommonData", url, body, result, sw.getDuration());
return result;
} catch (Throwable e) {
executeException(url, e);
return "";
}
}
}