Api-gateway(微服务- Springoot项目)-无法运行服务,我想将此服务注册为Eureka 的客户端

q3qa4bjr  于 2023-03-23  发布在  Spring
关注(0)|答案(1)|浏览(115)

第一节第一节第一节第一节第一次
所附文档中存在错误。
我在pom.xml和yml文件中添加了netflixEureka 依赖项。

这是yml文件。

spring:
  application:
    name: GATEWAY-SERVICE
  cloud:
    gateway:
      routes:
        - id: order-service
          uri: lb://ORDER-SERVICE
          predicates:
            - Path=/order/**
        - id: payment-service
          uri: lb://PAYMENT-SERVICE
          predicates:
            - Path=/payment/**

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    hostname: localhost

server:
  port: 8989

此文件为pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>api-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>api-service</name>
    <description>api-service</description>
    <properties>
        <java.version>17</java.version>
        <spring-cloud.version>Hoxton.SR12</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>netflix-candidates</id>
            <name>Netflix Candidates</name>
            <url>https://artifactory-oss.prod.netflix.net/artifactory/maven-oss-candidates</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>
icomxhvb

icomxhvb1#

我希望我的配置可以帮助你。这是客户端的YML文件的样子:

###############################################################
# SERVER CONFIG
###############################################################
server:
  port: 8083

spring:
  application:
    name: gateway

###############################################################
# EUREKA CONFIG
###############################################################
eureka:
  client:
    registry-fetch-interval-seconds: 1
    eureka-connection-idle-timeout-seconds: 30000
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://load-balancer:8070/eureka/
  instance:
    prefer-ip-address: true
    lease-renewal-interval-in-seconds: 1

ribbon:
  ServerListRefreshInterval: 1000

另外,别忘了在微服务的主类上方添加**@EnableEurekaClient**annotation:

@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
public class GatewayApplication {
...
}

我将依赖项添加到Gradle:

dependencies {
    ...
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    ...
}

这是Eureka 服务器微服务的一个例子:

spring:
  application:
    name: load-balancer

server:
  port: 8070

  tomcat:
    max-threads: 100000

ribbon:
  eureka:
    enabled: true
  ReadTimeout: 60000
  connection-timeout: 30000
  NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList
  NFLoadBalancerRuleClassName: com.netflix.loadbalancer.WeightResponseTimeRule

eureka:
  server:
    enable-self-preservation: false
    response-cache-update-interval-ms: 1000
  instance:
    prefer-ip-address: true
  client:
    register-with-eureka: false
    fetch-registry: false

别忘了在Eureka服务器应用程序的主类上方添加**@EnableEurekaServer**annotation:

@SpringBootApplication
@EnableEurekaServer
public class LoadBalancerApplication {
...
}

最后,我将依赖项添加到Gradle:

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    ...
}

相关问题