Azure Event Hubs Nedir?
İstemciler arasında güvenli ve ölçeklenebilir gerçek zamanlı veri alış verişi sağlayan Microsoft Azure tabanlı bir bulut çözümüdür.
Güvenlik
Event Hubs; CSA STAR, ISO, SOC, GxP, HIPAA, HITRUST ve PCI sertifikalarına sahiptir.
Diğer azure hizmetleri gibi ölçeklenebilmektedir. İhtiyacınız kadar kullanarak daha az maliyetle çözüm sağlayabilirsiniz.
İletişim
AMQP, HTTPS ve Apache Kafka® dahil olmak üzere popüler protokolleri desteklemektedir. Dilediğiniz yerden veri alabilir ve farklı platformlarda uygulama geliştirebilirsiniz.
Çalışma mantığını daha iyi anlayabilmek için bir örnek üzerinde inceleme yapalım. Örneğimizde iki farklı microservice kullanacağız bu iki servis kendi aralarında Azure Hub kullanarak haberleşecekler. Öncelikle Azure üzerinde bir Event Hubs Instance oluşturalım.
Burada belirleyeceğimiz Namespace, Resource group ve location bilgilerini unutmayalım daha sonra kullanacağız. Next diyerek diğer adımları geçebilirsiniz. (Bu adımlarda şimdilik bir şey seçmeyeceğiz.)
Şimdi oluşturduğumuz Instance üzerinde yeni bir hub oluşturalım.
Name kısmını doldurarak geçebilirsiniz. Örneğimiz için partition ve retention alanları yeterli olacaktır.
Eventlerimizi store edeceğimiz bir adet storage account oluşturalım.
Azure tarafında işlemlerimiz bitti şimdi bir dizin oluşturarak içine iki tane java spring-boot servis ekleyelim.
https://start.spring.io/ adresine girerek aşağıdaki bilgiler ile product-service ve application-log isimlerinde iki tane servis oluşturalım.
Her iki servisimize de aşağıdaki bağımlılığı ekleyelim. (pom.xml)
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>spring-cloud-azure-eventhubs-stream-binder</artifactId>
<version>1.2.6</version>
</dependency>
Evet kodumuzu yazmadan önce azure hub’a bağlanabilmek için Credential file oluşturalım.
cd ./product-service/src/main/resource
Azure login
az login
Accountları listeleyelim
az account list
Burada id kolonunu kopyalayarak aşağıdaki komutu çalıştıralım.
az account set -s xxxxxx-xxxxxxx-xxxxxxxx
Ardından aşağıdaki komut ile product.azureauth credential dosyamızı oluşturalım.
az ad sp create-for-rbac --sdk-auth > product.azureauth
Örnek dosya içeriği:
{ "clientId": "33333333-3333-3333-3333-333333333333", "clientSecret": "44444444-4444-4444-4444-444444444444", "subscriptionId": "11111111-1111-1111-1111-111111111111", "tenantId": "22222222-2222-2222-2222-222222222222", "activeDirectoryEndpointUrl": "https://login.microsoftonline.com", "resourceManagerEndpointUrl": "https://management.azure.com/", "activeDirectoryGraphResourceId": "https://graph.windows.net/", "sqlManagementEndpointUrl": "https://management.core.windows.net:8443/", "galleryEndpointUrl": "https://gallery.azure.com/", "managementEndpointUrl": "https://management.core.windows.net/" }
Buraya kadar her şey yolundaysa az bir yolumuz kaldı :)
Son olarak azure üzerinde tanımladığımız hub’a ait configleri tanımlayalım. Aşağıdaki config bilgileri product-service’e aittir.
server.port=8080
spring.cloud.azure.credential-file-path=product.azureauth
spring.cloud.azure.resource-group=LuisResource
spring.cloud.azure.region=West US
spring.cloud.azure.eventhub.namespace=mediumeventhub
spring.cloud.azure.eventhub.checkpoint-storage-account=mediumdemostorage
spring.cloud.stream.eventhub.bindings.input.consumer.checkpoint-mode=MANUAL
spring.cloud.stream.bindings.output.destination=mediumhub
Örneğimizde iki tane servis birbiri ile haberleşecek demiştik. Şimdi product servisimize yeni bir method ekleyerek (addProduct) örneğimize başlayalım.
EventSource.java
package com.azurehubs.productservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.messaging.Source;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@EnableBinding(Source.class)
@RestController
public class EventhubSource {
@Autowired
private Source source;
@PostMapping("/product")
public String addProduct(@RequestBody String productName) {
this.source.output().send(new GenericMessage<>(productName));
return productName;
}
}
Burada göreceğiniz üzere basit bir method içinde azure hubs’a eklediğimiz product name’i gönderiyoruz. Böylece azure hubs’ı dinleyen application-log uygulaması bu product name’i alacak şekilde dizayn edeceğiz.
Application-log servisimize yukardaki gibi (product-service) azureauth ve config bilgilerini ekleyelim.
Şimdi application-log uygulamasına aşağıdaki StreamListener’ı tanımlayalım.
EventhubSink.java
package com.azurehubs.applicationlog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
@EnableBinding(Sink.class)
public class EventhubSink {
private static final Logger LOGGER = LoggerFactory.getLogger(EventhubSink.class);
@StreamListener(Sink.INPUT)
public void handleProductMessage(String productName) {
LOGGER.info("New product message received: '{}'", productName);
}
}
Şimdi iki uygulamamızı da çalıştıralım.
mvn clean package
mvn spring-boot:run
product-service port: 8080
applicationlog-service: 8081
Şimdi product service’ bir ürün ekleyelim ve applicationlog-service uygulamasının log ekranını inceleyelim.
curl -X POST -H "Content-Type: text/plain" -d "Macbook 2020" http://localhost:8080/product
Şimdi applicationlog-service log ekranına bakabiliriz.
Evet gördüğünüz üzere product-service eklediğimiz product’dan sonra Azure Event hub’a gönderdiğimiz productName doğrudan applicationlog-service’imize ulaştı. Böylece servislerimiz bulut (Azure Event Hubs) üzerinden birbiri ile haberleşmiş oldu.
Repo:
Faydalı olması dileğiyle. Hoşça kalın…