分佈式開發涉及到遠程過程調用即RPC。須要集成grpc。但若是想無縫集成,則涉及到普通的請求對象轉換爲grpc請求對象。
因爲null不能序列化,因此grpc的對象屬性都會有默認值,這在開發中,很難區分,到底請求傳的是默認值仍是請求自己攜帶的值。因此使用protocol buffers 的oneof關鍵字,用於規避默認值。
新建員工類java
package com.dld.hll.financial; import java.math.BigDecimal; import java.sql.Date; import java.sql.Timestamp; import java.util.List; import java.util.Map; import java.util.Set; /** * @Author yue chao * @Date 2019/8/10 0010 下午 21:23 */ public class EmpOne { private Long empIdOne; private String empNameOne; private Boolean empSexOne; private Long totalAssetOne; private List<String> skills; private Set<Integer> lucks; private Map<String, String> relations; private Date bornDate; private Timestamp createTime; private BigDecimal salary; private DeptOne dept; // ... 省略 get set }
新建部門類sql
package com.dld.hll.financial; /** * @Author yue chao * @Date 2019/8/10 0010 下午 21:24 */ public class DeptOne { private Long deptIdOne; private String deptNameOne; private Byte deptTypeOne; // 省略 get set }
proto文件apache
syntax = "proto3"; option java_multiple_files = true; option java_package = "com.dld.hll.financial.grpc"; option java_outer_classname = "EmpProto"; message EmpOne { oneof oneof_empId { int64 empIdOne = 4; } oneof oneof_totalAssetOne { int64 totalAssetOne = 8; } oneof oneof_empName { string empNameOne = 5; } oneof oneof_empSex { bool empSexOne = 6; } DeptOne dept = 7; repeated string skills = 9; repeated int32 lucks = 10; map<string, string> relations = 11; oneof oneof_bornDate { string bornDate = 12; } oneof oneof_createTime { string createTime = 13; } oneof oneof_salary { string salary = 14; } } message DeptOne { oneof oneof_deptId { int32 deptIdOne = 4; } oneof oneof_deptName { string deptNameOne = 5; } oneof oneof_deptType { int32 deptTypeOne = 6; } }
單元測試代碼json
package com.dld.hll.financial; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.protobuf.util.JsonFormat; import org.apache.commons.lang3.StringUtils; import org.junit.Test; import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; /** * @Author yue chao * @Date 2019/8/12 0012 上午 11:40 */ public class EmpOneTest { /** * oneof 普通請求轉grpc請求,再轉普通請求 * @throws IOException */ @Test public void testOneCommonRequestTransformToGrpcRequest() throws IOException { oneCommonRequestTransformToGrpcRequest(); } public static void oneCommonRequestTransformToGrpcRequest() throws IOException { DeptOne dept = new DeptOne(); dept.setDeptIdOne(0L); dept.setDeptNameOne(StringUtils.EMPTY); dept.setDeptTypeOne((byte)0); List<String> skills = new ArrayList<>(); skills.add("EDVSD"); Set<Integer> lucks = new HashSet<>(); EmpOne emp = new EmpOne(); emp.setEmpIdOne(0L); emp.setEmpNameOne(StringUtils.EMPTY); emp.setEmpSexOne(false); emp.setDept(dept); emp.setSkills(skills); emp.setLucks(lucks); emp.setBornDate(new java.sql.Date(System.currentTimeMillis())); emp.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis())); emp.setSalary(new BigDecimal("22.33")); Gson gson = new GsonBuilder().serializeNulls() .setPrettyPrinting() .registerTypeAdapter(java.sql.Timestamp.class, new TimeStampGSON()) .registerTypeAdapter(java.sql.Date.class, new SqlDateGSON()) .create(); String empJson = gson.toJson(emp); System.out.println("普通對象json=============="); System.out.println(empJson); com.dld.hll.financial.grpc.EmpOne.Builder builder = com.dld.hll.financial.grpc.EmpOne.newBuilder(); JsonFormat.parser().merge(empJson, builder); com.dld.hll.financial.grpc.EmpOne empOneGrpc = builder.build(); StringBuilder sb = new StringBuilder(); JsonFormat.printer() .includingDefaultValueFields() // 設置採用默認值,當集合爲空的時候會使用[]表示,map爲空的時候採用{},這樣可能有效防止直接使用而遇到的空指針 .appendTo(empOneGrpc, sb); System.out.println("grpc對象Json=============="); // 能夠觀察到普通對象json與grpcJson的區別,grpcJson對象字段爲null的字段不顯示,集合或map爲空,則使用以上規則處理了 System.out.println(sb); Gson gson2 = new GsonBuilder() .setPrettyPrinting() .serializeNulls() .registerTypeAdapter(java.sql.Timestamp.class, new TimeStampGSON()) .registerTypeAdapter(java.sql.Date.class, new SqlDateGSON()) .create(); EmpOne vo = gson2.fromJson(sb.toString(), EmpOne.class); System.out.println("普通對象json=============="); System.out.println(gson2.toJson(vo)); } }
測試結果app
普通對象json============== { "empIdOne": 0, "empNameOne": "", "empSexOne": false, "totalAssetOne": null, "skills": [ "EDVSD" ], "lucks": [], "relations": null, "bornDate": "2019-08-12", "createTime": "2019-08-12 13:39:14", "salary": 22.33, "dept": { "deptIdOne": 0, "deptNameOne": "", "deptTypeOne": 0 } } grpc對象Json============== { "empIdOne": "0", "empNameOne": "", "empSexOne": false, "dept": { "deptIdOne": 0, "deptNameOne": "", "deptTypeOne": 0 }, "skills": ["EDVSD"], "lucks": [], "relations": { }, "bornDate": "2019-08-12", "createTime": "2019-08-12 13:39:14", "salary": "22.33" } 普通對象json============== { "empIdOne": 0, "empNameOne": "", "empSexOne": false, "totalAssetOne": null, "skills": [ "EDVSD" ], "lucks": [], "relations": {}, "bornDate": "2019-08-12", "createTime": "2019-08-12 13:39:14", "salary": 22.33, "dept": { "deptIdOne": 0, "deptNameOne": "", "deptTypeOne": 0 } }