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

⭐️: agent在线状态校准接口 #162

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -32,6 +32,7 @@ public interface AgentsService extends IService<Agents> {

void updateName(int id, String name);

// todo 删除
@Deprecated
boolean offLine(int id);

Expand All @@ -43,16 +44,23 @@ public interface AgentsService extends IService<Agents> {

Agents findById(int id);

public void saveAgents(JSONObject jsonObject);
void saveAgents(JSONObject jsonObject);

public void saveAgents(Agents agents);
void saveAgents(Agents agents);

/**
* 会根据 {@link Agents#getLockVersion()}
* @param agents
* @return
* 会根据 {@link Agents#getLockVersion()} 更新Agent状态
*
* @param agents agent对象
* @return 是否更新成功
*/
public boolean updateAgentsByLockVersion(Agents agents);
boolean updateAgentsByLockVersion(Agents agents);

Agents findBySecretKey(String secretKey);

/**
* 校准agent在线状态,只应该在server端使用
*/
void correctionStatus();

public Agents findBySecretKey(String secretKey);
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,12 @@ public RespModel<?> findOne(@RequestParam(name = "id") int id) {
}
}

@WebAspect
@ApiOperation(value = "校准agent的在线状态", notes = "等心跳中断可能有20s延迟(取决于zk配置),这个接口某些情况能更快速更新状态")
@GetMapping("/correction/status")
public RespModel<String> correctionStatus() {
agentsService.correctionStatus();
return new RespModel<>(RespEnum.HANDLE_OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.cluster.router.address.Address;
import org.apache.dubbo.rpc.service.EchoService;
import org.cloud.sonic.common.services.AgentsClientService;
import org.cloud.sonic.controller.mapper.AgentsMapper;
import org.cloud.sonic.common.models.domain.Agents;
import org.cloud.sonic.common.models.domain.Devices;
Expand All @@ -46,6 +50,8 @@ public class AgentsServiceImpl extends SonicServiceImpl<AgentsMapper, Agents> im
private DevicesService devicesService;
@Resource
private AgentsMapper agentsMapper;
@DubboReference(parameters = {"router","address"})
private AgentsClientService agentsClientService;

@Override
public List<Agents> findAgents() {
Expand Down Expand Up @@ -161,4 +167,24 @@ public Agents findById(int id) {
public Agents findBySecretKey(String secretKey) {
return lambdaQuery().eq(Agents::getSecretKey, secretKey).one();
}

@Override
public void correctionStatus() {
List<Agents> agentsList = findAgents();
String msg = "OK";
String res = "";
for (Agents agents : agentsList) {
Address address = new Address(agents.getHost() + "", agents.getRpcPort());
RpcContext.getContext().setObjectAttachment("address", address);
try {
res = ((EchoService) agentsClientService).$echo(msg) + "";
} catch (Exception e) {
offLine(agents);
continue;
}
if (!msg.equals(res)) {
offLine(agents);
}
}
}
}