在社群或者QQ群我們經常看到有人争辯程式設計語言的好壞,隻要一提起這個話題常常就能引來很多人參與,往往最後就變成了一群人幾個小時的罵戰。今天我們要說的是如何讓Java和.Net(甚至更多語言)相結合。充分發揮其優勢揚長避短。
1、Anno是什麼?
Anno是一個微服務架構引擎。入門簡單、安全、穩定、高可用、全平台可監控、依賴第三方架構少。底層通訊RPC(Remote Procedure Call)采用穩定可靠經過無數成功項目驗證過的跨語言的
thrift、
grpc
。 自帶服務注冊發現健康檢查(不依賴于Etcd、Consul、Zookeeper)、調用鍊追蹤、Cron 排程、限流、事件總線等等。
2、Java和.Net 混合開發
Java的生态很強大,但是他的文法也常常讓人诟病。提起.Net經常有人說他不能跨平台(其實早期.net 可以借助Mono跨平台,并且在多個領域取得了不可磨滅的成績),國内生态不好。随着.net core 的出現和前段時間.Net5的釋出,讓.net 跨平台更容易。并且.net 有着超高的性能、優雅的文法等等很多方面被人稱贊。在程式設計語言百花齊放的今天,我們需要揚長避短利用不同程式設計語言各自的優勢為我們提供更好的服務。就在這種場景下我們推出了 Anno微服務架構,讓微服務、跨語言混合開發變的更簡單更容易。
在Anno中調用方(Client)無需知道服務提供方(Provider)的位址、端口、開發語言等等資訊,是以我們在開發過程中對于我們需要的服務我們隻管通過Client SDK調用,無需關注細節。這樣我們也避免了在多語言開發的微服務中帶來的各種奇葩問題。
下圖是一個線上體驗的環境:http://140.143.207.244/

1 package anno.componentservice;
2
3 import anno.componentservice.Models.UserInfo;
4 import anno.componentservice.events.UserEvent;
5 import anno.configuration.AnnoTheadPool;
6 import anno.entities.SysMember;
7 import anno.repository.SysMemberMapper;
8 import anno.thrift.annotation.AnnoInfo;
9 import anno.thrift.module.ActionResult;
10 import anno.thrift.module.BaseModule;
11 import org.springframework.beans.factory.annotation.Autowired;
12 import org.springframework.context.ApplicationEventPublisher;
13 import org.springframework.stereotype.Service;
14
15 import javax.annotation.Resource;
16 import java.util.HashMap;
17 @Service
18 //@Scope("prototype")
19 public class UserInfoModule extends BaseModule {
20 @Autowired
21 private ApplicationEventPublisher publisher;
22 @Autowired
23 private SysMemberMapper sysMemberMapper;
24 @AnnoInfo(desc = "使用者資訊")
25 public ActionResult<Object> GetUserInfo(GetUserInfoRequestDto queryInput){
26 UserInfo userinfo=new UserInfo();
27 userinfo.setAge(18);
28 userinfo.setName("Tom");
29 HashMap<String,Object> output=new HashMap<String, Object>();
30 output.put("key1","value1");
31 output.put("key2","value2");
32 for(HashMap.Entry<String,Object> kv:output.entrySet()){
33 output.put(kv.getKey(),kv.getValue());
34 }
35 HashMap<String,Object> outputData=new HashMap<String, Object>();
36 outputData.put("queryInput",queryInput);
37 outputData.put("userinfo",userinfo);
38 String msg= "this message from Java Server UserInfoModule.";
39 return new ActionResult<Object>(true, outputData, output, msg);
40 }
41 @AnnoInfo(desc = "你好世界")
42 public ActionResult<Object> HelloWorld(@AnnoInfo(desc = "名稱",name = "name",required = false,defaultValue = "Anno Default Value") String anno){
43 String greetings="Hello "+anno+" I am Anno!";
44 return new ActionResult<>(true,greetings);
45 }
46 public void PublishMsg(String name){
47 if(name==null){
48 name="Anno";
49 }
50 UserEvent uv=new UserEvent();
51 uv.setId(10010);
52 uv.setName(name);
53 /**
54 * 線程池方式釋出事件
55 */
56 AnnoTheadPool.getPool().execute(()->{
57 publisher.publishEvent(uv);
58 });
59 /**
60 * 釋出事件異步 建立線程
61 */
62 // new Thread(()->{
63 // publisher.publishEvent(uv);
64 // }).start();
65 /**
66 * 直接釋出事件 同步
67 */
68 // publisher.publishEvent(uv);
69 }
70
71 @AnnoInfo(desc = "根據ID擷取使用者資訊")
72 public ActionResult<SysMember> GetUserAutowired(long id) {
73 SysMember member=sysMemberMapper.selectById(id);
74 return new ActionResult<>(true,member);
75 }
76 }
View Code
更多關于Java和.NET的混合開發的詳情,請移步到GitHub檢視
學習交流 QQ群:478399354 ,到這裡我們互為師長互相學習。
Anno核心源碼:https://github.com/duyanming/Anno.Core
Java實作:https://github.com/duyanming/anno.thrift-parent
Viper示例項目:https://github.com/duyanming/Viper
體驗位址:http://140.143.207.244/Home/Login
文檔位址:https://duyanming.github.io/
關于Anno的更多内容,随後更新。敬請關注。開源不易,感謝Star。