0

I am building a simple proxy to point to another server. Everything works but I need to find a way to be able to set the hosts in a ClientBuilder externally most likely using Docker or maybe some sort of configuration file. Here is what I have:

import java.net.InetSocketAddress
import com.twitter.finagle.Service
import com.twitter.finagle.builder.{ServerBuilder, ClientBuilder}
import com.twitter.finagle.http.{Request, Http}
import com.twitter.util.Future
import org.jboss.netty.handler.codec.http._

object Proxy extends App {

  val client: Service[HttpRequest, HttpResponse] = {
  ClientBuilder()
    .codec(Http())
    .hosts("localhost:8888")
    .hostConnectionLimit(1)
    .build()
  }

  val server = {
    ServerBuilder()
      .codec(Http())
      .bindTo(new InetSocketAddress(8080))
      .name("TROGDOR")
      .build(client)
  }
}

If you know of a way to do this or have any ideas about it please let me know!

4

2 回答 2

0

if you want running this simple proxy in a docker container and manage the target host ip dynamically, you can try to pass a target host ip through environment variable and change your code like this

import java.net.InetSocketAddress
import com.twitter.finagle.Service
import com.twitter.finagle.builder.{ServerBuilder, ClientBuilder}
import com.twitter.finagle.http.{Request, Http}
import com.twitter.util.Future
import org.jboss.netty.handler.codec.http._

object Proxy extends App {
  val target_host = sys.env.get("TARGET_HOST")

  val client: Service[HttpRequest, HttpResponse] = {
  ClientBuilder()
    .codec(Http())
    .hosts(target_host.getOrElse("127.0.0.1:8888"))
    .hostConnectionLimit(1)
    .build()
  }

  val server = {
    ServerBuilder()
      .codec(Http())
      .bindTo(new InetSocketAddress(8080))
      .name("TROGDOR")
      .build(client)
  }
}

this will let your code read system environment variable TARGET_HOST. when you done this part, you can try to start your docker container by adding the following parameter to your docker run command:

-e "TARGET_HOST=127.0.0.1:8090"

for example docker run -e "TARGET_HOST=127.0.0.1:8090" <docker image> <docker command>

note that you can change 127.0.0.1:8090 to your target host.

于 2015-07-11T08:04:54.147 回答
0

您需要一个文件 server.properties 并将您的配置放入文件中:

HOST=host:8888

现在让 docker 在每次启动时使用 docker-entrypoint bash 脚本编写配置。添加此脚本并在 Dockerfile 中定义环境变量:

$ ENV HOST=myhost
$ ENV PORT=myport
$ ADD docker-entrypoint.sh /docker-entrypoint.sh
$ ENTRYPOINT ["/docker-entrypoint.sh"]
$ CMD ["proxy"]

写出你的 docker-entrypoint.sh:

#!/bin/bash -x

set -o errexit

cat > server.properties << EOF
HOST=${HOST}:${PORT}
EOF

if [ "$1" = 'proxy' ]; then
  launch server
fi

exec "$@"

使用您的配置和命令“proxy”启动 Docker:

$ docker run -e "HOST=host" -e "PORT=port" image proxy

当您不确定您的服务器容器 IP 地址时,您也可以进行链接:

$ docker run -e "HOST=mylinkhost" -e "PORT=port" --link myservercontainer:mylinkhost image proxy
于 2015-07-11T07:50:50.447 回答