AG-Merge
Spring Cloud Cross-Service Data Aggregation Framework
Solve the problem
Solve the pain of splitting properties of paging data after splitting Spring Cloud service or the attributes of individual objects. It supports automatic injection and transformation of static data attributes (data dictionary) and dynamic primary key data, among which the aggregated static data will be stored first-level mixed (guava).
Take a chestnut:
Two services, a table in service A uses the value of a table in service B. When we query the table in service A, we aggregate the values of a table in service B in the query process of service A.
Example
For specific example code, you can see the ace-merge-demo module.
|-------- ace-eureka Registration Center |-------- ace-data-merge-demo Query data, aggregation example here |-------- ace-data-provider Data provider
Maven adds dependencies
<dependency> <groupId>com.github.wxiaoqi</groupId> <artifactId>ace-merge-core</artifactId> <version>2.0-SNAPSHOT</version></dependency>
Recommended warehouse configuration
<repositories> <repository> <id>oss</id> <name>oss</name> <url>https://oss.sonatype.org/content/groups/public</url> </repository> </repository> </repositories>
Start class annotation
@EnableAceMerge
application.yml configuration
# Cross-service data merge merge: enabled: true guavaCacheNumMaxSize: 1000 guavaCacheRefreshWriteTime: 10 # min guavaCacheRefreshThreadPoolSize: 10 aop: # How to start annotations, automatically aggregate enabled: true
Code example (The data of the @MergeField flag 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 merge attribute values as query values* @return */ boolean isValueNeedMerge() default false;} Aggregate objects
public class User { private String name; // Attributes that require aggregation @MergeField(key="test", feign = IService2.class,method = "writeLog") private String sex; // Attributes that require aggregation @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 (example is through FeignClient, or it can be a local spring bean object)
Special requirements: the entry parameter must be a String, and the return value must be Map<String,String>. The composition of the return value is the key and corresponding value of the aggregate object attribute.
@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 2017/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","female"); 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; }} Biz class that aggregates objects (the following method is to use the aop scan annotation)
@Service@Slf4jpublic class UserBiz { @Autowired private MergeCore mergeCore; /** * Aggregation method of aop annotation* The return value of the aggregation method must be list, * If it is a complex object, you need to customize your own aggregation parser (implementing the 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 aggregation mergeCore.mergeResult(User.class,users); // single object aggregation// 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 to everyone's learning and I hope everyone will support Wulin.com more.