微服务架构设计模式聚合器模式 _ JAVA
聚合器调用多个服务实现应用程序所需的功能。它可以是一个简单的Web页面,将检索到的数据进行处理展示。它也可以是一个更高层次的组合微服务,对检索到的数据增加业务逻辑后进一步发布成一个新的微服务,这符合DRY(不做重复的事(Don’t Repeat Yourself))原则。
另外,每个服务都有自己的缓存和数据库。如果聚合器是一个组合服务,那么它也有自己的缓存和数据库。聚合器可以沿X轴和Z轴独立扩展。
下面是使用spring boot构建的一个聚合器服务
项目整体目录结构如下:
aggregator-microservices —– aggregator-service——聚合服务 —– information-microservice——产品信息服务 —– inventory-microservice——产品库存服务
nventory-microservice 库存微服务
@SpringBootApplication
public class InventoryApplication {
public static void main(String[] args) {
SpringApplication.run(InventoryApplication.class, args);
}
}
/** 库存的接口,提供产品的库存查询 */
@RestController
public class InventoryController {
@RequestMapping(value = "/inventory", method = RequestMethod.GET)
public Integer getProductTitle() {
return 5;
}
}
information-microservice 产品信息微服务
@SpringBootApplication
public class InformationApplication {
public static void main(String[] args) {
SpringApplication.run(InformationApplication.class, args);
}
}
/** 信息服务,提供产品信息 */
@RestController
public class InformationController {
@RequestMapping(value = "/information", method = RequestMethod.GET)
public String getProductTitle() {
return "产品名称";
}
}
aggregator-service 产品服务
/** 聚合器聚合各种微服务的调用,收集数据并在Rest端点下进一步发布它们 */
@RestController
public class Aggregator {
// 产品信息客户端
@Resource
private ProductInformation informationClient;
// 产品库存客户端
@Resource
private ProductInventory inventoryClient;
/** 获取产品数据 */
@RequestMapping("/product")
public Product getProduct() {
Product product = new Product();
product.setTitle(informationClient.getProductTitle());
product.setQuantity(inventoryClient.getProductQuantity());
return product;
}
}
Product实体类
@RestController
public class Product {
private String title;
private Integer quantity;
public String getTitle() { return title; }
public Integer getQuantity() { return quantity; }
public void setTitle(String title) { this.title = title; }
public void setQuantity(Integer quantity) { this.quantity = quantity; }
}
public interface ProductInformation {
String getProductTitle();
}
@Component
public class ProductInformationImpl implements ProductInformation {
private static final Logger LOGGER = LoggerFactory.getLogger(ProductInformationImpl.class);
private static final String URI = "http://localhost:50014/information";
@Override
public String getProductTitle() {
String response = null;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet(URI);
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
response = EntityUtils.toString(httpResponse.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
public interface ProductInventory {
Integer getProductQuantity();
}
@Component
public class ProductInventoryImpl implements ProductInventory {
private static final Logger LOGGER = LoggerFactory.getLogger(ProductInformationImpl.class);
private static final String URI = "http://localhost:50024/inventory";
@Override
public Integer getProductQuantity() {
Integer response = null;
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
HttpGet httpGet = new HttpGet(URI);
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
response = Integer.parseInt(EntityUtils.toString(httpResponse.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
@SpringBootApplication
public class App {
/** 程序入口点
* @param args 命令行参数
*/
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
用户对聚合器进行单个调用,然后聚合器调用每个相关的微服务并收集数据,将业务逻辑应用到它们,然后将完整数据作为Rest端点发布。
除了聚合模式外,还有许多微服务相关的设计模式例如 代理微服务设计模式:根据业务需求调用不同的微服务; 链式微服务设计模式:在这种情况下,每个微服务都依赖于、链接到一系列其他微服务
推荐阅读
-
4个理由告诉你Java为何排行第一
本文由码农网 –单劼原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划!Java已经有20年的历史了,甚...
-
写给精明Java开发者的测试技巧
我们都会为我们的代码编写测试,不是吗?毫无疑问,我知道这个问题的答案可能会从“当然,但你知道怎样才能避免写测试吗?”到“必须...
-
Java 微服务框架 Redkale 入门介绍
Redkale功能Redkale虽然只有1.xM大小,但是麻雀虽小五脏俱全。既可作为服务器使用,也可当工具包使用。作为独立的工...
-
Java内存管理原理及内存区域详解
一、概述Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干不同的数据区域,这些区域都有各自的用途以及创建和销毁...
-
2015年Java开发岗位面试题归类
下面是我自己收集整理的Java岗位今天面经遇到的面试题,可以用它来好好准备面试。一、Java基础1.String...
-
Java 虚拟机类加载机制和字节码执行引擎
引言我们知道java代码编译后生成的是字节码,那虚拟机是如何加载这些class字节码文件的呢?加载之后又是如何进行方法调用的呢?...
-
Java常量池理解与总结
一.相关概念什么是常量用final修饰的成员变量表示常量,值一旦给定就无法改变!final修饰的变量有三种:静态...
-
Java 实现线程死锁
概述春节的时候去面试了一家公司,笔试题里面有一道是使用简单的代码实现线程的‘死锁’,当时没有想到这道题考的是Sync...
-
Java:过去、未来的互联网编程之王
Java对你而言是什么?一门你大学里学过的语言?一个IT行业的通用语言?你相信Java已经为下一次互联网爆炸做好了准备么?Java...
-
20个高级Java面试题汇总
本文由码农网 –小峰原创翻译,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划!这是一个高级Java面试系列题中...
