mqttid后缀自动追加时间戳防止冲突 web服务器handler调用加入jwt解码后的参数 服务发现新增从本地获取全部ip进行地址匹配
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:EasyDartModule/base/discovery/Discovery.dart';
|
||||
@@ -17,11 +18,24 @@ class NacosDiscovery implements Discovery {
|
||||
Future<bool> registerInstance(String serviceName, String ip, int port,
|
||||
{String groupName = 'DEFAULT_GROUP'}) async {
|
||||
try {
|
||||
//获取本机网卡ip
|
||||
String? nip;
|
||||
for (var interface in await NetworkInterface.list()) {
|
||||
for (var addr in interface.addresses) {
|
||||
if (addr.address.startsWith(ip)) {
|
||||
nip = addr.address;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (nip != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
final response = await dio.post(
|
||||
'/nacos/v2/ns/instance',
|
||||
queryParameters: {
|
||||
'serviceName': serviceName,
|
||||
'ip': ip,
|
||||
'ip': nip ?? ip,
|
||||
'port': port,
|
||||
'groupName': groupName,
|
||||
'namespaceId': config.namespaceId,
|
||||
@@ -40,7 +54,7 @@ class NacosDiscovery implements Discovery {
|
||||
final rr = await dio
|
||||
.put("/nacos/v1/ns/instance/beat", queryParameters: {
|
||||
'serviceName': serviceName,
|
||||
'ip': ip,
|
||||
'ip': nip ?? ip,
|
||||
'port': port,
|
||||
'groupName': groupName,
|
||||
'namespaceId': config.namespaceId,
|
||||
|
||||
@@ -21,8 +21,8 @@ class Mqtt {
|
||||
if (_client != null) {
|
||||
return true;
|
||||
}
|
||||
_client =
|
||||
MqttServerClient.withPort(_config.host, _config.clientId, _config.port);
|
||||
_client = MqttServerClient.withPort(_config.host,
|
||||
"${_config.clientId}_${DateTime.now().millisecondsSinceEpoch}", _config.port);
|
||||
_client?.autoReconnect = true;
|
||||
|
||||
await _client?.connect(_config.username, _config.password);
|
||||
|
||||
@@ -4,6 +4,7 @@ import 'dart:mirrors';
|
||||
import 'package:EasyDartModule/EasyDartModule.dart';
|
||||
import 'package:EasyDartModule/base/logger/Logger.dart';
|
||||
import 'package:EasyDartModule/base/webserver/WebServer.dart';
|
||||
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
|
||||
import 'package:shelf/shelf.dart';
|
||||
import 'package:shelf/shelf_io.dart';
|
||||
import 'package:shelf_router/shelf_router.dart';
|
||||
@@ -187,7 +188,19 @@ class ShelfWebServer implements WebServer {
|
||||
//把地址加入路由
|
||||
|
||||
addRouter(m.reflectee.method, p, (req, [a, b, c, d, e, f]) {
|
||||
return im.invoke(v.simpleName, [req]).reflectee;
|
||||
//从request中获取token并解码传入
|
||||
String? token = req.headers["token"];
|
||||
dynamic payload;
|
||||
if (token != null) {
|
||||
try {
|
||||
payload = JWT.decode(token).payload;
|
||||
} catch (e) {
|
||||
//jwt解码失败
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
return im.invoke(v.simpleName,
|
||||
[req, payload ?? <String, dynamic>{}]).reflectee;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ dependencies:
|
||||
mqtt5_client: ^4.6.2
|
||||
minio: ^3.5.7
|
||||
redis: ^4.0.0
|
||||
dart_jsonwebtoken: ^2.14.2
|
||||
|
||||
dev_dependencies:
|
||||
lints: ^4.0.0
|
||||
|
||||
Reference in New Issue
Block a user