日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

基于Rx-netty和Karyon2的云就绪微服务

發布時間:2023/12/3 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 基于Rx-netty和Karyon2的云就绪微服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Netflix Karyon提供了一個干凈的框架來創建可用于云的微服務。 在您的組織中,如果您使用包含Eureka的Netflix OSS堆棧進行服務注冊和發現,使用Archaius進行資產管理,那么很可能會使用Karyon創建微服務。

Karyon最近發生了很多變化,我的目的是使用新版本的Karyon記錄一個好的樣本。 舊的細胞核(稱之為Karyon1)是基于JAX-RS 1.0規格與新澤西的實施,細胞核(Karyon2)的新版本仍然支持球衣也鼓勵使用RX-的Netty這是一個定制版本的Netty用支持Rx-java 。

話雖如此,讓我跳入一個樣本。 我對這個樣本的目標是創建一個“乒乓”微服務,該服務接受一個“ POST”消息并返回一個“確認”消息。

以下是一個示例請求:

{ "id": "id", "payload":"Ping" }

和預期的響應:

{"id":"id","received":"Ping","payload":"Pong"}

第一步是創建一個RequestHandler ,顧名思義,它是一個用于處理傳入請求路由的RX-Netty組件:

package org.bk.samplepong.app;import com.fasterxml.jackson.databind.ObjectMapper; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpResponseStatus; import io.reactivex.netty.protocol.http.server.HttpServerRequest; import io.reactivex.netty.protocol.http.server.HttpServerResponse; import io.reactivex.netty.protocol.http.server.RequestHandler; import netflix.karyon.transport.http.health.HealthCheckEndpoint; import org.bk.samplepong.domain.Message; import org.bk.samplepong.domain.MessageAcknowledgement; import rx.Observable;import java.io.IOException; import java.nio.charset.Charset;public class RxNettyHandler implements RequestHandler<ByteBuf, ByteBuf> {private final String healthCheckUri;private final HealthCheckEndpoint healthCheckEndpoint;private final ObjectMapper objectMapper = new ObjectMapper();public RxNettyHandler(String healthCheckUri, HealthCheckEndpoint healthCheckEndpoint) {this.healthCheckUri = healthCheckUri;this.healthCheckEndpoint = healthCheckEndpoint;}@Overridepublic Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {if (request.getUri().startsWith(healthCheckUri)) {return healthCheckEndpoint.handle(request, response);} else if (request.getUri().startsWith("/message") && request.getHttpMethod().equals(HttpMethod.POST)) {return request.getContent().map(byteBuf -> byteBuf.toString(Charset.forName("UTF-8"))).map(s -> {try {Message m = objectMapper.readValue(s, Message.class);return m;} catch (IOException e) {throw new RuntimeException(e);}}).map(m -> new MessageAcknowledgement(m.getId(), m.getPayload(), "Pong")).flatMap(ack -> {try {return response.writeStringAndFlush(objectMapper.writeValueAsString(ack));} catch (Exception e) {response.setStatus(HttpResponseStatus.BAD_REQUEST);return response.close();}});} else {response.setStatus(HttpResponseStatus.NOT_FOUND);return response.close();}} }

該流程是完全異步的,并由RX-java庫在內部進行管理,Java 8 Lambda表達式也有助于使代碼簡潔。 您將在這里看到的一個問題是路由邏輯(控制器的哪個uri)與實際的控制器邏輯混合在一起,我認為這已得到解決 。

有了這個RequestHandler,就可以使用原始的RX-Netty在獨立的Java程序中啟動服務器,實質上就是這樣,將在端口8080處啟動一個端點來處理請求:

public final class RxNettyExample {public static void main(String... args) throws Exception {final ObjectMapper objectMapper = new ObjectMapper();RxNettyHandler handler = new RxNettyHandler();HttpServer<ByteBuf, ByteBuf> server = RxNetty.createHttpServer(8080, handler);server.start();

但是,這是本機的Rx-netty方法,對于支持云的微服務,必須進行一些操作,該服務應在Eureka上注冊,并應響應Eureka的運行狀況檢查,并應該能夠使用Archaius加載屬性。

因此,對于Karyon2,主程序中的啟動看起來有點不同:

package org.bk.samplepong.app;import netflix.adminresources.resources.KaryonWebAdminModule; import netflix.karyon.Karyon; import netflix.karyon.KaryonBootstrapModule; import netflix.karyon.ShutdownModule; import netflix.karyon.archaius.ArchaiusBootstrapModule; import netflix.karyon.eureka.KaryonEurekaModule; import netflix.karyon.servo.KaryonServoModule; import netflix.karyon.transport.http.health.HealthCheckEndpoint; import org.bk.samplepong.resource.HealthCheck;public class SamplePongApp {public static void main(String[] args) {HealthCheck healthCheckHandler = new HealthCheck();Karyon.forRequestHandler(8888,new RxNettyHandler("/healthcheck",new HealthCheckEndpoint(healthCheckHandler)),new KaryonBootstrapModule(healthCheckHandler),new ArchaiusBootstrapModule("sample-pong"),KaryonEurekaModule.asBootstrapModule(),Karyon.toBootstrapModule(KaryonWebAdminModule.class),ShutdownModule.asBootstrapModule(),KaryonServoModule.asBootstrapModule()).startAndWaitTillShutdown();} }

現在它基本上已經可以使用云了,該版本的程序在啟動時將在Eureka進行干凈注冊并公開運行狀況檢查端點。 它還在端口8077處公開了一套簡潔的管理端點。

結論

我希望這對使用Karyon2開發基于Netflix OSS的內容有很好的介紹。 整個示例可在我的github倉庫中找到 :https://github.com/bijukunjummen/sample-ping-pong-netflixoss/tree/master/sample-pong。 作為后續,我將展示如何使用spring-cloud開發相同的服務,這是Spring創建微服務的方式。

翻譯自: https://www.javacodegeeks.com/2015/06/rx-netty-and-karyon2-based-cloud-ready-microservice.html

總結

以上是生活随笔為你收集整理的基于Rx-netty和Karyon2的云就绪微服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。