Aliware MQ 使用中遇到代理问题
在学习使用Aliware MQ过程中,遇到了如下错误。
Exception in thread “main”
com.aliyun.openservices.ons.api.exception.ONSClientException: Can not
find name server, May be your network problem. See
http://docs.aliyun.com/cn#/pub/ons/faq/exceptions&namesrv_not_exist
for further details. at
com.aliyun.openservices.ons.api.impl.rocketmq.ONSClientAbstract.(ONSClientAbstract.java:70)
at
com.aliyun.openservices.ons.api.impl.rocketmq.ProducerImpl.(ProducerImpl.java:31)
at
com.aliyun.openservices.ons.api.impl.ONSFactoryImpl.createProducer(ONSFactoryImpl.java:20)
at
com.aliyun.openservices.ons.api.ONSFactory.createProducer(ONSFactory.java:45)
at
com.aliyun.openservices.client.ProducerClien.main(ProducerClien.java:44)
异常原因是因为网络使用了代理,需要在代码中加入代理参数。
Java中设置代理可以使用Authenticator类,但Authenticator类是一个抽象类,所以需要实现一下:
public class BasicAuthenticator extends Authenticator {
StringuserName;
String password;
public BasicAuthenticator(String userName, String password) {
this.userName = userName;
this.password = password;
}
/**
* Called when password authorization is needed. Subclasses should override the default implementation, which
* returns null.
*
* @return The PasswordAuthentication collected from the user, or null if none is provided.
*/
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password.toCharArray());
}
}
在业务类中加入以下代码块:
static{
System.setProperty(“http.proxyHost”, “proxy.*.com”);
System.setProperty(“http.proxyPort”, “8080”);
Authenticator.setDefault(new BasicAuthenticator(“XXX”, “XXXX”));
}