Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command consul registry #96

Merged
merged 9 commits into from
Jun 21, 2016
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package com.weibo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.weibo.api.motan.registry.support.command.RpcCommand;
import com.weibo.api.motan.registry.support.command.RpcCommandUtil;
import com.weibo.api.motan.util.LoggerUtil;
import com.weibo.dao.OperationRecordMapper;
import com.weibo.model.OperationRecord;
import com.weibo.service.CommandService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public abstract class AbstractCommandService implements CommandService {

@Autowired(required = false)
private OperationRecordMapper recordMapper;

/**
* 向指定group添加指令
*
* @param group
* @param command
* @return
*/
@Override
public boolean addCommand(String group, RpcCommand.ClientCommand command) {
RpcCommand remoteCommand = RpcCommandUtil.stringToCommand(getCommands(group));
if (remoteCommand == null) {
remoteCommand = new RpcCommand();
}
List<RpcCommand.ClientCommand> clientCommandList = remoteCommand.getClientCommandList();
if (clientCommandList == null) {
clientCommandList = new ArrayList<RpcCommand.ClientCommand>();
}

// 该方法只在流量切换界面被调用,此时指令序号默认是0
int index = getRpcCommandMaxIndex(remoteCommand);
command.setIndex(index + 1);
clientCommandList.add(command);
remoteCommand.setClientCommandList(clientCommandList);

return setCommand(group, remoteCommand);
}

/**
* 更新指定group的某条指令
*
* @param command
* @param group
* @return
*/
@Override
public boolean updateCommand(String group, RpcCommand.ClientCommand command) {
RpcCommand remoteCommand = RpcCommandUtil.stringToCommand(getCommands(group));
if (remoteCommand == null) {
LoggerUtil.info("update failed, command not found");
return false;
}
List<RpcCommand.ClientCommand> clientCommandList = remoteCommand.getClientCommandList();
if (clientCommandList == null) {
LoggerUtil.info("update failed, command not found");
return false;
}
boolean found = false;
for (RpcCommand.ClientCommand cmd : clientCommandList) {
if (cmd.getIndex().equals(command.getIndex())) {
clientCommandList.remove(cmd);
clientCommandList.add(command);
found = true;
break;
}
}
if (!found) {
LoggerUtil.info("update failed, command not found");
return false;
}
remoteCommand.setClientCommandList(clientCommandList);
return setCommand(group, remoteCommand);
}

/**
* 删除指定group的某条指令
*
* @param group
* @param index
* @return
*/
@Override
public boolean deleteCommand(String group, int index) {
RpcCommand remoteCommand = RpcCommandUtil.stringToCommand(getCommands(group));
if (remoteCommand == null) {
LoggerUtil.info("delete failed, command not found");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

日志里记录上下文

return false;
}
List<RpcCommand.ClientCommand> clientCommandList = remoteCommand.getClientCommandList();
if (clientCommandList == null) {
LoggerUtil.info("delete failed, command not found");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

日志里记录上下文

return false;
}
boolean found = false;
for (RpcCommand.ClientCommand cmd : clientCommandList) {
if (cmd.getIndex() == index) {
clientCommandList.remove(cmd);
found = true;
break;
}
}
if (!found) {
LoggerUtil.info("delete failed, command not found");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

日志里记录上下文

return false;
}
remoteCommand.setClientCommandList(clientCommandList);

return setCommand(group, remoteCommand);
}

/**
* 获取指令集中最大的指令序号
*
* @param rpcCommand
* @return
*/
@Override
public int getRpcCommandMaxIndex(RpcCommand rpcCommand) {
return 0;
}

/**
* 预览指令
*
* @param group
* @param clientCommand
* @param previewIP
* @return
*/
@Override
public List<JSONObject> previewCommand(String group, RpcCommand.ClientCommand clientCommand, String previewIP) {
return null;
}

/**
* 根据group和clientCommand生成指令
*
* @param group
* @param clientCommand
* @return
*/
@Override
public RpcCommand buildCommand(String group, RpcCommand.ClientCommand clientCommand) {
RpcCommand rpcCommand = new RpcCommand();
List<RpcCommand.ClientCommand> commandList = new ArrayList<RpcCommand.ClientCommand>();
commandList.add(clientCommand);
rpcCommand.setClientCommandList(commandList);
return rpcCommand;
}

/**
* 获取指令操作记录
*
* @return
*/
@Override
public List<OperationRecord> getAllRecord() {
List<OperationRecord> records;
if (recordMapper != null) {
records = recordMapper.selectAll();
} else {
return null;
}
return records;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

import com.alibaba.fastjson.JSONObject;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.kv.model.GetValue;
import com.weibo.api.motan.registry.consul.ConsulConstants;
import com.weibo.api.motan.registry.support.command.RpcCommand;
import com.weibo.model.OperationRecord;
import com.weibo.service.CommandService;
import com.weibo.api.motan.registry.support.command.RpcCommandUtil;
import com.weibo.utils.ConsulClientWrapper;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

@Service
@Lazy
public class ConsulCommandService implements CommandService {
public class ConsulCommandService extends AbstractCommandService {
@Autowired
private ConsulClientWrapper clientWrapper;
private ConsulClient consulClient;
Expand All @@ -25,53 +29,102 @@ void init() {
consulClient = clientWrapper.getConsulClient();
}

/**
* 获取所有指令
*
* @return
*/
@Override
public List<JSONObject> getAllCommands() {
return null;
List<JSONObject> commands = new ArrayList<JSONObject>();
Response<List<GetValue>> response = consulClient.getKVValues(ConsulConstants.CONSUL_MOTAN_COMMAND);
List<GetValue> values = response.getValue();
if (values != null) {
for (GetValue value : values) {
JSONObject node = new JSONObject();
if (value.getValue() == null) {
continue;
}
String group = value.getKey().substring(ConsulConstants.CONSUL_MOTAN_COMMAND.length());
String command = new String(Base64.decodeBase64(value.getValue()));
node.put("group", group);
node.put("command", RpcCommandUtil.stringToCommand(command));
commands.add(node);
}
}
return commands;
}

/**
* 获取指定group的指令列表
*
* @param groupName
* @return
*/
@Override
public String getCommands(String groupName) {
return null;
Response<GetValue> response = consulClient.getKVValue(ConsulConstants.CONSUL_MOTAN_COMMAND + groupName);
GetValue value = response.getValue();
String command = "";
if (value != null && value.getValue() != null) {
command = new String(Base64.decodeBase64(value.getValue()));
}
return command;
}

/**
* 更新指定group的指令列表
*
* @param command
* @param group
* @return
*/
@Override
public boolean setCommand(String group, RpcCommand command) {
return false;
}

@Override
public boolean addCommand(String group, RpcCommand.ClientCommand command) {
return false;
}

@Override
public boolean updateCommand(String group, RpcCommand.ClientCommand command) {
return false;
}

@Override
public boolean deleteCommand(String group, int index) {
return false;
}

@Override
public int getRpcCommandMaxIndex(RpcCommand rpcCommand) {
return 0;
}
List<RpcCommand.ClientCommand> newCommandList = new ArrayList<RpcCommand.ClientCommand>();
for (RpcCommand.ClientCommand clientCommand : command.getClientCommandList()) {
List<String> newMergeGroups = new ArrayList<String>();
for (String mergeGroup : clientCommand.getMergeGroups()) {
mergeGroup = removeGroupNamePrefix(mergeGroup);
newMergeGroups.add(mergeGroup);
}
clientCommand.setMergeGroups(newMergeGroups);
newCommandList.add(clientCommand);
}
command.setClientCommandList(newCommandList);

@Override
public List<JSONObject> previewCommand(String group, RpcCommand.ClientCommand clientCommand, String previewIP) {
return null;
Response<Boolean> response = consulClient.setKVValue(
ConsulConstants.CONSUL_MOTAN_COMMAND + removeDatacenterPrefix(group),
RpcCommandUtil.commandToString(command));
return response.getValue();
}

@Override
public RpcCommand buildCommand(String group, RpcCommand.ClientCommand clientCommand) {
return null;
/**
* 去除group的datacenter前缀
*
* @param group
* @return
*/
private String removeDatacenterPrefix(String group) {
int index = group.indexOf(":");
if (index > 0) {
return group.substring(group.indexOf(":") + 1);
} else {
return group;
}
}

@Override
public List<OperationRecord> getAllRecord() {
return null;
/**
* 去除group的motan标识前缀
*
* @param group
* @return
*/
private String removeGroupNamePrefix(String group) {
if (group.contains(ConsulConstants.CONSUL_SERVICE_MOTAN_PRE)) {
return removeDatacenterPrefix(group).substring(ConsulConstants.CONSUL_SERVICE_MOTAN_PRE.length());
} else {
return group;
}
}
}
Loading