Scala
Any language compiled into byte code which is run on JVM can be used to write microservices for JLupin. The example of such a language is Scala. It also supports using Java libraries inside Scala's code, so it is easy to use JLupin Client library.
Native microservice
You can use libraries written in Java in your Scala code, so you won't see any plain Java class.
Project structure and dependencies
This is standard structure of Scala project. Create directories and files as shown below.
+--+ additional-files
| |
| +--- configuration.yml
| |
| +--- log4j2.xml
|
+--+ src
| |
| +--+ main
| | |
| | +--+ scala
| | |
| | +--+ com
| | |
| | +--+ example
| | |
| | +--+ configuration
| | | |
| | | +--+ ScalaHelloWorldJLupinConfiguration.scala
| | | |
| | | +--+ ScalaHelloWorldSpringConfiguration.scala
| | |
| | +--+ service
| | |
| | +--+ impl
| | | |
| | | +--+ ExampleServiceImpl.scala
| | |
| | +--+ interfaces
| | |
| | +--+ ExampleService.scala
| |
| +--+ test
| |
| +--+ scala
|
+--- pom.xml
Configure your pom (pom.xml
):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>jlupin-platform-parent</artifactId>
<groupId>com.jlupin</groupId>
<version>1.6.0.2</version>
</parent>
<name>scala-hello-world</name>
<artifactId>scala-hello-world-implementation</artifactId>
<groupId>com.example</groupId>
<version>1.0</version>
<repositories>
<!-- Repository is also accessible using https connection: -->
<!-- https://support.jlupin.com/maven2/ -->
<repository>
<id>jlupin-central</id>
<name>jlupin</name>
<url>http://support.jlupin.com/maven2/</url>
</repository>
</repositories>
<pluginRepositories>
<!-- Repository is also accessible using https connection: -->
<!-- https://support.jlupin.com/maven2/ -->
<pluginRepository>
<id>jlupin-central</id>
<name>jlupin</name>
<url>http://support.jlupin.com/maven2/</url>
</pluginRepository>
</pluginRepositories>
<properties>
<scala.version>2.11.8</scala.version>
<scala-maven-plugin.version>3.4.6</scala-maven-plugin.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.jlupin</groupId>
<artifactId>jlupin-platform-native</artifactId>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}-app-${project.version}</finalName>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.jlupin</groupId>
<artifactId>jlupin-platform-maven-plugin</artifactId>
<executions>
<execution>
<id>jlupin-repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>jlupin-zip</id>
<goals>
<goal>zip</goal>
</goals>
<configuration>
<additionalFilesDirectories>
<param>additional-files</param>
</additionalFilesDirectories>
</configuration>
</execution>
<execution>
<id>jlupin-deploy</id>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
It is default pom.xml
for single native module microservice with only added scala support.
Microservice code
Add two files with configuration: one for JLupin (ScalaHelloWorldJLupinConfiguration
) and one for Spring container (ScalaHelloWorldSpringConfiguration
). Create package com.example.configuration
and put classed there.
package com.example.configuration
import com.jlupin.impl.container.application.spring.{JLupinAbstractSpringApplicationContainer}
import com.jlupin.interfaces.configuration.microservice.container.application.{JLupinAbstractApplicationContainerProducer}
import org.springframework.context.annotation.{AnnotationConfigApplicationContext}
import org.springframework.context.support.{AbstractApplicationContext}
class ScalaHelloWorldJLupinConfiguration extends JLupinAbstractApplicationContainerProducer {
def produceJLupinApplicationContainer = new JLupinAbstractSpringApplicationContainer {
def getAbstractApplicationContext = {
new AnnotationConfigApplicationContext(classOf[ScalaHelloWorldSpringConfiguration]).asInstanceOf[AbstractApplicationContext]
}
}
}
package com.example.configuration
import org.springframework.context.annotation.{Bean, ComponentScan, Configuration}
import scala.collection.JavaConverters._
@Configuration
@ComponentScan(Array("com.example"))
class ScalaHelloWorldSpringConfiguration {
@Bean(name = Array("jLupinRegularExpressionToRemotelyEnabled"))
def getRemotelyBeanList = {
val list = List(
"exampleService"
)
list.asJava
}
}
Microservice is configured but does nothing. Create two packages com.example.service.interfaces
and com.example.service.impl
and put service definitions in them:
package com.example.service.interfaces
trait ExampleService {
def hello(name : String): String
}
package com.example.service.impl
import com.example.service.interface.{ExampleService}
import org.springframework.stereotype.Service
@Service(value = "exampleService")
class ExampleServiceImpl extends ExampleService {
def hello(name : String) = "Hello, " + name + "!"
}
Microservice is done. You only need to add configuration for it. Create special directory for it called additional-files
. Put in there two files: configuration.yml and log4j2.xml.
Put default configuration (link) in configuration.yml
and update it's application section with proper class:
[...]
APPLICATION:
applicationContainerProducerClassName: 'com.example.configuration.ScalaHelloWorldJLupinConfiguration
[...]
Also put default configuration for Log4j2 (link).
Microservice deployment
You can deplpoy your microservice manually or automatically. Just follow instructions in documentation.
Checking microservice
You can check if your microservice is running using HTTP Elastic API and cURL for example:
curl -X POST http://localhost:8082/scala-hello-world/exampleService/hello -H 'Content-Type: application/json' -H 'X-JLNS-API-ID: ROA' -d '"Peter"'
As output you should see:
"Hello, Peter!"
RATE & DISCUSS (0)