我正在尝试为我的 4 个微服务实现发现优先引导程序的方式。第一个是从 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);
}
应用程序.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
引导程序.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 with full config
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 中的本地主机。我的工作配置
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
配置 server 和 Eureka 服务器都在 Docker 中运行。因此,他们可以使用自己在 Docker 中的名字相互联系,而不是localhost
。
所以尤里卡服务网址应该是:http://eurekaserver:8761/
。
您应该将以下代码添加到docker-compose.yml
中的configserver
配置中:
links:
- eurekaserver
应用程序属性文件中添加以下内容:
eureka.client.register-with-eureka=falseeureka.client.fetch-registry=false
这使您的尤里卡服务器在默认端口 8080 上启动,这些属性还可以帮助您不将 eurekaServer 注册到自身。
(EurekaServer 本身也是一个客户端。
如果要在端口 8761 上运行此服务器,请在 application.properties 文件中添加以下内容:
server.port=8761
PS-当您不使用码头工人时,它会有所帮助。
使用 docker,但我遇到了同样的错误,我在网上搜索了很多,但没有找到任何东西。
然后我只是停止服务器并更新我的 maven 依赖项一次,然后尝试启动它服务发现。成功了!!
我希望这将帮助那些不使用和 docker 并获得相同内容的人。
P.S.-还要确保在application.properties或application.yml中具有这些配置
server.port=8761
spring.application.name=eureka-discovery
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone: http://localhost:8761/eureka
在我的情况下,devtool 是罪魁祸首,请删除依赖项并进行 maven 清理和安装。我希望它能解决问题,前提是上面提到的所有属性都在属性或 yml 文件中。