对于同一个应用部署了多套的情况下,可能出现接口不同步的问题。因为magic-api在启动之后,接口信息全部缓存在内存中,新增也如此,所以对于其他机器是不知道有接口变动的。
<!-- 集群插件 -->
<dependency>
<groupId>org.ssssssss</groupId>
<artifactId>magic-api-plugin-cluster</artifactId>
<version>2.0.2</version>
</dependency>
默认情况下,集群插件依赖于redis插件,引入cluster插件后,将自动引入redis插件
在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
实现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)));
}
}
@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;
}
}
@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));
}
}
@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);
}
@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;
}
启动两台相同的微服务进行测试。