我正在尝试为我的4个微服务实现Discovery First Bootstrap方法。第一个是从git获取配置的配置服务器,第二个是Eureka服务器。当我运行docker-compose up时,我的配置服务器无法向eureka注册。我有:
请求执行错误。 endpoint = DefaultEndpoint {serviceUrl ='http://localhost:8761/}连接被拒绝
我的配置服务器
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
application.yml
server:
port: 8888
spring:
cloud:
config:
server:
encrypt.enabled: false
git:
uri: https://github.com/Artuwok/artbook-config-repo/
searchPaths: userservice
username: Artuwok
password: Civilization1986
timeout: 10
force-pull: true
bootstrap.yml
spring:
application:
name: configserver
cloud:
config:
fail-fast: true
discovery.enabled: true
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl.defaultZone: http://localhost:8761/eureka/
尤里卡课
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
Eureka bootstrap.yml
server:
port: 8761
eureka:
client:
registerWithEureka: false
fetchRegistry: false
具有完整配置的docker-compose.yml
version: '3'
services:
configserver:
image: configserver:1.0
ports:
- "8888:8888"
depends_on:
- eurekaserver
restart: on-failure
eurekaserver:
image: eurekasvr:1.0
ports:
- "8761:8761"
users_db:
image: mysql:5.7
container_name: users_db
restart: always
environment:
- MYSQL_DATABASE=artbook
- MYSQL_ROOT_PASSWORD=password
- MYSQL_USER=user
- MYSQL_PASSWORD=password
volumes:
- users_db:/var/lib/mysql
ports:
- "3306:3306"
depends_on:
- eurekaserver
user-service:
image: userservice:1.0
ports:
- "8080:8080"
depends_on:
- eurekaserver
- configserver
restart: on-failure
volumes:
users_db:
因此最终我能够启动服务。请特别注意要在其上发现服务的URL。 如果您正在使用docker映像,则必须使用服务名称,而不是uri和url中的localhost。我的工作配置
spring:
application:
name: configserver
cloud:
config:
fail-fast: false
discovery.enabled: false
uri: http://configserver:8888
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl.defaultZone: http://eurekaserver:8761/eureka
答案 0 :(得分:1)
您的配置服务器和Eureka服务器都在Docker中运行。因此他们可以在Docker中使用自己的名称而不是localhost
相互连接。
因此,尤里卡(Eureka)服务网址应为:http://eurekaserver:8761/
。
您应该将以下代码添加到configserver
中的docker-compose.yml
配置中:
links:
- eurekaserver
答案 1 :(得分:0)
在您的应用程序、属性文件中添加以下内容:
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
这使您的 eureka 服务器在默认端口 8080 上运行,这些属性还可以帮助您不向自身注册 eurekaServer。
(EurekaServer 本身也是一个客户端。)
如果要在端口 8761 上运行此服务器,请在 application.properties 文件中添加以下内容:
server.port=8761
PS- 当您不使用 docker 时它会有所帮助。