当前位置: 首页 > 知识库问答 >
问题:

Spring微服务eureka客户端未在eureka服务器上注册

皇甫高阳
2023-03-14

我对docker,Spring框架的一切都很陌生…它们成功地在localhost环境中运行,现在我想把它们推送到docker中,但是总是给我错误,我在那里卡住了大约一周。有人能帮我弄清楚吗!!!

现在我有 eureka 服务器和云配置服务器服务,这是我的代码:

尤里卡服务器应用程序.yml

server:
  port: 8761
spring:
  application:
    name: service-registration
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
management:
  security:
    enabled: false

和 Dockerfile:

FROM openjdk:11
COPY target/service-registration-0.0.1-SNAPSHOT.jar service-registration.jar
EXPOSE 8761
CMD ["java", "-jar", "service-registration.jar"]

现在我有云配置服务器应用程序.yml:在这个文件中,我试图在github上备份它。从一开始,我就尝试将主机名更改为本地主机或任何其他主机名,如服务注册、尤里卡服务器等......但不是工作,并且“服务网址”或“服务网址”....

server:
  port: 9291

spring:
  application:
    name: cloud-config-server
  profiles:
    active: production
#  cloud:
#    config:
#      server:
#        git:
#          uri: https://github.com/sshaunn/config-server
#          clone-on-start: true

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://service-registration:8761/eureka/
  instance:
#    prefer-ip-address: true
#    ip-address: http://eureka-server:8761/eureka/å
    hostname: service-registration

Dockerfile:

FROM openjdk:11
COPY target/cloud-config-server-0.0.1-SNAPSHOT.jar cloud-config-server.jar
EXPOSE 9291
ENTRYPOINT ["java", "-jar", "cloud-config-server.jar"]

和 docker-compose.yml 文件:

version: '3.7'
services:
  service-registration:
    image: service-registration
    networks:
      - eureka-server
    ports:
      - "8761:8761"
    container_name: service-registration
    hostname: service-registration
  cloud-config-server:
    build: cloud-config-server
    networks:
      - eureka-server
    ports:
      - "9291:9291"
    depends_on:
      - service-registration
    container_name: cloud-config-server
    hostname: service-registration
    environment:
      EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://service-registration:8761/eureka

networks:
  eureka-server:
    name: eureka-server
    driver: bridge
#  route:
#    image: route
#    ports:
#      - "9191:9191"
#    container_name: api-gateway

#    environment:
#      EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://service-registration:8761/eureka

2022-05-29 07:22:41.713 WARN 1-[freshxecutor-0]c . n . d . s . t . d . retryableurekahttpclient:请求执行失败,出现消息:对“http://localhost:8761/eureka/apps/”的GET请求出现I/O错误:连接到localhost:8761[localhost/127 . 0 . 0 . 1]失败:连接被拒绝(连接被拒绝);2022-05-29 07:06:11.565 WARN 1-[tbeat executor-0]c . n . d . s . t . d . retryableurekahttpclient:请求执行失败,出现消息:对“http://localhost:8761/eureka/apps/CONFIG-SERVER/service-registration:CONFIG-SERVER:9291”的PUT请求出现I/O错误:连接到localhost:8761[localhost/127 . 0 . 0 . 1]

共有2个答案

祁正阳
2023-03-14

您应该使用您的主机ip地址在您的本地机器上运行。对于docker,它不应该是< code>localhost

请尝试在您的< code > application . properties 文件中使用以下配置。

eureka.client.service-url.defaultZone=http://host.docker.internal:8761/eureka/

濮阳征
2023-03-14

好吧,我不知道为什么,但我建立了一个docker-compose.yml文件,它是这样的:注意,我在docker-compose.yml文件中设置了关于“服务URL”default zone的环境。

version: '3.7'
services:
  service-registration:
    container_name: service-registration
    build:
      context: ./service-registration
      dockerfile: Dockerfile
    ports:
      - "8761:8761"
  cloud-config-server:
    container_name: cloud-config-server
    build:
      context: ./cloud-config-server
      dockerfile: Dockerfile
    environment:
      - eureka.client.service-url.defaultZone=http://service-registration:8761/eureka
    depends_on:
      - service-registration
    ports:
      - "9191:9191"
  route:
    container_name: route
    build:
      context: ./route
      dockerfile: Dockerfile
    environment:
      - eureka.client.service-url.defaultZone=http://service-registration:8761/eureka
    depends_on:
      - service-registration
      - employee
      - income
    ports:
      - "9291:9291"
  employee:
    container_name: employee
    build:
      context: ./employee
      dockerfile: Dockerfile
    environment:
      - eureka.client.service-url.defaultZone=http://service-registration:8761/eureka
    depends_on:
      - service-registration
    ports:
      - "9001:9001"
  income:
    container_name: income
    build:
      context: ./income
      dockerfile: Dockerfile
    environment:
      - eureka.client.service-url.defaultZone=http://service-registration:8761/eureka
    depends_on:
      - service-registration
    ports:
      - "9002:9002"
 类似资料: