Alpakka

Alpakka为集成各种用例,提供了各种Akka Stream连接器、集成模式和数据转换。

例如Spring Web

Spring 5.0引入了Reactive Streams的兼容性。

由于采用了Reactive Streams,多个库现在可以互操作,因为所有这些库都实现了相同的接口。按照设计,Akka Stream隐藏了终端用户的原始响应流类型,因为它允许从响应流分离这些类型,并允许无缝迁移到 java.util.concurrent.Flow(在Java9引入)。

此 Alpakka 模块使您可以直接返回 Spring Web 终结点中的源。

在Spring Web(或Spring Boot)中使用Akka Streams是非常简单的,因为Alpakka提供了对框架的自动配置,这意味着Spring已经知道了Sources和Sinks等。

所有你需要做的就是引入上面的依赖(akka-stream-alpakka-spring-web),像往常一样启动你的应用程序:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }

}

你可以直接在HTTP端点返回Akka Streams:

import akka.NotUsed;
import akka.actor.ActorSystem;
import akka.stream.Materializer;
import akka.stream.javadsl.Source;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {

  @RequestMapping("/")
  public Source<String, NotUsed> index() {
    return
      Source.repeat("Hello world!")
        .intersperse("\n")
        .take(10);
  }

}

javadsl 和scaladsl Akka Stream 类型均支持。

所提供的配置
自动启用的配置如下:

import akka.actor.ActorSystem;
import akka.stream.ActorMaterializer;
import akka.stream.Materializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.ReactiveAdapterRegistry;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

@Configuration
@ConditionalOnClass(akka.stream.javadsl.Source.class)
public class SpringWebAkkaStreamsConfiguration {

    private final ActorSystem system;
    private final ActorMaterializer mat;

    @Autowired
    public SpringWebAkkaStreamsConfiguration(RequestMappingHandlerAdapter requestMappingHandlerAdapter) {
        final ReactiveAdapterRegistry registry = requestMappingHandlerAdapter.getReactiveAdapterRegistry();

        system = ActorSystem.create("SpringWebAkkaStreamsSystem");
        mat = ActorMaterializer.create(system);
        new AkkaStreamsRegistrar(mat).registerAdapters(registry);
    }

    @Bean
    @ConditionalOnMissingBean(ActorSystem.class)
    public ActorSystem getActorSystem() {
        return system;
    }

    @Bean
    @ConditionalOnMissingBean(Materializer.class)
    public ActorMaterializer getMaterializer() {
        return mat;
    }
}

如果你想手动配置稍有不同。

虽然这里介绍的集成有效,但并不是将Akka与服务HTTP应用程序结合使用的最佳方式。建议使用Akka HTTP。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,860评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,936评论 6 342
  • 重要说明:本方是翻译自https://docs.spring.io/spring-cloud-dataflow/d...
    静悟2020阅读 10,552评论 1 12
  • 天使投资人的投资原则:投势、赌人、修心。 什么样的人值得赌? 自控力。 领导力。 有使命驱动的偏执狂。 思维方式独...
    金禅子阅读 1,145评论 0 0
  • 李萍在深圳一家奶茶店打工,她很早就沒读书出来工作。每天都可以看到她站在路口那间奶茶店前系着围巾给前来的学生妹和情侣...
    蔡不帅阅读 1,139评论 10 5