Dropwizardのデフォルトポートを変更します
-
21-12-2019 - |
質問
デフォルトのポート8080(サービス)と8081(管理者)で実行されているDropwizardベースのジャージーRESTサービスを持っています、私はデフォルトのポートを一般的に使用されていないものに変更する必要があります、私は情報を見つけることができませんそうすることができます、誰かが私にそうするようにしてもらえますか?
解決
YAML設定ファイルのポートを更新できます。
http:
port: 9000
adminPort: 9001
.
http://www.dropwizard.io/0.9.2/docs/手動/ configuration.html#http 詳細については
編集
Dropwizard 0.7.x、0.8.x、0.9.xに移行した場合は、次のようになります。
server:
applicationConnectors:
- type: http
port: 9000
adminConnectors:
- type: http
port: 9001
. 他のヒント
コマンドラインから、Dropwizard 0.6:
で設定できます。java -Ddw.http.port=9090 -Ddw.http.adminPort=9091 -jar yourapp.jar server yourconfig.yml
.
DropWizard 0.7を使用すると、システムのプロパティがこのように設定されています。
java -Ddw.server.applicationConnectors[0].port=9090 -Ddw.server.adminConnectors[0].port=9091 -jar yourapp.jar server yourconfig.yml
.
システムプロパティを介してポートを設定する場合は、YMLに設定する必要があります(とにかくシステムプロパティが優先されます)。少なくともドロップウィザード0.7で私に起こっています。YAMLポート設定の例:
server:
applicationConnectors:
- type: http
port: 8090
adminConnectors:
- type: http
port: 8091
.
yamlにそれらのポートを入れない場合、Dropwizardは以下のとおりです。
Exception in thread "main" java.lang.IllegalArgumentException: Unable to override server.applicationConnectors[0].port; node with index not found.
. これは私のテストアプリケーションのために私がしたものです(0.7.x、0.8.x、0.9.x):
public class TestConfiguration extends Configuration {
public TestConfiguration() {
super();
// The following is to make sure it runs with a random port. parallel tests clash otherwise
((HttpConnectorFactory) ((DefaultServerFactory) getServerFactory()).getApplicationConnectors().get(0)).setPort(0);
// this is for admin port
((HttpConnectorFactory) ((DefaultServerFactory) getServerFactory()).getAdminConnectors().get(0)).setPort(0); } }
.
0は利用可能なランダムなポートを与えます。
私はそれがきれいではないことを知っていますが、プログラム的にそれをするより良い方法を見つけることができませんでした。ポートが異なる統合テストの間で衝突しないようにする必要がありました。各テストのためにランダムにYMLファイルを作成することは、私が信じるUlierであったでしょう。
ああ、これが後で実行されているポートの取得方法です。
@Override
public void run(TestConfiguration configuration, Environment environment) throws Exception {
this.environment = environment;
// do other stuff if you need to
}
public int getPort() {
return ((AbstractNetworkConnector) environment.getApplicationContext().getServer().getConnectors()[0]).getLocalPort();
}
. 私は以前にドロップウィザードを扱ったことがない、ジャージを使って単純なサービスを作成するだけです。私はユーザーのマニュアルを見ることを決め、すぐに設定の説明を見つけました。
Dropwizard Configuration Manual
サービスを開始するときに特別なJavaシステムプロパティを渡して構成設定を上書きできます。オーバーライドはプレフィックスDWから始める必要があります。続いて、上書きされている構成値へのパスが続きます。 たとえば、HTTPポートを使用するようにオーバーライドするには、次のようにサービスを開始できます。
java -Ddw.http.port=9090 server my-config.json
.
あなたには適していますか?
実行時に使用したい場合は
-Ddw.server.applicationConnectors[0].port=9090 -Ddw.server.adminConnectors[0].port=9091
.
私はバージョン1.0.5でそれを使用しました
Dropwizard 0.6.2の場合、Portmableをプログラムで変更することで、サービスクラスの下に変更できます。
import com.yammer.dropwizard.config.Configuration;
import com.yammer.dropwizard.config.Bootstrap;
import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.config.HttpConfiguration;
import com.yammer.dropwizard.Service;
public class BlogService extends Service<Configuration> {
public static void main(String[] args) throws Exception {
new BlogService().run(new String[] {"server"});
}
@Override
public void initialize(Bootstrap<Configuration> bootsrap) {
bootsrap.setName("blog");
}
public void run(Configuration configuration, Environment environment) throws Exception {
HttpConfiguration config = new HttpConfiguration();
config.setPort(8085);
config.setAdminPort(8086);
configuration.setHttpConfiguration(config);
}
}
. ポートを設定する必要がありましたが、コマンドラインからそれらを設定できませんでした。私はこの解決策で終わりました:
public static void main(String[] args) throws Exception {
String applicationPort = "9090";
String adminPort = "9091";
System.setProperty("dw.server.applicationConnectors[0].port", applicationPort);
System.setProperty("dw.server.adminConnectors[0].port", adminPort);
new Main().run(args);
}
.
Dropwizard 1.3.0-rc7
ドロップウィザード0.8.0 -
あなたのyamlファイルは -
.server: type: simple connector: type: http port: 80
コマンドラインからポートを変更したい場合は、
java -Ddw.server.connector.port=9090 -jar yourapp.jar server yourconfig.yml
.
コマンドは、YAMLファイルにエントリがある場合にのみ機能します。DWは上書きできるデフォルト値が必要です。