【问题标题】:Change Dropwizard default ports更改 Dropwizard 默认端口
【发布时间】:2013-11-30 11:05:08
【问题描述】:

我有一个基于 Dropwizard 的 Jersey REST 服务在默认端口 8080(服务)和 8081(管理员)上运行,我需要将默认端口更改为不常用的端口,我无法找到任何信息这样做,有人可以指点我这样做吗?

【问题讨论】:

    标签: java rest jersey dropwizard


    【解决方案1】:

    在较新版本的 dropwizard(例如 2.0.25)中: 在你的资源目录中创建一个包含以下内容的属性文件 config.yml:

    server:
       applicationConnectors:
        - type: http
          port: 5020
       adminConnectors:
        - type: http
          port: 5022
    

    如果使用 Intellij IDE(2021 版),请记住将以下内容添加到运行配置程序参数:

    server src/main/resources/config.yml
    

    【讨论】:

      【解决方案2】:

      在您的 .yml 文件中进行这些更改

      server:
        registerDefaultExceptionMappers: false
        applicationConnectors:
          - type: http
            port: 5020
        adminConnectors:
          - type: http
            port: 5022
      

      【讨论】:

        【解决方案3】:

        我需要设置端口,但我无法从命令行设置它们。我最终得到了这个解决方案:

        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 完成的

        【讨论】:

          【解决方案4】:

          如果您希望在运行时更改它,请使用

          -Ddw.server.applicationConnectors[0].port=9090  -Ddw.server.adminConnectors[0].port=9091
          

          我用的是1.0.5版

          【讨论】:

            【解决方案5】:

            对于 Dropwizard 0.6.2,您可以在服务类中以编程方式更改端口,如下所示。

            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);
            }
            
            }
            

            【讨论】:

              【解决方案6】:

              您可以在 yaml 配置文件中更新端口:

              http:
                port: 9000
                adminPort: 9001
              

              更多信息请参见http://www.dropwizard.io/0.9.2/docs/manual/configuration.html#http

              编辑

              如果您已迁移到 Dropwizard 0.7.x、0.8.x、0.9.x,则可以使用以下版本:

              server:
                applicationConnectors:
                - type: http 
                  port: 9000
                adminConnectors:
                - type: http
                  port: 9001
              

              【讨论】:

              • 谢谢,但只是在我的项目的 .yml 文件中设置这些端口并不会更改默认端口,它仍然在 8080 上运行。是否有一个默认的 yaml 文件,与我拥有的不同我需要使用哪些服务来输入这些新值?
              • 奇怪——它对我有用。你是如何指定你的配置文件的?启动服务时应该在 Java 命令行上...
              • 我在eclipse的项目文件夹下直接有一个.yml文件,并使用fat jar“java -jar myservice.jar server”运行服务,我没有使用命令行指定它,有吗这里有两个配置文件?一个用于服务,一个用于配置?我正在按照入门教程进行操作。此配置文件是否与教程中提到的 Hello World 示例中的 .yml 文件相同?
              • 将你的配置添加到server之后的命令行。有关更多信息,请参阅dropwizard.codahale.com/getting-started/#running-your-service。它应该具有预期的效果。
              • 完美!非常感谢,我真的很感激,我的印象是配置文件会被自动拾取。现在我在命令行中指定了它,它就像一个魅力!
              【解决方案7】:

              这是我为我的测试应用程序(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 文件会更难看。

              哦,这就是你稍后获得运行端口的方式:

              @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();
                }
              

              【讨论】:

              • 也适用于 0.8.x
              • 是的,实际上也是 0.9.x。我会更新答案。
              【解决方案8】:

              对于 Dropwizard 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 需要一个可以覆盖的默认值。

              【讨论】:

                【解决方案9】:

                我以前从未使用过 dropwizard,只使用 jersey 创建简单的服务。我决定查看用户手册,并立即找到了设置说明。

                Dropwizard configuration manual

                您可以在启动服务时通过传递特殊的 Java 系统属性来覆盖配置设置。覆盖必须以前缀 dw. 开头,后跟要覆盖的配置值的路径。 例如,要覆盖要使用的 HTTP 端口,您可以这样启动服务:

                java -Ddw.http.port=9090 server my-config.json
                

                适合你吗?

                【讨论】:

                  【解决方案10】:

                  在命令行中,您可以在 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中设置(反正系统属性优先)。至少这在 Dropwizard 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.
                  

                  【讨论】:

                    猜你喜欢
                    • 2011-10-31
                    • 1970-01-01
                    • 1970-01-01
                    • 2014-08-29
                    • 2014-08-27
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2015-05-24
                    相关资源
                    最近更新 更多