Whiteboard pattern for hosting http4s server in OSGi container.
Here's the simplistic implementation of HTTP Whiteboard idea using http4s
Feel free to share your ideas and discuss features at gitter
BTW: as of November 2023 I'm actively looking for the Scala job. Please contact me if you have any proposals.
Also, you can
I like deploying to OSGi containers. This is how JVM based microservices should look like, not like tones of docker images each containing its own JVM running in K8s and communicating via snail-fast network RPC.
OSGi container is an observable, controllable, dynamic system. When you replace one service everything else keeps running and all dependencies are automatically updated in container. You keep the hot JVM with all the code it pre-compiled and all the optimisation it made so far.
Each JVM gets enough CPU power to do its magic, not "0.2 vCPU" and you don't have to pay the tax in memory for running separate instance of JVM for every microservice.
The downside is: if you get OOM-killed the whole container goes down.
The solution is extremely simple though: do not become OOM.
JVM is designed to work for prolonged periods of time and it's fantastic in doing so.
Combined with cats-effect asynchronous runtime we get extremely good performance and CPU utilization.
See the example http4s routes provider here
karaf
karaf@root()> feature:repo-add https://raw.githubusercontent.com/p-pavel/osgi-scala/main/scala-libs.xml # Scala libraries
karaf@root()> feature:repo-add https://raw.githubusercontent.com/p-pavel/http4s-whiteboard/main/feature.xml # repo containing http4s-whiteboard feature
karaf@root()> feature:install com.perikov.http4s-whiteboard
karaf@root()> scr:list
# ServiceComponentRuntimeMBean in bundle 53 (org.apache.karaf.scr.management:4.4.4) enabled, 1 instance.
# Id: 0, State:ACTIVE
# ServiceComponentRuntimeBundleStateService in bundle 54 (org.apache.karaf.scr.state:4.4.4) enabled, 1 instance.
# Id: 1, State:ACTIVE
# com.perikov.osgi.http4s.whiteboard.server.Server in bundle 56 (com.perikov.http4s.whiteboard:0.1.0.SNAPSHOT) enabled, 1 instance.
# Id: 2, State:ACTIVE
# com.perikov.osgi.http4s.whiteboard.server.SampleRoute in bundle 56 (com.perikov.http4s.whiteboard:0.1.0.SNAPSHOT) disabled, 0 instances.
karaf@root()> scr:enable SampleRoute
karaf@root()> http4s:list
# path │ service.bundleid │ service.id │ component.name
# ──────┼──────────────────┼────────────┼──────────────────────────────────────────────────────
# /time │ 84 │ 109 │ com.perikov.osgi.http4s.whiteboard.server.SampleRoute
Now you server is running on port 8801.
You can change the port via configuration (container will restart the server automagically):
karaf@root()> config:property-set -p com.perikov.osgi.http4s.whiteboard.server.Server port 8802
The project needs Scala libraries hosted in OSGi container to run (you can find Karaf feature repository here).
The project uses declarative services. In Karaf you can install them with feature:install scr