helm java client_Helm:与Consul交互的本地Scala客户端

Helm

logo.png

68747470733a2f2f7472617669732d63692e6f72672f566572697a6f6e2f68656c6d2e7376673f6272616e63683d6d617374657268747470733a2f2f6d6176656e2d6261646765732e6865726f6b756170702e636f6d2f6d6176656e2d63656e7472616c2f696f2e766572697a6f6e2e68656c6d2f636f72655f322e31312f62616467652e73766768747470733a2f2f636f6465636f762e696f2f67682f566572697a6f6e2f68656c6d2f6272616e63682f6d61737465722f67726170682f62616467652e737667

A native Scala client for interacting with Consul. There is currently one supported client, which uses http4s to make HTTP calls to Consul. Alternative implementations could be added with relative ease by providing an additional free interpreter for the ConsulOp algebra.

Getting Started

Add the following to your build.sbt:

libraryDependencies += "io.verizon.helm" %% "http4s" % "1.3.+"

The Helm binaries are located on maven central, so no additional resolvers are needed.

Algebra

Consul operations are specified by the ConsulOp algebra. Two examples are get and set:

import helm._

val s: ConsulOpF[Unit] = : ConsulOp.set("key", "value")

val g: ConsulOpF[Option[String]] = : ConsulOp.get("key")

These are however just descriptions of what operations the program might perform in the future, just creating these operations does not actually execute the operations. In order to perform the gets and sets, we need to use the http4s interpreter.

The http4s interpreter

First we create an interpreter, which requires an http4s client and a base url for consul:

import helm.http4s._

import org.http4s.Uri.uri

import org.http4s.client.blaze.PooledHttp1Client

val client = PooledHttp1Client()

val baseUrl = uri("http://127.0.0.1:8500")

val interpreter = new Http4sConsulClient(baseUrl, client)

Now we can apply commands to our http4s client to get back Tasks which actually interact with consul.

import scalaz.concurrent.Task

val s: Task[Unit] = helm.run(ConsulOp.set("testkey", "testvalue"))(interpreter)

val g: Task[String] = helm.run(ConsulOp.get("testkey"))(interpreter)

// actually execute the calls

s.run

g.run

Typically, the Helm algebra would be a part of a Coproduct with other algebras in a larger program, so running the Task immediately after helm.run is not typical.

Contributing

Contributions are welcome; particularly to expand the algebra with additional operations that are supported by Consul but not yet supported by Helm.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值