在社区或者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。