1. 背景
作為微服務體系, 應該是不限語言的, 不管是.net、java
, 都可以是一個微服務. 可以使用JAVA或者.NET 去實現業務模塊,通過統一的消息模型進行傳輸調用
因客戶技術棧以多語言,多元化發展,語言包含了Java、.NET語言,並且要支持不同業務場景需要,所以surging必然要實現多語言混合微服務異構,並且支持多協議擴展以便支持不同業務場景,那么將通過幾篇文章來介紹surging 如何實現JAVA微服務以便支持多語言混合。
組件
目前JAVA微服務引擎實現了以下組件:
skywalking探針:以便支持.net和java 鏈路追蹤
rpc組件:基於netty遠程調用組件
Messagepack組件:以便支持.net和java基於messagepack 編解碼
Json組件:以便支持.net和java基於json編解碼,JAVA使用的是gson, .net使用的是Newtonsoft.Json
注冊中心組件:實現基於zookeeper,consul 的注冊中心,可以支持兼容.net和java服務注冊、服務發現、服務治理。
服務引擎構建組件:以便實現服務配置,組件注入。
使用入門
基於java寫個簡單的服務接口
@ServiceBundle(RouteTemplate = "api/{Service}") public interface IHelloService extends IServiceKey { @Command(CircuitBreakerForceOpen = false,ShuntStrategy = AddressSelectorMode.RoundRobin, Strategy = StrategyType.Failover, RequestCacheEnabled = false, InjectionNamespaces = {}, BreakerForceClosed = false, FallBackName = "") String Say(String name) throws IOException, InterruptedException, KeeperException; }
基於java寫個 簡單的服務實現
1 public class HelloService extends ServiceBase implements IHelloService { 2 3 @Inject 4 private IServiceProxyProvider serviceProxyProvider; 5 6 public String Say(String name) throws IOException, InterruptedException, KeeperException { 7 8 HashMap<String, Object> model = new HashMap<>(); 9 model.put("name", "fanly"); 10 String path = "api/manager/say"; 11 12 String result = serviceProxyProvider.Invoke(model, path, null); 13 return result; 14 // return name+":say hello"; 15 } 16 }
C# 通過以下代碼就能調用以上JAVA服務
1 Dictionary<string, object> model = new Dictionary<string, object>(); 2 model.Add("name", name); 3 string path = "api/hello/say"; 4 5 string result =await _serviceProxyProvider.Invoke<string>(model, path, null);
以下通過swagger 調用JAVA服務返回的結果
SkyWalking鏈路追蹤:
總結
surging 將繼續為企業服務,后期會支持升級支持skywalking8.0+和支持擴展haproxy, rtmp 多媒體推送服務。