spring - 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"]
解决方案
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.
推荐阅读
- linux - `entry_SYSCALL64_slow_path` 和 `entry_SYSCALL64_fast_path` 的区别
- neo4j - 密码查询 Neo4J
- python - X.func 和 X.func( ) 的区别
- json - Firestore 将架构结构导出到 json
- java - Java Kafka Stream - Avro 序列化程序错误
- c - c中的字符输入
- android - 使用 Google 的 FIT API 跟踪当天的总步数
- mysql - 即使用户拥有所有权限,如何在春季启动中解决“用户'user'@'localhost'的java.sql.sqlexception访问被拒绝”?
- server - 创建 Minecraft 服务器时,我必须使用 ISP 提供的 Suddenlink 调制解调器/路由器,还是可以使用第二个 Nighthawk 路由器?
- python - Python 3.8 二维数组拼接正在产生奇怪的结果。拼接就是让列表指向内存