BAEL-711 Guide_to_Microservices_using_lagom_framework Changes (#1495)

* BAEL_711_Guide_to_Microservices_using_lagom_framework Changes

* BAEL_711_Guide_to_Microservices_using_lagom_framework_v2

* BAEL_711_Guide_to_Microservices_using_lagom-Review Comments
This commit is contained in:
Nikhil Khatwani 2017-03-31 22:17:58 +05:30 committed by pedja4
parent c73c252924
commit 4daef51161
21 changed files with 385 additions and 0 deletions

4
lagom/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
greeting-impl/logs/*
lagom-hello-world/greeting-impl/bin/classes/*
lagom-hello-world/logs/application.log
lagom-hello-world/weather-impl/logs/application.log

40
lagom/README Normal file
View File

@ -0,0 +1,40 @@
Steps to setup from scratch
1) Create sbt build file "build.sbt"
2) Create plugins file project/plugins.sbt
3) Create build properties file project/build.properties
4) Run sbt command from project root to generate project directories, generated projects at target/lagom-dynamic-projects
Service Locator project: lagom-internal-meta-project-service-locator
cassandra project: lagom-internal-meta-project-cassandra
5) Create folders in all projects to follow maven like directory structure layout for project source code: src/main/java and src/main/java/resources and test folders.
6) Weather microservice
a) WeatherService Interface with method:
weatherStatsForToday()
b) WeatherServiceImpl to return weatherstats from a random list of weather stats
7) Greeting Microservice
GreetingService Interface:
handleGreetFrom(String user)
a) Reply back to user with greeting message("Hello") along with weather stats fetched from weather microservice
b) Update the greeting message("Hello Again") for an existing user.
8) GreetingEntity: PersistentEntity<GreetingCommand, GreetingEvent, GreetingState>
a) handles ReceivedGreetingCommand
b) Stores ReceivedGreetingEvent events to cassandra
c) Processes ReceivedGreetingEvent to update GreetingState("Hello" to "Hello Again") if required.
9) Register modules with lagom using application.conf files.
10) Run project: sbt lagom:runAll
Sample Run:
curl http://localhost:9000/api/greeting/Nikhil;
Hello Nikhil! Today's weather stats: Going to be very humid, Take Water
curl http://localhost:9000/api/greeting/Nikhil;
Hello Again Nikhil! Today's weather stats: Going to Rain, Take Umbrella

41
lagom/build.sbt Normal file
View File

@ -0,0 +1,41 @@
organization in ThisBuild := "org.baeldung"
// the Scala version that will be used for cross-compiled libraries
scalaVersion in ThisBuild := "2.11.7"
lagomKafkaEnabled in ThisBuild := false
lazy val greetingApi = project("greeting-api")
.settings(
version := "1.0-SNAPSHOT",
libraryDependencies ++= Seq(
lagomJavadslApi
)
)
lazy val greetingImpl = project("greeting-impl")
.enablePlugins(LagomJava)
.settings(
version := "1.0-SNAPSHOT",
libraryDependencies ++= Seq(
lagomJavadslPersistenceCassandra
)
)
.dependsOn(greetingApi, weatherApi)
lazy val weatherApi = project("weather-api")
.settings(
version := "1.0-SNAPSHOT",
libraryDependencies ++= Seq(
lagomJavadslApi
)
)
lazy val weatherImpl = project("weather-impl")
.enablePlugins(LagomJava)
.settings(
version := "1.0-SNAPSHOT"
)
.dependsOn(weatherApi)
def project(id: String) = Project(id, base = file(id))

View File

@ -0,0 +1,23 @@
package org.baeldung.lagom.helloworld.greeting.api;
import static com.lightbend.lagom.javadsl.api.Service.named;
import static com.lightbend.lagom.javadsl.api.Service.restCall;
import com.lightbend.lagom.javadsl.api.Descriptor;
import com.lightbend.lagom.javadsl.api.Service;
import com.lightbend.lagom.javadsl.api.ServiceCall;
import com.lightbend.lagom.javadsl.api.transport.Method;
import akka.NotUsed;
public interface GreetingService extends Service {
public ServiceCall<NotUsed, String> handleGreetFrom(String user);
@Override
default Descriptor descriptor() {
return named("greetingservice").withCalls(
restCall(Method.GET, "/api/greeting/:fromUser", this::handleGreetFrom)
).withAutoAcl(true);
}
}

View File

@ -0,0 +1 @@
play.modules.enabled += org.baeldung.lagom.helloworld.greeting.impl.GreetingServiceModule

View File

@ -0,0 +1 @@
play.modules.enabled += org.baeldung.lagom.helloworld.greeting.impl.GreetingServiceModule

View File

@ -0,0 +1,29 @@
package org.baeldung.lagom.helloworld.greeting.impl;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.common.base.Preconditions;
import com.lightbend.lagom.javadsl.persistence.PersistentEntity;
import com.lightbend.lagom.serialization.CompressedJsonable;
import com.lightbend.lagom.serialization.Jsonable;
public interface GreetingCommand extends Jsonable {
@SuppressWarnings("serial")
@JsonDeserialize
public final class ReceivedGreetingCommand implements GreetingCommand,
CompressedJsonable, PersistentEntity.ReplyType<String> {
private final String fromUser;
@JsonCreator
public ReceivedGreetingCommand(String fromUser) {
this.fromUser = Preconditions.checkNotNull(fromUser, "fromUser");
}
public String getFromUser() {
return fromUser;
}
}
}

View File

@ -0,0 +1,32 @@
package org.baeldung.lagom.helloworld.greeting.impl;
import java.util.Optional;
import org.baeldung.lagom.helloworld.greeting.impl.GreetingCommand.ReceivedGreetingCommand;
import org.baeldung.lagom.helloworld.greeting.impl.GreetingEvent.ReceivedGreetingEvent;
import com.lightbend.lagom.javadsl.persistence.PersistentEntity;
public class GreetingEntity extends PersistentEntity<GreetingCommand,
GreetingEvent, GreetingState> {
@Override
public Behavior initialBehavior(Optional<GreetingState> snapshotState) {
BehaviorBuilder b = newBehaviorBuilder(new GreetingState("Hello "));
b.setCommandHandler(ReceivedGreetingCommand.class,
(cmd, ctx) -> {
String fromUser = cmd.getFromUser();
String currentGreeting = state().getMessage();
return ctx.thenPersist(
new ReceivedGreetingEvent(fromUser),
evt -> ctx.reply(currentGreeting + fromUser + "!"));
});
b.setEventHandler(ReceivedGreetingEvent.class,
// We simply update the current state
evt -> state().withMessage("Hello Again "));
return b.build();
}
}

View File

@ -0,0 +1,23 @@
package org.baeldung.lagom.helloworld.greeting.impl;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.lightbend.lagom.serialization.Jsonable;
public interface GreetingEvent extends Jsonable {
class ReceivedGreetingEvent implements GreetingEvent {
private final String fromUser;
@JsonCreator
public ReceivedGreetingEvent(String fromUser) {
this.fromUser = fromUser;
}
public String getFromUser() {
return fromUser;
}
}
}

View File

@ -0,0 +1,48 @@
package org.baeldung.lagom.helloworld.greeting.impl;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import org.baeldung.lagom.helloworld.greeting.api.GreetingService;
import org.baeldung.lagom.helloworld.greeting.impl.GreetingCommand.ReceivedGreetingCommand;
import org.baeldung.lagom.helloworld.weather.api.WeatherService;
import org.baeldung.lagom.helloworld.weather.api.WeatherStats;
import com.google.inject.Inject;
import com.lightbend.lagom.javadsl.api.ServiceCall;
import com.lightbend.lagom.javadsl.persistence.PersistentEntityRef;
import com.lightbend.lagom.javadsl.persistence.PersistentEntityRegistry;
import akka.NotUsed;
public class GreetingServiceImpl implements GreetingService {
private final PersistentEntityRegistry persistentEntityRegistry;
private final WeatherService weatherService;
@Inject
public GreetingServiceImpl(PersistentEntityRegistry persistentEntityRegistry, WeatherService weatherService) {
this.persistentEntityRegistry = persistentEntityRegistry;
this.weatherService = weatherService;
persistentEntityRegistry.register(GreetingEntity.class);
}
@Override
public ServiceCall<NotUsed, String> handleGreetFrom(String user) {
return request -> {
// Look up the hello world entity for the given ID.
PersistentEntityRef<GreetingCommand> ref = persistentEntityRegistry.refFor(GreetingEntity.class, user);
CompletableFuture<String> greetingResponse = ref.ask(new ReceivedGreetingCommand(user))
.toCompletableFuture();
CompletableFuture<WeatherStats> todaysWeatherInfo =
(CompletableFuture<WeatherStats>) weatherService.weatherStatsForToday().invoke();
try {
return CompletableFuture.completedFuture(greetingResponse.get() +
" Today's weather stats: " + todaysWeatherInfo.get().getMessage());
} catch (InterruptedException | ExecutionException e) {
return CompletableFuture.completedFuture("Sorry Some Error at our end, working on it");
}
};
}
}

View File

@ -0,0 +1,18 @@
package org.baeldung.lagom.helloworld.greeting.impl;
import org.baeldung.lagom.helloworld.greeting.api.GreetingService;
import org.baeldung.lagom.helloworld.weather.api.WeatherService;
import com.google.inject.AbstractModule;
import com.lightbend.lagom.javadsl.server.ServiceGuiceSupport;
/**
* The module that binds the GreetingService so that it can be served.
*/
public class GreetingServiceModule extends AbstractModule implements ServiceGuiceSupport {
@Override
protected void configure() {
bindServices(serviceBinding(GreetingService.class, GreetingServiceImpl.class));
bindClient(WeatherService.class);
}
}

View File

@ -0,0 +1,24 @@
package org.baeldung.lagom.helloworld.greeting.impl;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@JsonDeserialize
public class GreetingState {
private final String message;
@JsonCreator
public GreetingState(String message) {
this.message = message;
}
GreetingState withMessage(String message) {
return new GreetingState(message);
}
public String getMessage() {
return message;
}
}

View File

@ -0,0 +1 @@
play.modules.enabled += org.baeldung.lagom.helloworld.greeting.impl.GreetingServiceModule

View File

@ -0,0 +1 @@
sbt.version=0.13.11

View File

@ -0,0 +1,2 @@
addSbtPlugin("com.lightbend.lagom" % "lagom-sbt-plugin" % "1.3.1")
addSbtPlugin("com.typesafe.sbteclipse" % "sbteclipse-plugin" % "3.0.0")

View File

@ -0,0 +1,28 @@
package org.baeldung.lagom.helloworld.weather.api;
import static com.lightbend.lagom.javadsl.api.Service.named;
import static com.lightbend.lagom.javadsl.api.Service.*;
import com.lightbend.lagom.javadsl.api.Descriptor;
import com.lightbend.lagom.javadsl.api.Service;
import com.lightbend.lagom.javadsl.api.ServiceCall;
import com.lightbend.lagom.javadsl.api.transport.Method;
import akka.NotUsed;
/**
* WeatherService Interface.
*/
public interface WeatherService extends Service {
// Fetch Today's Weather Stats service call
public ServiceCall<NotUsed, WeatherStats> weatherStatsForToday();
@Override
default Descriptor descriptor() {
return named("weatherservice").withCalls(
restCall(Method.GET, "/api/weather", this::weatherStatsForToday)
).withAutoAcl(true);
}
}

View File

@ -0,0 +1,32 @@
package org.baeldung.lagom.helloworld.weather.api;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public enum WeatherStats {
STATS_RAINY("Going to Rain, Take Umbrella"), STATS_HUMID("Going to be very humid, Take Water");
private final String message;
private static final List<WeatherStats> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
private static final int SIZE = VALUES.size();
private static final Random RANDOM = new Random();
WeatherStats(String msg) {
this.message = msg;
}
public static WeatherStats forToday() {
return VALUES.get(RANDOM.nextInt(SIZE));
}
public String getMessage() {
return message;
}
}

View File

@ -0,0 +1 @@
play.modules.enabled += org.baeldung.lagom.helloworld.weather.impl.WeatherServiceModule

View File

@ -0,0 +1,19 @@
package org.baeldung.lagom.helloworld.weather.impl;
import java.util.concurrent.CompletableFuture;
import org.baeldung.lagom.helloworld.weather.api.WeatherService;
import org.baeldung.lagom.helloworld.weather.api.WeatherStats;
import com.lightbend.lagom.javadsl.api.ServiceCall;
import akka.NotUsed;
public class WeatherServiceImpl implements WeatherService {
@Override
public ServiceCall<NotUsed, WeatherStats> weatherStatsForToday() {
return (req) -> CompletableFuture.completedFuture(WeatherStats.forToday());
}
}

View File

@ -0,0 +1,16 @@
package org.baeldung.lagom.helloworld.weather.impl;
import org.baeldung.lagom.helloworld.weather.api.WeatherService;
import com.google.inject.AbstractModule;
import com.lightbend.lagom.javadsl.server.ServiceGuiceSupport;
/**
* The module that binds the GreetingService so that it can be served.
*/
public class WeatherServiceModule extends AbstractModule implements ServiceGuiceSupport {
@Override
protected void configure() {
bindServices(serviceBinding(WeatherService.class, WeatherServiceImpl.class));
}
}

View File

@ -0,0 +1 @@
play.modules.enabled += org.baeldung.lagom.helloworld.weather.impl.WeatherServiceModule