Skip to content

Commit 4daef51

Browse files
khatwaniNikhilpedja4
authored andcommitted
BAEL-711 Guide_to_Microservices_using_lagom_framework Changes (eugenp#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
1 parent c73c252 commit 4daef51

21 files changed

Lines changed: 385 additions & 0 deletions

File tree

‎lagom/.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
greeting-impl/logs/*
2+
lagom-hello-world/greeting-impl/bin/classes/*
3+
lagom-hello-world/logs/application.log
4+
lagom-hello-world/weather-impl/logs/application.log

‎lagom/README‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Steps to setup from scratch
2+
3+
1) Create sbt build file "build.sbt"
4+
2) Create plugins file project/plugins.sbt
5+
3) Create build properties file project/build.properties
6+
4) Run sbt command from project root to generate project directories, generated projects at target/lagom-dynamic-projects
7+
Service Locator project: lagom-internal-meta-project-service-locator
8+
cassandra project: lagom-internal-meta-project-cassandra
9+
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.
10+
6) Weather microservice
11+
a) WeatherService Interface with method:
12+
weatherStatsForToday()
13+
b) WeatherServiceImpl to return weatherstats from a random list of weather stats
14+
15+
7) Greeting Microservice
16+
GreetingService Interface:
17+
handleGreetFrom(String user)
18+
19+
a) Reply back to user with greeting message("Hello") along with weather stats fetched from weather microservice
20+
b) Update the greeting message("Hello Again") for an existing user.
21+
22+
8) GreetingEntity: PersistentEntity<GreetingCommand, GreetingEvent, GreetingState>
23+
a) handles ReceivedGreetingCommand
24+
b) Stores ReceivedGreetingEvent events to cassandra
25+
c) Processes ReceivedGreetingEvent to update GreetingState("Hello" to "Hello Again") if required.
26+
27+
9) Register modules with lagom using application.conf files.
28+
10) Run project: sbt lagom:runAll
29+
30+
31+
Sample Run:
32+
curl http://localhost:9000/api/greeting/Nikhil;
33+
Hello Nikhil! Today's weather stats: Going to be very humid, Take Water
34+
35+
curl http://localhost:9000/api/greeting/Nikhil;
36+
Hello Again Nikhil! Today's weather stats: Going to Rain, Take Umbrella
37+
38+
39+
40+

‎lagom/build.sbt‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
organization in ThisBuild := "org.baeldung"
2+
3+
// the Scala version that will be used for cross-compiled libraries
4+
scalaVersion in ThisBuild := "2.11.7"
5+
6+
lagomKafkaEnabled in ThisBuild := false
7+
8+
lazy val greetingApi = project("greeting-api")
9+
.settings(
10+
version := "1.0-SNAPSHOT",
11+
libraryDependencies ++= Seq(
12+
lagomJavadslApi
13+
)
14+
)
15+
16+
lazy val greetingImpl = project("greeting-impl")
17+
.enablePlugins(LagomJava)
18+
.settings(
19+
version := "1.0-SNAPSHOT",
20+
libraryDependencies ++= Seq(
21+
lagomJavadslPersistenceCassandra
22+
)
23+
)
24+
.dependsOn(greetingApi, weatherApi)
25+
26+
lazy val weatherApi = project("weather-api")
27+
.settings(
28+
version := "1.0-SNAPSHOT",
29+
libraryDependencies ++= Seq(
30+
lagomJavadslApi
31+
)
32+
)
33+
34+
lazy val weatherImpl = project("weather-impl")
35+
.enablePlugins(LagomJava)
36+
.settings(
37+
version := "1.0-SNAPSHOT"
38+
)
39+
.dependsOn(weatherApi)
40+
41+
def project(id: String) = Project(id, base = file(id))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.lagom.helloworld.greeting.api;
2+
3+
import static com.lightbend.lagom.javadsl.api.Service.named;
4+
import static com.lightbend.lagom.javadsl.api.Service.restCall;
5+
6+
import com.lightbend.lagom.javadsl.api.Descriptor;
7+
import com.lightbend.lagom.javadsl.api.Service;
8+
import com.lightbend.lagom.javadsl.api.ServiceCall;
9+
import com.lightbend.lagom.javadsl.api.transport.Method;
10+
11+
import akka.NotUsed;
12+
13+
public interface GreetingService extends Service {
14+
15+
public ServiceCall<NotUsed, String> handleGreetFrom(String user);
16+
17+
@Override
18+
default Descriptor descriptor() {
19+
return named("greetingservice").withCalls(
20+
restCall(Method.GET, "/api/greeting/:fromUser", this::handleGreetFrom)
21+
).withAutoAcl(true);
22+
}
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
play.modules.enabled += org.baeldung.lagom.helloworld.greeting.impl.GreetingServiceModule
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
play.modules.enabled += org.baeldung.lagom.helloworld.greeting.impl.GreetingServiceModule
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.baeldung.lagom.helloworld.greeting.impl;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
5+
import com.google.common.base.Preconditions;
6+
import com.lightbend.lagom.javadsl.persistence.PersistentEntity;
7+
import com.lightbend.lagom.serialization.CompressedJsonable;
8+
import com.lightbend.lagom.serialization.Jsonable;
9+
10+
public interface GreetingCommand extends Jsonable {
11+
12+
@SuppressWarnings("serial")
13+
@JsonDeserialize
14+
public final class ReceivedGreetingCommand implements GreetingCommand,
15+
CompressedJsonable, PersistentEntity.ReplyType<String> {
16+
private final String fromUser;
17+
18+
@JsonCreator
19+
public ReceivedGreetingCommand(String fromUser) {
20+
this.fromUser = Preconditions.checkNotNull(fromUser, "fromUser");
21+
}
22+
23+
public String getFromUser() {
24+
return fromUser;
25+
}
26+
27+
}
28+
29+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.baeldung.lagom.helloworld.greeting.impl;
2+
3+
import java.util.Optional;
4+
5+
import org.baeldung.lagom.helloworld.greeting.impl.GreetingCommand.ReceivedGreetingCommand;
6+
import org.baeldung.lagom.helloworld.greeting.impl.GreetingEvent.ReceivedGreetingEvent;
7+
8+
import com.lightbend.lagom.javadsl.persistence.PersistentEntity;
9+
10+
public class GreetingEntity extends PersistentEntity<GreetingCommand,
11+
GreetingEvent, GreetingState> {
12+
13+
@Override
14+
public Behavior initialBehavior(Optional<GreetingState> snapshotState) {
15+
BehaviorBuilder b = newBehaviorBuilder(new GreetingState("Hello "));
16+
17+
b.setCommandHandler(ReceivedGreetingCommand.class,
18+
(cmd, ctx) -> {
19+
String fromUser = cmd.getFromUser();
20+
String currentGreeting = state().getMessage();
21+
return ctx.thenPersist(
22+
new ReceivedGreetingEvent(fromUser),
23+
evt -> ctx.reply(currentGreeting + fromUser + "!"));
24+
});
25+
26+
b.setEventHandler(ReceivedGreetingEvent.class,
27+
// We simply update the current state
28+
evt -> state().withMessage("Hello Again "));
29+
30+
return b.build();
31+
}
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.lagom.helloworld.greeting.impl;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.lightbend.lagom.serialization.Jsonable;
5+
6+
7+
public interface GreetingEvent extends Jsonable {
8+
9+
class ReceivedGreetingEvent implements GreetingEvent {
10+
private final String fromUser;
11+
12+
@JsonCreator
13+
public ReceivedGreetingEvent(String fromUser) {
14+
this.fromUser = fromUser;
15+
}
16+
17+
public String getFromUser() {
18+
return fromUser;
19+
}
20+
21+
}
22+
23+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package org.baeldung.lagom.helloworld.greeting.impl;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
6+
import org.baeldung.lagom.helloworld.greeting.api.GreetingService;
7+
import org.baeldung.lagom.helloworld.greeting.impl.GreetingCommand.ReceivedGreetingCommand;
8+
import org.baeldung.lagom.helloworld.weather.api.WeatherService;
9+
import org.baeldung.lagom.helloworld.weather.api.WeatherStats;
10+
11+
import com.google.inject.Inject;
12+
import com.lightbend.lagom.javadsl.api.ServiceCall;
13+
import com.lightbend.lagom.javadsl.persistence.PersistentEntityRef;
14+
import com.lightbend.lagom.javadsl.persistence.PersistentEntityRegistry;
15+
16+
import akka.NotUsed;
17+
18+
public class GreetingServiceImpl implements GreetingService {
19+
20+
private final PersistentEntityRegistry persistentEntityRegistry;
21+
private final WeatherService weatherService;
22+
23+
@Inject
24+
public GreetingServiceImpl(PersistentEntityRegistry persistentEntityRegistry, WeatherService weatherService) {
25+
this.persistentEntityRegistry = persistentEntityRegistry;
26+
this.weatherService = weatherService;
27+
persistentEntityRegistry.register(GreetingEntity.class);
28+
}
29+
30+
@Override
31+
public ServiceCall<NotUsed, String> handleGreetFrom(String user) {
32+
return request -> {
33+
// Look up the hello world entity for the given ID.
34+
PersistentEntityRef<GreetingCommand> ref = persistentEntityRegistry.refFor(GreetingEntity.class, user);
35+
CompletableFuture<String> greetingResponse = ref.ask(new ReceivedGreetingCommand(user))
36+
.toCompletableFuture();
37+
CompletableFuture<WeatherStats> todaysWeatherInfo =
38+
(CompletableFuture<WeatherStats>) weatherService.weatherStatsForToday().invoke();
39+
try {
40+
return CompletableFuture.completedFuture(greetingResponse.get() +
41+
" Today's weather stats: " + todaysWeatherInfo.get().getMessage());
42+
} catch (InterruptedException | ExecutionException e) {
43+
return CompletableFuture.completedFuture("Sorry Some Error at our end, working on it");
44+
}
45+
};
46+
}
47+
48+
}

0 commit comments

Comments
 (0)