JAVA-13855 Create new microservices-modules (#12612)
This commit is contained in:
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.micronaut.helloworld.client;
|
||||
|
||||
import io.micronaut.http.HttpRequest;
|
||||
import io.micronaut.http.client.annotation.Client;
|
||||
import io.micronaut.http.client.RxHttpClient;
|
||||
import io.reactivex.Single;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class ConcreteGreetingClient
|
||||
{
|
||||
private RxHttpClient httpClient;
|
||||
|
||||
public ConcreteGreetingClient(@Client("/") RxHttpClient httpClient) {
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public String greet(String name) {
|
||||
HttpRequest<String> req = HttpRequest.GET("/greet/" + name);
|
||||
return httpClient.retrieve(req).blockingFirst();
|
||||
}
|
||||
|
||||
public Single<String> greetAsync(String name) {
|
||||
HttpRequest<String> req = HttpRequest.GET("/async/greet/" + name);
|
||||
return httpClient.retrieve(req).first("An error as occurred");
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.micronaut.helloworld.client;
|
||||
|
||||
import io.micronaut.http.annotation.Get;
|
||||
import io.micronaut.http.client.annotation.Client;
|
||||
|
||||
@Client("/greet")
|
||||
public interface GreetingClient {
|
||||
|
||||
@Get("/{name}")
|
||||
String greet(String name);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.micronaut.helloworld.server;
|
||||
|
||||
import io.micronaut.runtime.Micronaut;
|
||||
|
||||
public class ServerApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Micronaut.run(ServerApplication.class);
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.micronaut.helloworld.server.controller;
|
||||
|
||||
import com.baeldung.micronaut.helloworld.server.service.GreetingService;
|
||||
import io.micronaut.http.annotation.Controller;
|
||||
import io.micronaut.http.annotation.Get;
|
||||
import io.reactivex.Single;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@Controller("/async/greet")
|
||||
public class AsyncGreetController {
|
||||
|
||||
@Inject
|
||||
private GreetingService greetingService;
|
||||
|
||||
@Get("/{name}")
|
||||
public Single<String> greet(String name) {
|
||||
return Single.just(greetingService.getGreeting() + name);
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.micronaut.helloworld.server.controller;
|
||||
|
||||
import com.baeldung.micronaut.helloworld.server.service.GreetingService;
|
||||
import io.micronaut.http.MediaType;
|
||||
import io.micronaut.http.annotation.Body;
|
||||
import io.micronaut.http.annotation.Controller;
|
||||
import io.micronaut.http.annotation.Get;
|
||||
import io.micronaut.http.annotation.Post;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@Controller("/greet")
|
||||
public class GreetController {
|
||||
|
||||
@Inject
|
||||
private GreetingService greetingService;
|
||||
|
||||
@Get("/{name}")
|
||||
public String greet(String name) {
|
||||
return greetingService.getGreeting() + name;
|
||||
}
|
||||
|
||||
@Post(value = "/{name}", consumes = MediaType.TEXT_PLAIN)
|
||||
public String setGreeting(@Body String name)
|
||||
{
|
||||
return greetingService.getGreeting() + name;
|
||||
}
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package com.baeldung.micronaut.helloworld.server.service;
|
||||
|
||||
import io.micronaut.context.annotation.Primary;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@Primary
|
||||
@Singleton
|
||||
public class EnglishGreetingService implements GreetingService {
|
||||
@Override
|
||||
public String getGreeting() {
|
||||
return "Hello ";
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.baeldung.micronaut.helloworld.server.service;
|
||||
|
||||
public interface GreetingService {
|
||||
|
||||
String getGreeting();
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.micronaut.helloworld.server.service;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class SpanishGreetingService implements GreetingService {
|
||||
@Override
|
||||
public String getGreeting() {
|
||||
return "Hola ";
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.micronaut.vs.springboot;
|
||||
|
||||
import io.micronaut.runtime.Micronaut;
|
||||
|
||||
public class CompareApplication {
|
||||
public static void main(String[] args) {
|
||||
Micronaut.run(CompareApplication.class);
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.micronaut.vs.springboot.client;
|
||||
|
||||
import io.micronaut.http.annotation.Get;
|
||||
import io.micronaut.http.client.annotation.Client;
|
||||
|
||||
@Client("/math")
|
||||
public interface ArithmeticClient {
|
||||
|
||||
@Get("/sum/{number1}/{number2}")
|
||||
String sum(float number1, float number2);
|
||||
|
||||
@Get("/subtract/{number1}/{number2}")
|
||||
String subtract(float number1, float number2);
|
||||
|
||||
@Get("/multiply/{number1}/{number2}")
|
||||
String multiply(float number1, float number2);
|
||||
|
||||
@Get("/divide/{number1}/{number2}")
|
||||
String divide(float number1, float number2);
|
||||
|
||||
@Get("/memory")
|
||||
String memory();
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.micronaut.vs.springboot.client;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import io.micronaut.http.HttpRequest;
|
||||
import io.micronaut.http.client.RxHttpClient;
|
||||
import io.micronaut.http.client.annotation.Client;
|
||||
|
||||
@Singleton
|
||||
public class ArithmeticClientImpl {
|
||||
private RxHttpClient httpClient;
|
||||
|
||||
public ArithmeticClientImpl(@Client("/") RxHttpClient httpClient) {
|
||||
this.httpClient = httpClient;
|
||||
}
|
||||
|
||||
public String sum(float number1, float number2) {
|
||||
HttpRequest<String> req = HttpRequest.GET("/math/sum/" + number1 + "/" + number2);
|
||||
return httpClient.retrieve(req)
|
||||
.blockingFirst();
|
||||
}
|
||||
|
||||
public String subtract(float number1, float number2) {
|
||||
HttpRequest<String> req = HttpRequest.GET("/math/subtract/" + number1 + "/" + number2);
|
||||
return httpClient.retrieve(req)
|
||||
.blockingFirst();
|
||||
}
|
||||
|
||||
public String multiply(float number1, float number2) {
|
||||
HttpRequest<String> req = HttpRequest.GET("/math/multiply/" + number1 + "/" + number2);
|
||||
return httpClient.retrieve(req)
|
||||
.blockingFirst();
|
||||
}
|
||||
|
||||
public String divide(float number1, float number2) {
|
||||
HttpRequest<String> req = HttpRequest.GET("/math/divide/" + number1 + "/" + number2);
|
||||
return httpClient.retrieve(req)
|
||||
.blockingFirst();
|
||||
}
|
||||
|
||||
public String memory() {
|
||||
HttpRequest<String> req = HttpRequest.GET("/math/memory");
|
||||
return httpClient.retrieve(req)
|
||||
.blockingFirst();
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.baeldung.micronaut.vs.springboot.controller;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.MemoryMXBean;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import com.baeldung.micronaut.vs.springboot.service.ArithmeticService;
|
||||
|
||||
import io.micronaut.http.annotation.Controller;
|
||||
import io.micronaut.http.annotation.Get;
|
||||
|
||||
@Controller("/math")
|
||||
public class ArithmeticController {
|
||||
@Inject
|
||||
private ArithmeticService arithmeticService;
|
||||
|
||||
@Get("/sum/{number1}/{number2}")
|
||||
public float getSum(float number1, float number2) {
|
||||
return arithmeticService.add(number1, number2);
|
||||
}
|
||||
|
||||
@Get("/subtract/{number1}/{number2}")
|
||||
public float getDifference(float number1, float number2) {
|
||||
return arithmeticService.subtract(number1, number2);
|
||||
}
|
||||
|
||||
@Get("/multiply/{number1}/{number2}")
|
||||
public float getMultiplication(float number1, float number2) {
|
||||
return arithmeticService.multiply(number1, number2);
|
||||
}
|
||||
|
||||
@Get("/divide/{number1}/{number2}")
|
||||
public float getDivision(float number1, float number2) {
|
||||
return arithmeticService.divide(number1, number2);
|
||||
}
|
||||
|
||||
@Get("/memory")
|
||||
public String getMemoryStatus() {
|
||||
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
|
||||
String memoryStats = "";
|
||||
|
||||
String init = String.format("Initial: %.2f GB \n", (double) memoryBean.getHeapMemoryUsage()
|
||||
.getInit() / 1073741824);
|
||||
String usedHeap = String.format("Used: %.2f GB \n", (double) memoryBean.getHeapMemoryUsage()
|
||||
.getUsed() / 1073741824);
|
||||
String maxHeap = String.format("Max: %.2f GB \n", (double) memoryBean.getHeapMemoryUsage()
|
||||
.getMax() / 1073741824);
|
||||
String committed = String.format("Committed: %.2f GB \n", (double) memoryBean.getHeapMemoryUsage()
|
||||
.getCommitted() / 1073741824);
|
||||
memoryStats += init;
|
||||
memoryStats += usedHeap;
|
||||
memoryStats += maxHeap;
|
||||
memoryStats += committed;
|
||||
|
||||
return memoryStats;
|
||||
}
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.micronaut.vs.springboot.service;
|
||||
|
||||
import javax.inject.Singleton;
|
||||
|
||||
@Singleton
|
||||
public class ArithmeticService {
|
||||
public float add(float number1, float number2) {
|
||||
return number1 + number2;
|
||||
}
|
||||
|
||||
public float subtract(float number1, float number2) {
|
||||
return number1 - number2;
|
||||
}
|
||||
|
||||
public float multiply(float number1, float number2) {
|
||||
return number1 * number2;
|
||||
}
|
||||
|
||||
public float divide(float number1, float number2) {
|
||||
if (number2 == 0) {
|
||||
throw new IllegalArgumentException("'number2' cannot be zero");
|
||||
}
|
||||
return number1 / number2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
micronaut:
|
||||
application:
|
||||
name: hello-world-server
|
||||
server:
|
||||
port: ${random.port}
|
||||
@@ -0,0 +1,14 @@
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<!-- encoders are assigned the type
|
||||
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="info">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.micronaut.helloworld.client;
|
||||
|
||||
import io.micronaut.context.ApplicationContext;
|
||||
import io.micronaut.runtime.server.EmbeddedServer;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class ConcreteGreetingClientUnitTest
|
||||
{
|
||||
private EmbeddedServer server;
|
||||
private ConcreteGreetingClient client;
|
||||
|
||||
@Before
|
||||
public void setup()
|
||||
{
|
||||
server = ApplicationContext.run(EmbeddedServer.class);
|
||||
client = server.getApplicationContext().getBean(ConcreteGreetingClient.class);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup()
|
||||
{
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGreeting() {
|
||||
assertEquals(client.greet("Mike"), "Hello Mike");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGreetingAsync() {
|
||||
assertEquals(client.greetAsync("Mike").blockingGet(), "Hello Mike");
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.micronaut.helloworld.client;
|
||||
|
||||
import io.micronaut.context.ApplicationContext;
|
||||
import io.micronaut.runtime.server.EmbeddedServer;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class GreetingClientUnitTest {
|
||||
private EmbeddedServer server;
|
||||
private GreetingClient client;
|
||||
|
||||
@Before
|
||||
public void setup()
|
||||
{
|
||||
server = ApplicationContext.run(EmbeddedServer.class);
|
||||
client = server.getApplicationContext().getBean(GreetingClient.class);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup()
|
||||
{
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGreeting() {
|
||||
assertEquals(client.greet("Mike"), "Hello Mike");
|
||||
}
|
||||
}
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.baeldung.micronaut.vs.springboot;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
import io.micronaut.context.ApplicationContext;
|
||||
import io.micronaut.runtime.server.EmbeddedServer;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.micronaut.vs.springboot.client.ArithmeticClientImpl;
|
||||
|
||||
public class ArithmeticClientUnitTest {
|
||||
private EmbeddedServer server;
|
||||
private ArithmeticClientImpl client;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
server = ApplicationContext.run(EmbeddedServer.class);
|
||||
client = server.getApplicationContext()
|
||||
.getBean(ArithmeticClientImpl.class);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
server.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoNumbers_whenAdd_thenCorrectAnswerReturned() {
|
||||
String expected = Float.valueOf(10 + 20).toString();
|
||||
assertEquals(expected, client.sum(10, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoNumbers_whenSubtract_thenCorrectAnswerReturned() {
|
||||
String expected = Float.valueOf(20 - 10).toString();
|
||||
assertEquals(expected, client.subtract(20, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoNumbers_whenMultiply_thenCorrectAnswerReturned() {
|
||||
String expected = Float.valueOf(10 * 20).toString();
|
||||
assertEquals(expected, client.multiply(10, 20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoNumbers_whenDivide_thenCorrectAnswerReturned() {
|
||||
String expected = Float.valueOf(30 / 10).toString();
|
||||
assertEquals(expected, client.divide(30, 10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMemory_thenCorrectAnswerReturned() {
|
||||
String expected = "Initial:";
|
||||
assertThat(client.memory(), containsString(expected));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user