4 Mayıs 2021 Salı

SpringNative

Giriş
Spring 3 ile bu yazıyı iki kısma ayırmak gerekti.

1. Spring 3
Spring 3 SpringNative yazısına taşıdım

2. Spring 3'ten Önce
Daha önceki Spring sürümler için açıklama şöyle
Spring Boot offers two alternatives to create native binaries:
1. A system-dependent binary: this approach requires a local GraalVM installation with the native-image extension. It will create a non-cross-platform system-dependent binary.
For this, Spring Boot has a dedicated profile:
./mvnw -Pnative package

2. A Docker image: this approach builds a containerized version of the application. It requires a local image build, e.g., Docker. Internally, it leverages CNCF Buildpacks (but doesn’t require pack).
Spring Boot provides a Maven target for this:
./mvnw spring-boot:native-image
Eğer yerel GraalVM kurulumunu seçeceksek ilave olarak native-image extension da kurulmalıdır

Her iki yöntem için de 
1. dependency olarak spring-native eklenir. 
2. plugin  olarak şu eklenir
maven için : spring-aot-maven-plugin 
gradle için :  org.springframework.experimental.aot
Açıklaması şöyle
The Spring AOT plugin will automatically be run in the build pipeline to create a special Spring Boot JAR which is needed to run when compiled to a native image. It will try to create all needed reachability configurations for your program to work correctly as a native image.
reflection-config.json Dosyası
SpringNative reflection-config.json Dosyası yazısına taşıdım

Maven
Örnek
Şöyle yaparız
<dependency>
  <groupId>org.springframework.experimental</groupId>
  <artifactId>spring-native</artifactId>
  <version>${spring-native.version}</version>
</dependency>
<plugin>
  <groupId>org.springframework.experimental</groupId>
  <artifactId>spring-aot-maven-plugin</artifactId>
  <version>${spring-native.version}</version>
  <executions>
    <execution>
      <id>test-generate</id>
      <goals>
        <goal>test-generate</goal>
      </goals>
    </execution>
    <execution>
      <id>generate</id>
        <goals>
          <goal>generate</goal>
        </goals>
      </execution>
  </executions>
</plugin>
Gradle
Örnek
Şöyle yaparız
plugins {
  id 'org.springframework.boot' version '2.6.4'
  id 'io.spring.dependency-management' version '1.0.11.RELEASE'
  id 'java'
  id 'org.springframework.experimental.aot' version '0.11.3'
}

group = 'com.springnative.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
  maven { url 'https://repo.spring.io/release' }
  mavenCentral()
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-web'
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

tasks.named('test') {
  useJUnitPlatform()
}

tasks.named('bootBuildImage') {
  builder = 'paketobuildpacks/builder:tiny'
  environment = ['BP_NATIVE_IMAGE': 'true']
}
Şöyle yaparız
./gradlew nativeRun
build-image seçeneği
Projeyi maven ile derlesek bir tane docker image üretir. Bunu derleyip çalıştırmak için şöyle yaparız
$ mvn clean package
$ mvn spring-boot:build-image

$ docker run --name spring-native-example -p 8080:8080 spring-native-example:0.0.1-SNAPSHOT
Örnek
Şöyle yaparız
//Build a normal Spring Boot image
mvn spring-boot:build-image -DskipTests

//Build a Spring Boot native image
mvn -Pnative spring-boot:build-image -DskipTests
native-image seçeneği
Şöyle yaparız
./mvnw spring-boot:native-image
@TypeHint Anotasyonu
Kodda reflection configuration belirtmeyi sağlar
Örnek - AccessBits.FULL_REFLECTION 
Şöyle yaparız
@SpringBootApplication
@NativeHint(options = ["--enable-https"])                        <1>
@TypeHint(
    types = [
        Model::class, Data::class, Result::class, Thumbnail::class,
        Collection::class, Resource::class, Url::class, URI::class
    ],
    access = AccessBits.FULL_REFLECTION                          <2>
)
class BootNativeApplication {...}
Örnek
Şöyle yaparız
@TypeHint(
  types = [News::class],
  access = [TypeAccess.DECLARED_CONSTRUCTORS, TypeAccess.PUBLIC_METHODS]
)
@SpringBootApplication
class SpringBootKotlinReactiveApplication





Hiç yorum yok:

Yorum Gönder