Alpakka

Alpakka為集成各種用例,提供了各種Akka Stream連接器、集成模式和數(shù)據(jù)轉(zhuǎn)換。

例如Spring Web

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

由于采用了Reactive Streams,多個(gè)庫(kù)現(xiàn)在可以互操作,因?yàn)樗羞@些庫(kù)都實(shí)現(xiàn)了相同的接口。按照設(shè)計(jì),Akka Stream隱藏了終端用戶的原始響應(yīng)流類型,因?yàn)樗试S從響應(yīng)流分離這些類型,并允許無(wú)縫遷移到 java.util.concurrent.Flow(在Java9引入)。

此 Alpakka 模塊使您可以直接返回 Spring Web 終結(jié)點(diǎn)中的源。

在Spring Web(或Spring Boot)中使用Akka Streams是非常簡(jiǎn)單的,因?yàn)锳lpakka提供了對(duì)框架的自動(dòng)配置,這意味著Spring已經(jīng)知道了Sources和Sinks等。

所有你需要做的就是引入上面的依賴(akka-stream-alpakka-spring-web),像往常一樣啟動(dòng)你的應(yīng)用程序:

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端點(diǎn)返回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 類型均支持。

所提供的配置
自動(dòng)啟用的配置如下:

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;
    }
}

如果你想手動(dòng)配置稍有不同。

雖然這里介紹的集成有效,但并不是將Akka與服務(wù)HTTP應(yīng)用程序結(jié)合使用的最佳方式。建議使用Akka HTTP。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,569評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,275評(píng)論 6 342
  • 重要說(shuō)明:本方是翻譯自https://docs.spring.io/spring-cloud-dataflow/d...
    靜悟2020閱讀 10,802評(píng)論 1 12
  • 天使投資人的投資原則:投勢(shì)、賭人、修心。 什么樣的人值得賭? 自控力。 領(lǐng)導(dǎo)力。 有使命驅(qū)動(dòng)的偏執(zhí)狂。 思維方式獨(dú)...
    金禪子閱讀 1,330評(píng)論 0 0
  • 李萍在深圳一家奶茶店打工,她很早就沒(méi)讀書(shū)出來(lái)工作。每天都可以看到她站在路口那間奶茶店前系著圍巾給前來(lái)的學(xué)生妹和情侶...
    蔡不帥閱讀 1,244評(píng)論 10 5

友情鏈接更多精彩內(nèi)容