Oracle发布开源的轻量级 Java 微服务框架 Helidon

近日,Oracle推出了一个新的开源框架Helidon,该项目是一个用于创建基于微服务的应用程序的Java库集合。和Payara Micro、Thorntail(之前的WildFly Swarm)、OpenLiberty、TomEE等项目一样,该项目也加入了MicroProfile家族。

Helidon最初被命名为J4C(Java for Cloud),其设计以简单、快速为目标,它包括两个版本:Helidon SE和Helidon MP。Helidon SE提供了创建微服务的三个核心API:Web服务器、配置和安全,用于构建基于微服务的应用程序,不需要应用服务器。Helidon MP支持用于构建基于微服务的应用程序的MicroProfile 1.1规范。

Helidon的架构

下面的架构图显示了Helidon SE和Helidon MP的关系。

下图说明了Helidon SE和Helidon MP所属的微服务框架类别。

Web服务器

受NodeJS和其他Java框架的启发,Helidon的Web服务器是一个异步、反应性API,运行在Netty之上。WebServer接口包括对配置、路由、错误处理以及构建度量和健康端点的支持。

快速入门示例

Helidon提供了快速入门示例来演示Helidon SE和Helidon MP之间的区别。

先借用官方示例,稍后我们手写一个示例。

在GitHub上可以找到整个官方Helidon项目。

https://github.com/oracle/helidon

构建Docker镜像

Helidon SE示例

docker build -t quickstart-se target

Helidon MP示例

docker build -t quickstart-mp target

运行Docker镜像

Helidon SE示例

docker run --rm -p 8080:8080 quickstart-se:latest

Helidon MP示例

docker run --rm -p 8080:8080 quickstart-mp:latest

测试

这两个示例都支持相同的REST接口

该示例是一个非常简单的“Hello World”问候语服务。响应使用JSON编码。例如:

curl -X GET http://localhost:8080/greet
{"message":"Hello World!"}

curl -X GET http://localhost:8080/greet/Joe
{"message":"Hello Joe!"}

curl -X PUT http://localhost:8080/greet/greeting/Hola
{"greeting":"Hola"}

curl -X GET http://localhost:8080/greet/Jose
{"message":"Hola Jose!"}

动手写一个示例

环境

Helidon需要Java 8(或更高版本)和Maven。如果要构建和部署Docker容器,则需要Docker。如果要部署到Kubernetes,则需要kubectl和Kubernetes集群

以下列表显示了最低版本

| Java SE 8或Open JDK 8 | | Maven 3.5 | | Docker 18.02 | 使用Edge通道在桌面上运行Kubernetes | | Kubectl 1.7.4 |

Maven坐标

将以下代码段添加到pom.xml文件中

<dependency>
    <groupId>io.helidon.webserver</groupId>
    <artifactId>helidon-webserver</artifactId>
    <version>0.10.1</version>
</dependency>

<dependency>
    <groupId>io.helidon.webserver</groupId>
    <artifactId>helidon-webserver-netty</artifactId>
    <version>0.10.1</version>
</dependency>

<!--  WebServer Jersey依赖-->
<dependency>
    <groupId>io.helidon.webserver</groupId>
    <artifactId>helidon-webserver-jersey</artifactId>
    <version>0.10.1</version>
</dependency>

测试方法

package com.souyunku.helidon.webserver.examples.jersey;

import io.helidon.webserver.ServerRequest;
import io.helidon.webserver.ServerResponse;
import io.helidon.webserver.jersey.JerseySupport;
import io.opentracing.SpanContext;

import javax.inject.Inject;
import javax.inject.Named;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.stream.Collectors;

@Path("/")
public class HelloWorld {

    @Inject
    private ServerRequest request;

    @Inject
    private ServerResponse response;


    @Inject
    @Named(JerseySupport.REQUEST_SPAN_CONTEXT)
    private SpanContext spanContext;


    @GET
    @Path("hello")
    public Response hello() {
        return Response.ok("Hello World !").build();
    }


    @POST
    @Path("hello")
    public Response hello(String content) {
        return Response.accepted("Hello: " + content + "!").build();
    }

    @POST
    @Path("content")
    public Response content(String content) {
        return Response.accepted(content).build();
    }

    @GET
    @Path("injection")
    public Response webServerInjection() {
        return Response.ok("request=" + request.getClass().getName()
                + "\nresponse=" + response.getClass().getName()
                + "\nspanContext=" + spanContext.getClass().getName()).build();
    }

    @GET
    @Path("headers")
    public Response headers(@Context HttpHeaders headers, @QueryParam("header") String header) {
        return Response.ok("headers=" + headers.getRequestHeader(header).stream().collect(Collectors.joining(",")))
                .build();
    }

    @GET
    @Path("query")
    public Response query(@QueryParam("a") String a, @QueryParam("b") String b) {
        return Response.accepted("a='" + a + "';b='" + b + "'").build();
    }

    @GET
    @Path("path/{num}")
    public Response path(@PathParam("num") String num) {
        return Response.accepted("num=" + num).build();
    }

    @GET
    @Path("requestUri")
    public String getRequestUri(@Context UriInfo uriInfo) {
        return uriInfo.getRequestUri().getPath();
    }
}

启动服务

package com.souyunku.helidon.webserver.examples.jersey;

import io.helidon.webserver.Routing;
import io.helidon.webserver.ServerConfiguration;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.jersey.JerseySupport;
import org.glassfish.jersey.server.ResourceConfig;

import java.io.IOException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;
import java.util.logging.LogManager;

/**
 * WebServer Jersey
 */
public final class WebServerJerseyMain {

    private WebServerJerseyMain() {
    }

    /**
     * 运行Jersey WebServer示例。
     *
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException, InterruptedException, ExecutionException, TimeoutException {
        // 配置日志记录,以避免标准的JVM默认设置
        LogManager.getLogManager().readConfiguration(WebServerJerseyMain.class.getResourceAsStream("/logging.properties"));

        // 最简单方法启动在8080端口
        startServer(ServerConfiguration.builder()
                .port(8080)
                .build());

    }

    static CompletionStage<WebServer> startServer(ServerConfiguration serverConfiguration) {

        WebServer webServer = WebServer.create(
                serverConfiguration,
                Routing.builder()
                        //在/jersey上下文根目录注册Jersey应用程序
                        .register("/jersey",
                                JerseySupport.create(new ResourceConfig(HelloWorld.class)))
                        .build());

        return webServer.start()
                .whenComplete((server, t) -> {
                    System.out.println("Jersey WebServer started.");
                    System.out.println("Try the hello world resource at: http://localhost:" + server.port() + "/jersey/hello");
                });
        // http://localhost:8080/jersey/hello
    }
}

响应:

Jersey WebServer started.
Try the hello world resource at: http://localhost:8080/jersey/hello

测试

浏览器访问

http://localhost:8080/jersey/hello

响应:

Hello World !

带参数访问

http://localhost:8080/jersey/query?a=www.souyunku.com&b=www.ymq.io

响应:

a='www.souyunku.com';b='www.ymq.io'

更多就不演示了

调用链监控 Zipkin

WebServer包括Zipkin对OpenTracing的支持。启用后,WebServer会将其跟踪事件发送到Zipkin。

Maven坐标

WebServer Zipkin支持依赖

<dependency>
    <groupId>io.helidon.webserver</groupId>
    <artifactId>helidon-webserver-zipkin</artifactId>
</dependency>

配置跟踪支持

要启用Zipkin集成,请Tracer在上 配置ServerConfiguration.Builder。

zipkin是一个开放源代码分布式的跟踪系统,由Twitter公司开源,它致力于收集服务的定时数据,以解决微服务架构中的延迟问题,包括数据的收集、存储、查找和展现。它的理论模型来自于Google Dapper 论文。

配置OpenTracing Tracer

ServerConfiguration.builder()
                   .tracer(new ZipkinTracerBuilder.forService("my-application") 
                                 .zipkin("http://10.0.0.18:9411")  
                                 .build())
                   .build()

官方文档:

https://helidon.io/docs/latest

Helidon 的 GitHub 项目地址:

https://github.com/oracle/helidon

本文测试代码

GitHub: https://github.com/souyunku/DemoProjects/tree/master/helidon-examples

往期精彩文章

Contact

  • 作者:鹏磊
  • 出处:http://www.ymq.io/2018/10/15/Helidon
  • 版权归作者所有,转载请注明出处
  • Wechat:关注公众号,搜云库,专注于开发技术的研究与知识分享

关注公众号-搜云库

转载于:https://my.oschina.net/yanpenglei/blog/2246251

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Orleans 项目基本上被认为是并行计算模型 Actor Model 的分布式版本。  虽然已经存在 Erlang 和 Akka 这样利用 Actor Model 的框架,用户仍然需要做很多工作来确保那些 actors 保持在线以及能够处理故障和恢复。Orleans 框架着眼复杂项目和 actor 管理,让用户能够编写分布式项目而无需担心。    关于自家的云计算平台,微软最大的一个卖点就是开发人员可以使用.NET、Visual Studio和其它编程工具来编写Azure应用程序。不过这并不是事情的全部,微软研究人员正在研发下一代云计算编程模式和相关工具,根据最新的资 料,Orleans就微软下一代云计算编程模式(之一)。    Orleans是一种新的编程模式,用来提升微软通用语言运行库(CLR)的抽象水平,它引入了“grains”的概念,这是一个可以在数据中心之 间迁移的计算和数据存储单元。Orleans自身还将提供很多运行时,包括Geo-Distribution、数据复制与一致行、性能监控、自适应控制、 运行时监控、分布式调试。    Orleans的宗旨就是为了创建一种既适用于客户端又适用于服务器的编程模式,简化代码调试,提高代码的可移植性。    目前已知的资料并没有任何关于Orleans开发计划的内容,Orleans也许还处在概念设计阶段,也许已经开始了初期的开发工作,这些都要耐心等待才会有答案。相关入门教程: http://www.rm5u.com/orleans/orleans-intro.html 标签:云计算

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值