Spring Cloud cross-service data aggregation framework, springcloud
AG-Merge
Cross-service data aggregation framework of Spring Cloud
Solve the problem
It solves the pain point of splitting paging data attributes or attributes of a single object after the Spring Cloud service is split. It supports automatic injection and conversion of static data attributes (data dictionary) and dynamic primary key data, the aggregated static data is stored in guava ).
Example:
For two services, A table of service A uses the value of A table of Service B. When we query the table of Service, aggregate the values of A table in service B in the Query Process of Service.
Example
For more information about the sample code, see the ace-merge-demo module.
| ------- Ace-eureka Registration Center | ------- ace-data-merge-demo query data, Here aggregation example | ------- ace-data-provider
Add dependency for Maven
<dependency> <groupId>com.github.wxiaoqi</groupId> <artifactId>ace-merge-core</artifactId> <version>2.0-SNAPSHOT</version></dependency>
Recommended repository Configuration
<repositories> <repository> <id>oss</id> <name>oss</name> <url>https://oss.sonatype.org/content/groups/public</url> </repository> </repositories>
Startup class and Annotation
@EnableAceMerge
Application. yml Configuration
# Cross-service data merge: enabled: true guavachenummaxsize: 1000 guavacherefreshwritetime: 10 # min guavaCacheRefreshThreadPoolSize: 10 aop: # enable annotation, automatic aggregation enabled: true
Code example (@ MergeField indicates that the data of the object needs to be aggregated)
@ Retention (RetentionPolicy. RUNTIME) @ Target (value = {ElementType. METHOD, ElementType. TYPE, ElementType. FIELD}) public @ interface MergeField {/*** query value * @ return */String key () default ""; /*** target Class ** @ return */Class <? Extends Object> feign () default Object. class;/*** call method * @ return */String method () default ""; /*** whether to combine attribute values as the query value * @ return */boolean isValueNeedMerge () default false ;}
Aggregate object
Public class User {private String name; // attribute to be aggregated @ MergeField (key = "test", feign = IService2.class, method = "writeLog") private String sex; // attribute to be aggregated @ MergeField (feign = IService2.class, method = "getCitys", isValueNeedMerge = true) private String city; public User (String name, String sex, String city) {this. name = name; this. sex = sex; this. city = city;} public String getCity () {return city;} public void setCity (String city) {this. city = city;} public User (String name) {this. name = name;} public User (String name, String sex) {this. name = name; this. sex = sex;} public String getName () {return name;} public void setName (String name) {this. name = name;} public String getSex () {return sex;} public void setSex (String sex) {this. sex = sex ;}}
Aggregate Data Source Method (for example, through FeignClient or local spring bean object)
Special requirements: the input parameter must be a String, and the returned value must be Map <String, String>. The structure of the returned value is the key of the aggregate object attribute and the corresponding value.
@FeignClient("test")public interface IService2 { @RequestMapping("car/do") public Map<String, String> writeLog(String test); @RequestMapping("car/city") public Map<String, String> getCitys(String ids);}
Corresponding remote service interface
/*** @ Author ace * @ create secrets /11/20. * // @ RestController @ RequestMapping ("car") public class Service2Rest {private Logger logger = LoggerFactory. getLogger (Service2Rest. class); @ RequestMapping ("do") public Map <String, String> writeLog (String test) {logger.info ("service 2 is writing log! "); Map <String, String> map = new HashMap <String, String> (); map. put ("man", "male"); map. put ("woman", ""); return map;} @ RequestMapping ("city") public Map <String, String> getCity (String ids) {logger.info ("service 2 is writing log! "+ Ids); Map <String, String> map = new HashMap <String, String> (); map. put ("1", "Guangzhou"); map. put ("2", "Wuhan"); return map ;}}
The Biz class of the aggregate object (the following method uses the aop scanning annotation method)
@ Service @ Slf4jpublic class UserBiz {@ Autowired private MergeCore mergeCore;/*** aop annotation aggregation method * Where the returned value of the aggregation method must be list, * if it is a complex object, you need to customize your own aggregate Parser (Implementation interface IMergeResultParser) */@ MergeResult (resultParser = TestMergeResultParser. class) public List <User> getAopUser () {ArrayList <User> users = new ArrayList <User> (); for (int I = 1000; I> 0; I --) {users. add (new User ("zhangsan" + I, "man", "1"); users. add (new User ("lisi" + I, "woman", "2"); users. add (new User ("wangwu" + I, "unkonwn", "2");} return users ;} /*** manual aggregation method * @ return */public List <User> getUser () {ArrayList <User> users = new ArrayList <User> (); for (int I = 1000; I> 0; I --) {users. add (new User ("zhangsan" + I, "man", "1"); users. add (new User ("lisi" + I, "woman", "2"); users. add (new User ("wangwu" + I, "unkonwn", "2");} try {// list aggregate mergeCore. mergeResult (User. class, users); // aggregation of a single object // mergeCore. mergeOne (User. class, users. get (0);} catch (Exception e) {log. error ("data aggregation failed", e) ;}finally {return users ;}}}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.