借助Azure事件中心获得丰富的功能:第一部分

Ëvent hub is a big data ingestion offering from Microsoft, it leverages AMQP, HTTPS, and Apache Kafka under the hood. Event hub offers benefits like partitioning and check-pointing on the data stream, plus all the scalability your wallet can handle.

In this article I'll be going over how to setup a connection and send data to event hub using a spring boot web service. You can view the completed example here.

Setting up an Azure Account

If you haven't yet, you'll need to signup for an azure dev account. They hook you up with enough credit for what we need to do in this guide.

Creating the Event Hub Namespace

登录到Azure门户,然后在顶部的搜索框中搜索“事件中心”。 您需要选择“事件中心”选项。

Event Hub Search Result

从那里单击“事件中心”,然后单击添加(+)图标。

Adding Event Hub

填写创建表单,在此示例中,我使用的是最合理的设置。 您可能还需要为此事件中心创建一个资源组。

Event Hub Namespace Create Form

繁荣! 我们有一个事件中心名称空间设置。 命名空间充当您创建的事件中心的组织目录。

Creating the Event Hub

导航到事件中心名称空间,然后单击事件中心添加(+)图标。

Add Event Hub

给您的活动中心一个时髦的名称,根据需要扩展分区数,但对于本指南,我将其保留为2。单击表单底部的创建,我们可以开始了。

Event Hub Create Form

您应该看到一条消息,说明正在创建事件中心,完成后,从名称空间菜单导航至该中心。 您将进入一个显示所有甜美指标的仪表板,例如吞吐量和消息计数。

Event Hub Stats

在侧面菜单中,导航到“共享访问策略”,然后单击添加(+)。 给它起一个名字并赋予它管理访问权限(这使我们可以读写)。

Shared Access Policy Create

创建共享访问策略为我们提供了事件中心城堡的钥匙。 单击刚刚创建的策略,并记下连接字符串。

Sending Data to Event Hub

Adding in the event hub dependencies.

首先,将那些依赖项添加到pom文件中。 在撰写本文时,依赖项的最新版本为3.0.0。

      <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-eventhubs</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-eventhubs-eph</artifactId>
            <version>3.0.0</version>
        </dependency>

接下来,让我们创建一个配置类并创建一个EventHubClient。

Creating a client and hooking it up.

import com.microsoft.azure.eventhubs.EventHubClient;
import com.microsoft.azure.eventhubs.EventHubException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.concurrent.Executors;

@Configuration
public class EventHubConfig {

    @Value("${eventHub.connectionString}")
    private String connectionString;

    @Bean
    public EventHubClient setupEventHubConnection() throws IOException, EventHubException {
        return EventHubClient.createFromConnectionStringSync(connectionString,
                              Executors.newSingleThreadScheduledExecutor());
    }
}

在此类中,我从配置文件(在我的情况下为应用程序的yml文件)中提取connectionString,注入config类,然后将其原样传递给createFromConnectionStringSync方法,它包含一个连接字符串和一个ScheduledExecutorService宾语。 由于我们在这里没有做任何花哨的事情,因此我使用的是单线程执行器。

这是我定义yml文件的方式。

eventHub:
  connectionString: 'connections string here'

现在我们有了一个Event Hub Client bean,让我们继续创建一个使用它的服务组件。

import com.dublin.eventhub.demo.controller.Controller;
import com.dublin.eventhub.demo.model.EventPayload;
import com.microsoft.azure.eventhubs.EventData;
import com.microsoft.azure.eventhubs.EventHubClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.SerializationUtils;

import java.util.Objects;

@Service
public class EventHubService {
    private final EventHubClient eventHubClient;
    private Logger log = LoggerFactory.getLogger(Controller.class);

    @Autowired
    public EventHubService(EventHubClient eventHubClient) {
        this.eventHubClient = eventHubClient;
    }

    public void sendEvent(EventPayload test) {

        byte[] bytes = SerializationUtils.serialize(test);

        log.info("Sending message to the event hub {}", eventHubClient.getEventHubName());
        eventHubClient.send(EventData.create(Objects.requireNonNull(bytes)), test.toString());
    }
}

我们将使用构造函数注入将客户端注入到我们的服务中,从那里定义sendEvent使用客户端发送数据的方法。

要将数据发送到事件中心,我们需要将消息序列化为字节数组,将其包装在EventData对象中,然后将其传递到客户端的发送方法。

Creating a data class

对于此示例,我定义了一个简单的数据类。

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class EventPayload implements Serializable {
    private String firstName;
    private String lastName;
    private String favoriteFood;
}

请务必注意,用于事件中心的数据必须是可序列化,因此请务必实施可序列化您正在使用的数据类上,否则您将获得一个java.lang.IllegalArgumentException:无法反序列化对象信息。

接下来,我们将定义一个端点来将数据发布到该端点。

Building an Endpoint

import com.dublin.eventhub.demo.model.EventPayload;
import com.dublin.eventhub.demo.service.EventHubService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class Controller {

    private final EventHubService eventHubService;
    private Logger log = LoggerFactory.getLogger(Controller.class);

    @Autowired
    public Controller(EventHubService eventHubService) {
        this.eventHubService = eventHubService;
    }

    @PostMapping(path = "/eventhub/send")
    public ResponseEntity sendEvent(@RequestBody EventPayload payload) {
        try {
            log.info("Eventhub send endpoint called, sending {} to event hub..", payload.toString());
            eventHubService.sendEvent(payload);
        } catch (Exception e) {
            log.error("An error arose sending a message to event hub: " + e);
            return new ResponseEntity<Exception>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(HttpStatus.OK);
    }
}

我创建了POST方法“ / eventhub / send”,并将请求正文定义为我们的数据类。 从那里它只是发送到服务。

一旦完成所有这些工作,我们就应该能够运行我们的应用程序,并看到它成功连接到事件中心。

2019-09-24 13:42:21.670  INFO 16113 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-09-24 13:42:21.734  INFO 16113 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-09-24 13:42:21.734  INFO 16113 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1063 ms
2019-09-24 13:42:21.983  INFO 16113 --- [pool-1-thread-1] c.m.a.eventhubs.impl.MessagingFactory    : messagingFactory[MF_41d4fd_1569350541964], hostName[dublin-rest-demo.servicebus.windows.net], info[starting reactor instance.]
2019-09-24 13:42:21.999  INFO 16113 --- [pool-1-thread-1] c.m.azure.eventhubs.impl.ReactorHandler  : name[MF_41d4fd_1569350541964] reactor.onReactorInit
2019-09-24 13:42:22.002  INFO 16113 --- [pool-1-thread-1] c.m.a.eventhubs.impl.ConnectionHandler   : onConnectionInit hostname[dublin-rest-demo.servicebus.windows.net], connectionId[MF_41d4fd_1569350541964]
2019-09-24 13:42:22.003  INFO 16113 --- [pool-1-thread-1] c.m.a.eventhubs.impl.ConnectionHandler   : onConnectionLocalOpen hostname[dublin-rest-demo.servicebus.windows.net:5671], connectionId[MF_41d4fd_1569350541964], errorCondition[null], errorDescription[null]
2019-09-24 13:42:22.101  INFO 16113 --- [pool-1-thread-1] c.m.a.eventhubs.impl.ConnectionHandler   : onConnectionBound hostname[dublin-rest-demo.servicebus.windows.net], connectionId[MF_41d4fd_1569350541964]
2019-09-24 13:42:23.157  INFO 16113 --- [pool-1-thread-4] c.m.a.eventhubs.impl.ConnectionHandler   : onConnectionRemoteOpen hostname[dublin-rest-demo.servicebus.windows.net:5671], connectionId[MF_41d4fd_1569350541964], remoteContainer[100db877ccad41b1a689c5a458bf1fbc_G6]
2019-09-24 13:42:23.268  INFO 16113 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-09-24 13:42:23.449  INFO 16113 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2019-09-24 13:42:23.491  INFO 16113 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-09-24 13:42:23.495  INFO 16113 --- [           main] com.dublin.eventhub.demo.Application     : Started Application in 3.126 seconds (JVM running for 3.493)

Now that we're all setup, let's send some data to it. Pull up your favorite HTTP client and post some data to it. For this example I'm using Insomnia.

Insomnia Client POST

在日志中,我应该看到消息已成功发送到事件中心!

2019-09-24 13:43:25.806  INFO 16113 --- [nio-8080-exec-1] c.d.eventhub.demo.controller.Controller  : Eventhub send endpoint called, sending EventPayload(firstName=Johnny, lastName=Carson, email=null, favoriteFood=Potatoes and Molasses) to event hub..
2019-09-24 13:43:25.808  INFO 16113 --- [nio-8080-exec-1] c.d.eventhub.demo.controller.Controller  : Sending message to the event hub event-hub-test

我们做到了,我们正在序列化数据并将其发送到事件中心。 😁

Recap

In this guide I walked through creating an event hub in Azure, setting up an event hub client and service, along with an endpoint to post data to.
In part 2 I'll walk through consuming events.

希望本指南对您有所帮助,让我知道您在评论中的想法。

from: https://dev.to//danondso/getting-eventful-with-azure-event-hubs-part-one-3jn1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值