Commit 98a1c0d7 authored by Liuhaoyang's avatar Liuhaoyang

Refactor ApplicationService

parent 1e847fad
......@@ -20,5 +20,20 @@ namespace SkyWalking.Config
{
public class CollectorConfig
{
/// <summary>
/// service registry check interval
/// </summary>
public static long ServiceRegisterCheckInterval { get; set; } = 3;
/// <summary>
/// Collector agent_gRPC/grpc service addresses.
/// By using this, no discovery mechanism provided. The agent only uses these addresses to uplink data.
/// Recommend to use this only when collector cluster IPs are unreachable from agent side. Such as:
/// 1. Agent and collector cluster are in different VPC in Cloud.
/// 2. Agent uplinks data to collector cluster through Internet.
/// Single collector:DirectServers="127.0.0.1:11800"
/// Collector cluster:DirectServers="10.2.45.126:11800,10.2.45.127:11800"
/// </summary>
public static string DirectServers { get; set; }
}
}
\ No newline at end of file
......@@ -17,6 +17,7 @@
*/
using System.Collections.Generic;
using System.Linq;
using SkyWalking.Dictionarys;
namespace SkyWalking.Config
......@@ -36,10 +37,15 @@ namespace SkyWalking.Config
public static class Collector
{
private static IList<string> _grpcServers;
/// <summary>
/// Collector GRPC-Service address.
/// </summary>
public static IList<string> gRPCServers = new List<string>();
public static IList<string> gRPCServers
{
get => _grpcServers ?? CollectorConfig.DirectServers.Split(',').ToList();
set => _grpcServers = value;
}
}
}
}
......@@ -40,7 +40,8 @@ namespace SkyWalking.Boot
private Type[] FindServiceTypes()
{
return typeof(ServiceManager).Assembly.GetTypes().Where(x => typeof(IBootService).IsAssignableFrom(x))
return typeof(ServiceManager).Assembly.GetTypes().Where(x =>
x.IsClass && !x.IsAbstract && typeof(IBootService).IsAssignableFrom(x))
.ToArray();
}
......
......@@ -179,6 +179,8 @@ namespace SkyWalking.Context
{
}
public int Order { get; } = 1;
public Task Initialize(CancellationToken token)
{
TracingContext.ListenerManager.Add(this);
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using SkyWalking.Boot;
using SkyWalking.Config;
using SkyWalking.Context;
using SkyWalking.NetworkProtocol;
namespace SkyWalking.Remote
{
public class GrpcApplicationService : TimerService
{
public override int Order { get; } = -1;
protected override async Task Initializing(CancellationToken token)
{
var application = new Application {ApplicationCode = AgentConfig.ApplicationCode};
var applicationRegisterService =
new ApplicationRegisterService.ApplicationRegisterServiceClient(GrpcChannelManager.Instance.Channel);
var applicationMapping = await applicationRegisterService.applicationCodeRegisterAsync(application);
RemoteDownstreamConfig.Agent.ApplicationId = applicationMapping.Application.Value;
var instanceDiscoveryService =
new InstanceDiscoveryService.InstanceDiscoveryServiceClient(GrpcChannelManager.Instance.Channel);
var agentUUID = Guid.NewGuid().ToString().Replace("-", "");
var registerTime = DateTime.UtcNow.GetTimeMillis();
var hostName = Dns.GetHostName();
// var ipv4s = Dns.GetHostAddresses(hostName);
var osInfo = new OSInfo
{
Hostname = hostName,
OsName = Environment.OSVersion.ToString(),
ProcessNo = Process.GetCurrentProcess().Id
};
// foreach (var ipAddress in ipv4s.Where(x => x.AddressFamily == AddressFamily.InterNetwork))
// osInfo.Ipv4S.Add(ipAddress.ToString());
var applicationInstance = new ApplicationInstance
{
ApplicationId = applicationMapping.Application.Value,
AgentUUID = agentUUID,
RegisterTime = registerTime,
Osinfo = osInfo
};
var applicationInstanceMapping = await instanceDiscoveryService.registerInstanceAsync(applicationInstance);
RemoteDownstreamConfig.Agent.ApplicationInstanceId = applicationInstanceMapping.ApplicationInstanceId;
}
protected override TimeSpan Interval { get; } = TimeSpan.FromMinutes(1);
protected override async Task Execute(CancellationToken token)
{
var instanceDiscoveryService =
new InstanceDiscoveryService.InstanceDiscoveryServiceClient(GrpcChannelManager.Instance.Channel);
var heartbeat = new ApplicationInstanceHeartbeat
{
ApplicationInstanceId = RemoteDownstreamConfig.Agent.ApplicationInstanceId,
HeartbeatTime = DateTime.UtcNow.GetTimeMillis()
};
await instanceDiscoveryService.heartbeatAsync(heartbeat);
}
}
}
\ No newline at end of file
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System.Linq;
using System.Threading.Tasks;
using Grpc.Core;
using SkyWalking.Boot;
using SkyWalking.Config;
namespace SkyWalking.Remote
{
public class GrpcChannelManager
{
private static readonly GrpcChannelManager _client = new GrpcChannelManager();
public static GrpcChannelManager Instance => _client;
private Channel _channel;
public Channel Channel => _channel;
private GrpcChannelManager()
{
_channel = new Channel(RemoteDownstreamConfig.Collector.gRPCServers.First(), ChannelCredentials.Insecure);
}
public Task ConnectAsync()
{
return _channel.ConnectAsync();
}
public Task ShutdownAsync()
{
return _channel.ShutdownAsync();
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment