magic-api集群插件解决接口不同步问题

1、问题描述

对于同一个应用部署了多套的情况下,可能出现接口不同步的问题。因为magic-api在启动之后,接口信息全部缓存在内存中,新增也如此,所以对于其他机器是不知道有接口变动的。

2、引入集群插件pom依赖

<!-- 集群插件 -->
<dependency>
  <groupId>org.ssssssss</groupId>
  <artifactId>magic-api-plugin-cluster</artifactId>
  <version>2.0.2</version>
</dependency>

默认情况下,集群插件依赖于redis插件,引入cluster插件后,将自动引入redis插件

3、添加配置

在yml中添加如下配置:

#可以注释掉,这里我使用它的默认配置
#magic-api:
#    instanceId: .... #实例ID,集群环境下,要保证每台机器不同。默认启动后随机生成uuid,如无特殊情况可不配置。
#    cluster:
#        channel: magic-api:notify:channel #redis 通道,此值为默认值
# 配置Redis
spring:
    redis:
        host: 127.0.0.1
        port: 6379
        database: 0
        password: 123456

4、消息发送实现

实现MagicNotifyService接口的发送方式,这里使用的是stringRedisTemplate发送消息到redis队列

@Configuration
@AllArgsConstructor
public class BeanConfig {

	private final StringRedisTemplate stringRedisTemplate;
	private final ClusterConfig config;

	@Bean
	public MagicNotifyService magicNotifyService() {
		return magicNotify -> stringRedisTemplate.convertAndSend(config.getChannel(), Objects.requireNonNull(JsonUtils.toJsonString(magicNotify)));
	}
}

5、redis监听器配置

@Configuration
@AllArgsConstructor
public class RedisConfig {

	private final ClusterConfig config;

	@Bean
	public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory connectionFactory,
																	   RedisSubscribeService redisSubscribeService) {
		RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
		redisMessageListenerContainer.setConnectionFactory(connectionFactory);
		redisMessageListenerContainer.addMessageListener(redisSubscribeService, Lists.newArrayList(
			ChannelTopic.of(config.getChannel())
		));
		return redisMessageListenerContainer;
	}
}

6、消息订阅处理

@Component
@Slf4j
public class RedisSubscribeServiceImpl extends MessageListenerAdapter implements RedisSubscribeService {

	@Autowired
	private MagicAPIService service;

	@Override
	public void onMessage(@Nullable Message message, byte[] bytes) {
		//在接收到变动通知后,需调用processNotify方法处理通知
		service.processNotify(JsonUtils.readValue(message.getBody(), MagicNotify.class));
	}
}

7、缓存同步

7.1、添加接口时发送消息,通知其它节点

@Service
public class MagicApiServiceImpl implements MagicApiService {
	@Autowired
	private MagicResourceService magicResourceService;
	@Autowired
	private MagicNotifyService magicNotifyService;

	@Override
	public void save(ApiInfo apiInfo) {
		//添加接口
		magicResourceService.saveFile(apiInfo);

		//添加完接口后,发送通知同步接口至其它节点
		MagicNotify magicNotify = new MagicNotify();
		magicNotify.setAction(EventAction.SAVE);
		magicNotify.setType(Constants.EVENT_TYPE_FILE);
		magicNotifyService.sendNotify(magicNotify);
	}

7.2、删除接口时发送消息,通知其它节点

@Override
public void delete(String id) {
    //删除接口
    magicResourceService.delete(id);
    
    //删除完接口后,发送通知同步接口至其它节点
    MagicNotify magicNotify = new MagicNotify();
    magicNotify.setAction(EventAction.DELETE);
    magicNotify.setType(Constants.EVENT_TYPE_FILE);
    magicNotifyService.sendNotify(magicNotify);
    return deleteLogic;
}

8、测试

启动两台相同的微服务进行测试。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值