提交 11d3df66 编写于 作者: L laohu

add acl use example. AclClient.java

上级 94403714
......@@ -16,6 +16,7 @@
*/
package org.apache.rocketmq.acl.plug;
import java.util.HashMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.acl.AccessResource;
import org.apache.rocketmq.acl.AccessValidator;
......@@ -27,8 +28,6 @@ import org.apache.rocketmq.acl.plug.entity.ControllerParameters;
import org.apache.rocketmq.acl.plug.exception.AclPlugRuntimeException;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import java.util.HashMap;
public class DefaultAclRemotingServiceImpl implements AclRemotingService, AccessValidator {
private AclPlugEngine aclPlugEngine;
......@@ -80,8 +79,8 @@ public class DefaultAclRemotingServiceImpl implements AclRemotingService, Access
if (authenticationResult.getAccessControl() == null || !authenticationResult.isSucceed()) {
throw new AclPlugRuntimeException(String.format("%s accessControl data is %s", authenticationResult.getResultString(), accessResource.toString()));
}
}catch(Exception e) {
throw new AclPlugRuntimeException(String.format("validate exception AccessResource data %s", accessResource.toString()) , e);
} catch (Exception e) {
throw new AclPlugRuntimeException(String.format("validate exception AccessResource data %s", accessResource.toString()), e);
}
}
......
......@@ -20,7 +20,6 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.PullResult;
......@@ -40,21 +39,22 @@ import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
/**
* 1. 把broker模块src/test/resources/META-INF/service/org.apache.rocketmq.acl.AccessValidator 复制到src/java/resources/META-INF/service
* 2. 查看distribution模块下 /conf/transport.yml文件,注意里面的账户密码,ip
* 3. 把ALC_RCP_HOOK_ACCOUT与ACL_RCP_HOOK_PASSWORD 修改成transport.yml里面对应的账户密码
* @author laohu
*
* English explain
* 1. broker module src/test/resources/META-INF/service/org.apache.rocketmq.acl.AccessValidator copy to src/java/resources/META-INF/service.
*
* 2. view the /conf/transport.yml file under the distribution module, pay attention to the account password, IP.
*
* 3. Modify ALC_RCP_HOOK_ACCOUT and ACL_RCP_HOOK_PASSWORD to the corresponding account password in transport.yml
*
*/
public class AclClient {
private static final Map<MessageQueue, Long> OFFSE_TABLE = new HashMap<MessageQueue, Long>();
private static String ALC_RCP_HOOK_ACCOUT = "RocketMQ";
private static String ACL_RCP_HOOK_PASSWORD = "1234567";
private static final String ACL_RCPHOOK_ACCOUT = "RocketMQ";
private static final String ACL_RCPHOOK_PASSWORD = "1234567";
public static void main(String[] args) throws MQClientException, InterruptedException {
producer();
......@@ -63,7 +63,7 @@ public class AclClient {
}
public static void producer() throws MQClientException {
DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName",getAalRPCHook());
DefaultMQProducer producer = new DefaultMQProducer("ProducerGroupName", getAalRPCHook());
producer.setNamesrvAddr("127.0.0.1:9876");
producer.start();
......@@ -87,8 +87,7 @@ public class AclClient {
public static void pushConsumer() throws MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_5" , getAalRPCHook(),new AllocateMessageQueueAveragely());
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("please_rename_unique_group_name_5", getAalRPCHook(), new AllocateMessageQueueAveragely());
consumer.setNamesrvAddr("127.0.0.1:9876");
consumer.subscribe("TopicTest", "*");
consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
......@@ -108,7 +107,7 @@ public class AclClient {
}
public static void pullConsumer() throws MQClientException {
DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_6" , getAalRPCHook());
DefaultMQPullConsumer consumer = new DefaultMQPullConsumer("please_rename_unique_group_name_6", getAalRPCHook());
consumer.setNamesrvAddr("127.0.0.1:9876");
consumer.start();
......@@ -149,12 +148,11 @@ public class AclClient {
}
private static void printBody(List<MessageExt> msg) {
if(msg == null || msg.size() == 0)
if (msg == null || msg.size() == 0)
return;
for(MessageExt m : msg) {
if(m != null) {
System.out.printf("msgId : %s body : %s",m.getMsgId() , new String(m.getBody()));
System.out.println();
for (MessageExt m : msg) {
if (m != null) {
System.out.printf("msgId : %s body : %s \n\r", m.getMsgId(), new String(m.getBody()));
}
}
}
......@@ -172,17 +170,16 @@ public class AclClient {
}
static RPCHook getAalRPCHook() {
return new AalRPCHook(ALC_RCP_HOOK_ACCOUT, ACL_RCP_HOOK_PASSWORD);
return new AalRPCHook(ACL_RCPHOOK_ACCOUT, ACL_RCPHOOK_PASSWORD);
}
static class AalRPCHook implements RPCHook{
static class AalRPCHook implements RPCHook {
private String account;
private String password;
public AalRPCHook(String account , String password) {
public AalRPCHook(String account, String password) {
this.account = account;
this.password = password;
}
......@@ -191,7 +188,7 @@ public class AclClient {
public void doBeforeRequest(String remoteAddr, RemotingCommand request) {
HashMap<String, String> ext = request.getExtFields();
if(ext == null) {
if (ext == null) {
ext = new HashMap<>();
request.setExtFields(ext);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册