java 聚合模式_微服务架构设计模式聚合器模式

聚合器调用多个服务实现应用程序所需的功能。它可以是一个简单的Web页面,将检索到的数据进行处理展示。它也可以是一个更高层次的组合微服务,对检索到的数据增加业务逻辑后进一步发布成一个新的微服务,这符合DRY(不做重复的事(Don’t Repeat Yourself))原则。

另外,每个服务都有自己的缓存和数据库。如果聚合器是一个组合服务,那么它也有自己的缓存和数据库。聚合器可以沿X轴和Z轴独立扩展。

af0df95a2811d71b99d7f52fc2225591.gif

下面是使用spring boot构建的一个聚合器服务

项目整体目录结构如下:

aggregator-microservices

—– aggregator-service——聚合服务

—– information-microservice——产品信息服务

—– inventory-microservice——产品库存服务

af0df95a2811d71b99d7f52fc2225591.gif

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端点发布。

除了聚合模式外,还有许多微服务相关的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值