Skip to content

Instantly share code, notes, and snippets.

@adamatti

adamatti/00_README.md

Last active Nov 1, 2019
Embed
What would you like to do?
Meetup Micronaut #meetup #micronaut
take sample
mkdir -p src/main/java
touch src/main/java/Main.java
touch build.gradle
public class Main {
public static void main(String [] args){
System.out.println("Hello");
}
}
apply plugin: "java"
apply plugin: "application"
mainClassName = "Main"
jar {
manifest {
attributes("Main-Class": mainClassName)
}
}
mn create-app sample-mn --features groovy
package sample.mn
import spock.lang.Specification
import spock.lang.Unroll
// http://spockframework.org
class BasicSpock extends Specification {
def "nice features"(){
given:
def list = [1,2]
when:
list.add(3)
then:
// https://mrhaki.blogspot.com/2013/08/spocklight-using-old-method.html
list.size() == old(list.size()) + 1
}
// http://spockframework.org/spock/docs/1.3/data_driven_testing.html
@Unroll
def "#x + #y = #z"(){
when:
def result = x + y
then:
z == result
where:
x | y | z
1 | 1 | 2
1 | 2 | 3
1 | 3 | 4
}
}
package sample.mn
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
// https://guides.micronaut.io/micronaut-http-client-groovy/guide/index.html
@Controller("/hello")
class SampleController {
@Get
String sayHello(){
"Hello world"
}
}
package sample.mn
import io.micronaut.http.annotation.Get
import io.micronaut.http.client.annotation.Client
import io.micronaut.retry.annotation.CircuitBreaker
import io.micronaut.retry.annotation.Retryable
import io.reactivex.Single
// https://guides.micronaut.io/micronaut-http-client-groovy/guide/index.html
//@CircuitBreaker
@Client("https://pokeapi.co/api/v2")
interface PokemonClient {
@Get("/pokemon/{name}")
Map getPokemon(String name)
}
package sample.mn
import io.micronaut.context.ApplicationContext
import io.micronaut.runtime.server.EmbeddedServer
import io.micronaut.test.annotation.MicronautTest
import spock.lang.AutoCleanup
import spock.lang.Shared;
import spock.lang.Specification
import javax.inject.Inject;
@MicronautTest
class PokemonClientSpec extends Specification {
@Inject
private PokemonClient client
def "get pikachu"(){
when:
def response = client.getPokemon("pikachu")
println response
then:
response.name == "pikachu"
}
}
package sample.mn
import io.micronaut.configuration.kafka.annotation.KafkaClient
import io.micronaut.configuration.kafka.annotation.KafkaKey
import io.micronaut.configuration.kafka.annotation.KafkaListener
import io.micronaut.configuration.kafka.annotation.OffsetReset
import io.micronaut.configuration.kafka.annotation.Topic
import javax.inject.Inject
// https://micronaut-projects.github.io/micronaut-kafka/latest/guide/
@KafkaClient
interface ProductClient {
@Topic("my-products")
void sendProduct(@KafkaKey String brand, String name)
}
class KafkaService{
@Inject
private ProductClient client
void send(){
client.sendProduct("Nike", "Blue Trainers")
}
}
@KafkaListener(offsetReset = OffsetReset.EARLIEST)
class ProductListener {
@Topic("my-products")
void receive(@KafkaKey String brand, String name) {
println "Got Product - ${name} by ${brand}"
}
}
# https://guides.micronaut.io/micronaut-creating-first-graal-app/guide/index.html
mn create-app sample-graal --features=graal-native-image
./gradlew assemble
./docker-build.sh
docker run -p 8080:8080 sample-graal
# 03:42:29.075 [main] INFO io.micronaut.runtime.Micronaut - Startup completed in 110ms. Server Running: http://57ee6389bd17:8080
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment