【问题标题】:Http protocol not supported in GraalVM Spring Native appGraalVM Spring Native 应用程序不支持 Http 协议
【发布时间】:2021-03-29 15:45:20
【问题描述】:

我有一个 spring 原生应用程序,它可以对另一个服务进行 http 调用。 我使用本机映像 maven 插件(mvn -Pnative-image 包)编译了该服务。当我的应用程序尝试进​​行 http 调用时,我看到了这条消息,“支持 URL 协议 http,但默认情况下不启用。必须通过将 --enable-url-protocols=http 选项添加到 native-image 来启用它命令。”

org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://localhost:8081/v1/test": Accessing an URL protocol that was not enabled. The URL protocol http is supported but not enabled by default. It must be enabled by adding the --enable-url-protocols=http option to the native-image command.; nested exception is java.net.MalformedURsampleception: Accessing an URL protocol that was not enabled. The URL protocol http is supported but not enabled by default. It must be enabled by adding the --enable-url-protocols=http option to the native-image command.
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:785) ~[com.app.demo.sample.SampleApplication:5.3.4]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:711) ~[com.app.demo.sample.SampleApplication:5.3.4]
    at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:361) ~[com.app.demo.sample.SampleApplication:5.3.4]
  ----
    
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) ~[na:na]
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:305) ~[na:na]
    at java.util.concurrent.ScheduledThreadPoosampleecutor$ScheduledFutureTask.run(ScheduledThreadPoosampleecutor.java:305) ~[na:na]
    at java.util.concurrent.ThreadPoosampleecutor.runWorker(ThreadPoosampleecutor.java:1128) ~[na:na]
    at java.util.concurrent.ThreadPoosampleecutor$Worker.run(ThreadPoosampleecutor.java:628) ~[na:na]
    at java.lang.Thread.run(Thread.java:834) ~[na:na]
    at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:519) ~[na:na]
    at com.oracle.svm.core.posix.thread.PosixJavaThreads.pthreadStartRoutine(PosixJavaThreads.java:192) ~[na:na]
Caused by: java.net.MalformedURsampleception: Accessing an URL protocol that was not enabled. The URL protocol http is supported but not enabled by default. It must be enabled by adding the --enable-url-protocols=http option to the native-image command.
    at com.oracle.svm.core.jdk.JavaNetSubstitutions.unsupported(JavaNetSubstitutions.java:215) ~[na:na]
    at com.oracle.svm.core.jdk.JavaNetSubstitutions.getURLStreamHandler(JavaNetSubstitutions.java:172) ~[na:na]
    at java.net.URL.getURLStreamHandler(URL.java:71) ~[na:na]
    at java.net.URL.<init>(URL.java:651) ~[na:na]
    at java.net.URL.fromURI(URL.java:719) ~[na:na]
    at java.net.URI.toURL(URI.java:1116) ~[na:na]
    at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:145) ~[na:na]
    at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:124) ~[na:na]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:772) ~[com.app.demo.sample.SampleApplication:5.3.4]
    ... 18 common frames omitted

【问题讨论】:

    标签: spring http native graalvm


    【解决方案1】:

    上述问题的解决方案是将 build args "--enable-url-protocols=http" 添加到本机图像 maven 插件,如下所示。

    <profiles>
            <profile>
                <id>native-image</id>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.graalvm.nativeimage</groupId>
                            <artifactId>native-image-maven-plugin</artifactId>
                            <version>21.0.0</version>
                            <configuration>
                                <buildArgs>
                                    <buildArgss>--enable-url-protocols=http</buildArgss>
                                </buildArgs>
                                <!-- The native image build needs to know the entry point to your application -->
                                <mainClass>com.app.demo.sample.SampleApplication</mainClass>
                            </configuration>
                            <executions>
                                <execution>
                                    <goals>
                                        <goal>native-image</goal>
                                    </goals>
                                    <phase>package</phase>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
    

    文档参考:https://www.graalvm.org/reference-manual/native-image/NativeImageMavenPlugin/

    【讨论】:

      【解决方案2】:

      或者如果你使用 springboot maven 插件:

      <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                      <configuration>
                          <excludes>
                              <exclude>
                                  <groupId>org.projectlombok</groupId>
                                  <artifactId>lombok</artifactId>
                              </exclude>
                          </excludes>
                          <image>
                              <builder>paketobuildpacks/builder:tiny</builder>
                              <env>
                                  <BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
                                  <BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
                                      --enable-url-protocols=http
                                  </BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
                              </env>
                          </image>
                          <classifier>exec</classifier>
                      </configuration>
                  </plugin>
      

      【讨论】:

        猜你喜欢
        • 2020-08-15
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多