《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
public void init() {
gateways = new HashMap<>();
httpServices = new ArrayList<>();
for (TbTenantConfiguration configuration : configuration.getTenants()) {
isRemoteConfiguration = configuration.getRemoteConfiguration();
if (isRemoteConfiguration) {
String label = configuration.getLabel();
log.info(“[{}] Initializing gateway”, configuration.getLabel());
TenantServiceRegistry tenantServiceRegistry = new TenantServiceRegistry();
GatewayService service = null;
try {
// isRemoteConfiguration为true时,会响应服务端返回的配置信息
service = getGatewayService(configuration, c -> tenantServiceRegistry.updateExtensionConfiguration©);
tenantServiceRegistry.setService(service);
gateways.put(label, tenantServiceRegistry);
} catch (Exception e) {
log.info("[{}] Failed to initialize the service ", label, e);
try {
if (service != null) {
service.destroy();
}
} catch (Exception exc) {
log.info("[{}] Failed to stop the service ", label, exc);
}
}
} else {
String label = configuration.getLabel();
log.info(“[{}] Initializing gateway”, configuration.getLabel());
GatewayService service = null;
try {
TenantServiceRegistry tenantServiceRegistry = new TenantServiceRegistry();
// isRemoteConfiguration为false时,会忽略服务端返回的配置信息
service = getGatewayService(configuration, c -> {});
tenantServiceRegistry.setService(service);
for (TbExtensionConfiguration extensionConfiguration : configuration.getExtensions()) {
log.info(“[{}] Initializing extension: [{}]”, configuration.getLabel(), extensionConfiguration.getType());
ExtensionService extension = tenantServiceRegistry.createExtensionServiceByType(service, extensionConfiguration.getType());
extension.init(extensionConfiguration, isRemoteConfiguration);
if (extensionConfiguration.getType().equals(“HTTP”)) {
httpServices.add((HttpService) extension);
}
}
gateways.put(label, (TenantServiceRegistry) tenantServiceRegistry);
} catch (Exception e) {
log.info("[{}] Failed to initialize the service ", label, e);
try {
if (service != null) {
service.destroy();
}
} catch (Exception exc) {
log.info("[{}] Failed to stop the service ", label, exc);
}
}
}
}
}
gateway通过mqtt连接上tb后,会向tb发送获取configuration配置的rpc请求,具体在MqttGatewayService的initMqttClient方法中:
private MqttClient initMqttClient() {
try {
MqttClientConfig mqttClientConfig = getMqttClientConfig();
mqttClientConfig.setUsername(connection.getSecurity().getAccessToken());
tbClient = MqttClient.create(mqttClientConfig, this);
tbClient.setCallback(this);
tbClient.setEventLoop(nioEventLoopGroup);
Promise connectResult = (Promise) tbClient.connect(connection.getHost(), connection.getPort());
connectResult.addListener(future -> {
if (future.isSuccess()) {
MqttConnectResult result = (MqttConnectResult) future.getNow();
log.debug(“Gateway connect result code: [{}]”, result.getReturnCode());
} else {
log.error(“Unable to connect to mqtt server!”);
if (future.cause() != null) {
log.error(future.cause().getMessage(), future.cause());
}
}
});
connectResult.get(connection.getConnectionTimeout(), TimeUnit.MILLISECONDS);
最后
由于篇幅原因,就不多做展示了
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
zi0-1714680432179)]
[外链图片转存中…(img-A7DNTlXy-1714680432179)]
由于篇幅原因,就不多做展示了
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!