Aliware MQ 使用代理

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”));
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值