26 Eylül 2021 Pazar

SpringCloud GCP

Giriş
Google Cloud Platform (GCP) içindir

Maven
Örnek
Şu satırı dahil ederiz
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>spring-cloud-gcp-dependencies</artifactId>
      <version>2.0.4</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
Örnek
Şu satırı dahil ederiz
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>libraries-bom</artifactId>
      <version>26.0.0</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
1. Google Cloud Storage
Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>com.google.cloud</groupId>
  <artifactId>google-cloud-storage</artifactId>
</dependency>
Örnek
Şöyle yaparız
import com.google.auth.oauth2.GoogleCredentials;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;

public FileDto uploadFile(MultipartFile multipartFile, String fileName, 
  String contentType) {

  try{

    LOGGER.debug("Start file uploading process on GCS");
    byte[] fileData = FileUtils.readFileToByteArray(convertFile(multipartFile));

    InputStream inputStream = new ClassPathResource(gcpConfigFile).getInputStream();

    StorageOptions options = StorageOptions.newBuilder().setProjectId(gcpProjectId)
      .setCredentials(GoogleCredentials.fromStream(inputStream)).build();

    Storage storage = options.getService();
    Bucket bucket = storage.get(gcpBucketId,Storage.BucketGetOption.fields());

    RandomString id = new RandomString(6, ThreadLocalRandom.current());
    Blob blob = bucket.create(gcpDirectoryName + "/" + fileName + "-" + 
                      id.nextString() + checkFileExtension(fileName), 
                   fileData, contentType);

    if(blob != null){
      LOGGER.debug("File successfully uploaded to GCS");
      return new FileDto(blob.getName(), blob.getMediaLink());
    }

  }catch (Exception e){
    ...
  }
}
2. Pub/Sub
Maven
Şu satırı dahil ederiz
<dependency>
<groupId>com.google.cloud</groupId> <artifactId>spring-cloud-gcp-pubsub</artifactId> </dependency>
application.properties
Şöyle yaparız
spring.cloud.gcp.pubsub.enabled=true
PubSubInboundChannelAdapter Sınıfı
Örnek
Şöyle yaparız
spring.cloud.gcp.pubsub.enabled=true
constructor
Şöyle yaparız
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.handler.annotation.Header;
 
import com.google.cloud.spring.pubsub.core.PubSubTemplate;
import com.google.cloud.spring.pubsub.integration.AckMode;
import com.google.cloud.spring.pubsub.integration.inbound.PubSubInboundChannelAdapter;
import com.google.cloud.spring.pubsub.support.BasicAcknowledgeablePubsubMessage;
import com.google.cloud.spring.pubsub.support.GcpPubSubHeaders;
 
@Configuration
public class PubSubConfiguration {
  @Bean
  public MessageChannel pubsubInputChannel() {
    return new DirectChannel();
  }
  @Bean
  public PubSubInboundChannelAdapter messageChannelAdapter(
          @Qualifier("pubsubInputChannel") MessageChannel inputChannel,
          PubSubTemplate pubSubTemplate) {
    PubSubInboundChannelAdapter adapter =
      new PubSubInboundChannelAdapter(pubSubTemplate, "your-subscription");
    adapter.setOutputChannel(inputChannel);
    adapter.setAckMode(AckMode.MANUAL);
    adapter.setPayloadType(String.class);
    return adapter;
  }
  @ServiceActivator(inputChannel = "pubsubInputChannel")
  public void messageReceiver(String payload,
    @Header(GcpPubSubHeaders.ORIGINAL_MESSAGE) BasicAcknowledgeablePubsubMessage msg) {
    var data = msg.getPubsubMessage().getData().toStringUtf8());
    msg.ack();
  }
}




Hiç yorum yok:

Yorum Gönder