Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在docker容器上的RestTemplate url中使用spring.application.name?_Java_Spring Boot_Docker_Microservices_Netflix Eureka - Fatal编程技术网

Java 如何在docker容器上的RestTemplate url中使用spring.application.name?

Java 如何在docker容器上的RestTemplate url中使用spring.application.name?,java,spring-boot,docker,microservices,netflix-eureka,Java,Spring Boot,Docker,Microservices,Netflix Eureka,我有两个spring boot项目-album service端口8081和song service端口8082 song service有一个实体类song,其字段为albumId,该字段恰好是Album service中实体类Album的主键 我在我的服务类SongService中有一行,它使用RestTemplate返回一个Album对象: Album album = restTemplate.getForObject("http://localhost:8082/find/&q

我有两个spring boot项目-
album service
端口8081和
song service
端口8082

song service
有一个实体类
song
,其字段为
albumId
,该字段恰好是
Album service
中实体类
Album
的主键

我在我的服务类
SongService
中有一行,它使用RestTemplate返回一个
Album
对象:

Album album = restTemplate.getForObject("http://localhost:8082/find/"+song.getAlbumId(), Album.class);
现在,我想在RestTemplate上使用应用程序名,而不是使用
localhost:xxxx

因此,在专辑服务的
application.yml
中,我写了这句话:

spring:
 application:
  name: ALBUM-SERVICE
并将RestTemplate行替换为:

Album album = restTemplate.getForObject("http://ALBUM-SERVICE/find/"+song.getAlbumId(), Album.class);
当两个项目都在运行时,这似乎也可以正常工作(与Eureka服务器配对,并将
唱片服务
歌曲服务
作为Eureka客户端)

但是,当这些项目位于单独的docker容器上时,就会出现问题。当我运行使用上述包含RestTemplate的行的API时,我得到一个异常:

2021-05-03 11:14:51.588 ERROR 1 --- [nio-8082-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://ALBUM-SERVICE/albums/find/1": Connection refused; nested exception is java.net.ConnectException: Connection refused] with root cause
以下是所有内容的配置文件:

  • 相册服务的
    应用程序.yml
  • 歌曲服务的
    应用程序.yml
  • 服务注册表的
    应用程序.yml
    (Eureka服务器):
  • docker compose.yml

  • 关于如何在docker容器上复制本地行为的任何提示?

    我可以想到两个选项:

    使用容器名称作为主机 由于所有容器都在同一个docker网络中,因此在任何给定容器中使用容器名称将解析为正确的容器(这是很多容器lol)。在您的情况下,如果要解析为songService,主机名将是
    song service container

    使用host.docker.internal
    如果所有容器都在同一台计算机上运行,则可以尝试将主机设置为
    host.docker.internal
    。它将解析到您的本地计算机,并且由于容器的端口被转发到您的计算机,这也应该起作用。

    至于使用容器名称作为主机,您仍然需要提供端口号,正如我在service-url.defaultZone中所做的那样。有没有办法不使用端口号?
    server:
      port: 8081
    
    spring:
      data:
        mongodb:
          host: mongodb-container
          port: 27017
          database: music
      application:
        name: ALBUM-SERVICE
    
    eureka:
      client:
        fetch-registry: true
        register-with-eureka: true
        service-url:
          defaultZone: http://service-registry-container:8761/eureka/
    
      instance:
        hostname: service-registry-container
    
    server:
      port: 8082
    
    spring:
      data:
        mongodb:
          host: mongodb-container
          port: 27017
          database: music
      application:
        name: SONG-SERVICE
    
    
    eureka:
      client:
        fetch-registry: true
        register-with-eureka: true
        service-url:
          defaultZone: http://service-registry-container:8761/eureka/
    
      instance:
        hostname: service-registry-container
    
    server:
      port: 8761
    
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
    
    version: "3"
    services:
    
      service-registry-container:
        image: service-registry
        ports:
          - 8761:8761
        networks:
          - ms-music
    
      album-service-container:
        image: album-service
        ports:
          - 8081:8081
        networks:
          - ms-music
    
      song-service-container:
        image: song-service
        ports:
          - 8082:8082
        networks:
          - ms-music
    
      mongodb-container:
        image: arm64v8/mongo
        ports:
          - 27017:27017
        volumes: 
          - ~/mongo/data:/data/db
        networks:
          - ms-music
    
    networks:
      ms-music: