微服务架构设计模式聚合器模式 _ 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端点发布。

除了聚合模式外,还有许多微服务相关的设计模式例如 代理微服务设计模式:根据业务需求调用不同的微服务; 链式微服务设计模式:在这种情况下,每个微服务都依赖于、链接到一系列其他微服务

发布于 2020-04-19 22:36:23
收藏
分享
海报
0 条评论
185
上一篇:你应该知道的Java 12新特性概述 _ JAVA 下一篇:Java注解基本介绍 二 _ JAVA
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码