首页 > 解决方案 > Docker:客户端和服务器之间的链接

问题描述

我正在使用 Docker 运行一个简单的 Spring 微服务项目。我有两个项目:lucky-word-client(端口 8080)和 lucky-word-server(端口 8001)。但我无法与服务器通信客户端。事实上,如果lucky-word-client 与lucky-word-server 通信,结果是单词“Evviva”,否则单词是“Default”。我的结果始终是“默认”,但我希望客户端与服务器通信时使用“Evviva”一词。

这是两类幸运词客户:

package asw.springcloud.luckyword;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LuckyWordApplication {

    public static void main(String[] args) {
        SpringApplication.run(LuckyWordApplication.class, args);
    }
}


package asw.springcloud.luckyword;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Value;

@RestController
public class LuckyWordController {

    @Value("${lucky-word}") 
    private String luckyWord;

    @RequestMapping("/lucky-word")
    public String luckyWord() {
        return "The lucky word is: " + luckyWord; 
    }
}

这是lucky-word-client的文件application.properties:

# application.properties 
# parola fortunata di default (if server is not accessible)
lucky-word=Default

这是lucky-word-client的文件bootstrap.properties:

 # bootstrap.properties 
    spring.application.name=lucky-word 
    # i profili possibili sono italian e english
    spring.profiles.active=italian 
    # oppure: export SPRING_PROFILES_ACTIVE=italian 
    spring.cloud.config.uri=http://localhost:8001

这是lucky-word-client的文件Dockerfile:

FROM frolvlad/alpine-oraclejdk8 

ADD build/libs/lucky-word-client-0.0.1-SNAPSHOT.jar lucky-word-client.jar

EXPOSE 8080

ENTRYPOINT ["/usr/bin/java", "-Xmx128m", "-Xms128m"]

CMD ["-jar", "-Dspring.profiles.active=italian", "lucky-word-client.jar"]

这是幸运词服务器的类:

package asw.springcloud.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class CommonConfigServer {

    public static void main(String[] args) {
        SpringApplication.run(CommonConfigServer.class, args);
    }
}

这是lucky-word-server的文件application.properties:

# application.yml 
---
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/..../
          searchPaths: projects/config-data
server:
  port: 8001

这是lucky-word-server的文件Dockerfile:

FROM frolvlad/alpine-oraclejdk8 

ADD build/libs/common-config-server-0.0.1-SNAPSHOT.jar common-config-server.jar

EXPOSE 8001

ENTRYPOINT ["/usr/bin/java", "-Xmx128m", "-Xms128m"]

CMD ["-jar", "common-config-server.jar"]

标签: springdockerdocker-composedockerfiledocker-swarm

解决方案


I assume that this config line from your client is expected to point to your server:

spring.cloud.config.uri=http://localhost:8001

However if both your client and your server are deployed as docker containers, localhost won't work.

For a running docker container localhost is pointing to itself, the container, and not the host on which the container runs. It is not related to Spring but purely to docker networking.

See: https://docs.docker.com/network/

Basically you would need to deploy both containers in the same network and use the container name as a host.


推荐阅读