From now on, we plan to deal with microservices created around the Play Framework (hereafter, Play). Play has a fair number of breaking changes every time the version goes up to 0.1. So, with a view to upgrading some services implemented in the previous version to the latest version, I will (re) introduce to the latest version of Play Framework 2.7 at the moment. I rarely use version 2.6, so I'll keep up with the knowledge of version 2.6.
Play makes it easy to launch a project based on sbt (seed template command below).
Latest Play Templates for Java:
sbt new playframework/play-java-seed.g8
Latest Play Templates for Scala:
sbt new playframework/play-scala-seed.g8
However, it takes a long time to start up for the first time because the dependent libraries are collected from each repository. When trying out Play, it's a good idea to hit one of the above commands, then sbt run and then collect the necessary information.
In my case, it took about 6 minutes for Play to start in developer mode, and about 30 seconds for the browser to access port 9000, finish the first compilation, and get a response. It will take longer depending on the line condition.
Read "Play 2.7.0 is here!" in the Play Framework Blog. There, eight new features of Play 2.7 are listed. Below, my impression:
A play-grpc based on akka-grpc was provided. From the perspective of developing microservices, I'm happy (although it's currently treated as Incubating). With grpc supported as standard, it will be easier to work with other services using standard schema languages such as Protocol Buffers. If you haven't used Protocol Buffers etc., get an image of the schema language below.
The days when we could only write a single web app that connects to a single RDB are over. The data may be stored in various storage technologies, the backend may be split rather than a single service, and the clients may have web, iOS, and Android versions, each in a different language. It is implemented in, and the API must also be exposed to external developers. Therefore, it is necessary to serialize and deserialize the data everywhere, and to make sure that there is no contradiction in the interpretation at both ends of the communication. However, it is painful to talk to humans in natural language for the purpose of matching, so we want to talk with machine-processable code. Therefore, I want to properly define what kind of data will come in a declarative DSL. So it's a schema language. (Exhibition [Why the schema language is important](https://qiita.com/yugui/items/160737021d25d761b353#%E3%82%B9%E3%82%AD%E3%83%BC%E3%83%9E % E8% A8% 80% E8% AA% 9E% E3% 81% AF% E3% 81% AA% E3% 81% 9C% E9% 87% 8D% E8% A6% 81% E3% 81% AA% E3 % 81% AE% E3% 81% 8B)))
To get an idea of the code, let's take a look at the Hello World grpc example in Play2.7 grpc.
Schema definition of Hello World grpc by Protocol Buffers:
//(Excerpt)
package helloworld;
service GreeterService {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}
Implementation example of Greeter Service by Play2.7.
GreeterServiceImpl.scala
/*
* Copyright (C) 2018-2019 Lightbend Inc. <https://www.lightbend.com>
*/
package example.myapp.helloworld.grpc.helloworld
import akka.stream.Materializer
import javax.inject.Inject
import javax.inject.Singleton
import scala.concurrent.Future
/** User implementation, with support for dependency injection etc */
@Singleton
class GreeterServiceImpl @Inject()(implicit mat: Materializer) extends AbstractGreeterServiceRouter(mat) {
//Receive Hello Request and Future[HelloReply]SayHello method that returns
override def sayHello(in: HelloRequest): Future[HelloReply] =
Future.successful(HelloReply(s"Hello, ${in.name}!"))
}
// #service-impl
If you've never used akka, there are some terms you can't see, but let's just look at the sayHello method. Since the response of the method is Future [Hello Reply], it can be seen that the services are exchanged asynchronously. This example is currently scala-based, but you can write it in Java as well. The above sayHello method seems to be able to be written like this in Java.
@Override
public CompletionStage<HelloReply> sayHello(HelloRequest in) {
String message = String.format("Hello, %s!", in.getName());
HelloReply reply = HelloReply.newBuilder().setMessage(message).build();
return CompletableFuture.completedFuture(reply);
}
Sample code and documentation for grpc is being developed below. https://developer.lightbend.com/docs/play-grpc https://github.com/playframework/play-grpc
It would be great if play2.7's grpc could be the trigger for major JVM-based frameworks (Spring-based) to be connected to each other via grpc. In other words, when the service grows, it may be easier to replace the parts that are heavily affected by garbage collection with C ++ or Go.
It seems that Akka Coordinated Shutdown introduced from Play 2.6 has been incorporated into the Play life cycle in 2.7. This is actually Play 2.7, and you will appreciate it when you create a service that scales up and down at any time.
Play Cache APIs are now based on Java 8-based Caffeine. Caffeine seems to be starting to be used in Spring Boot and so on. This PAI may reduce the number of cases where you have to launch redis etc. externally and cache it.
I can't comment because I haven't studied "Content Security Policy (https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP)", but I would like to take this opportunity to get started.
Reference Study of Contents Security Policy (CSP)
This seems to be nice in Java. Easily submit forms, simplify code when returning to Http requests (so that you can write without Http.Context), etc.
On the other hand, in play, the previous writing style often doesn't work every time you upgrade, so be careful about changing the writing style. The latter half of the release highlights are listed. https://www.playframework.com/documentation/2.7.x/Highlights27
You'll appreciate it when writing a REST API in Play 2.7. Personally, I would like Play to actively support graphql, which takes advantage of asynchronous base.
With the goal of Golden Week, I would like to write an example of a microservice that uses grpc as a glue (connector) in Play2.7.
Recommended Posts