创建EurekaServer工程(详细详解各个部分及版本冲突问题)

eureka

概念:SpringCloud核心组件,默认的注册中心(Consul、Nacos)

作用:注册中心,服务注册、发现(注意:既不提供服也不消费服务)

角色:Eureka服务端(注册中心)、Eureka客户端(服务提供者、服务消费者)

eureka-server搭建步骤:

1、引入pom相关起步依赖

<!--特别注意:因为您入web jar包,一直报无法注入HttpTraceFilter错误
    从springboot 2.2.0.M3开始,HttpTraceRepository将变成条件式声明,不再通过自动配置声明。
    所以parent可以改为2.1.8
    (但俺就不用2.1.8 任性!)
    (因为用的2.3.2又引发一些问题;可能会有springboot和springCloud的版本冲突问题=-=)
-->
    <!--引入springboot父工程依赖-->
    <!--引入依赖作用:
    可以省去version标签来获得一些合理的默认配置
    -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>

    <!--因为用eureka,是springCloud的,所以引入springCloud版本锁定-->
  <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <!--spring cloud 版本-->
        <spring-cloud.version>Hoxton.SR10</spring-cloud.version>
    </properties>
    <!--引入Spring Cloud 依赖-->
    <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>

<dependencies>
    <!--引入eureka-server jar包 这是eureka服务端 服务注册中心-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>

    <!--因为server要对eureka客户端服务,所以需要启动类-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2、配置application.yml

spring:
  application:
    name: eureka-server

server:
  #ereuka服务的端口,这里端口号必须和下面defaultZone端口号一致!!!
  port: 8761
#eureka相关配置
eureka:
  client:
    register-with-eureka: false   #false 表示不向注册中心中注册自己
    fetch-registry: false   #false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
       #表示搭建eureka服务端集群,地址以','隔开,并且要把自我注册和发现全部设为true{因为你有小伙伴了,要相互知道,并且你也可以去找其他服务端你没有的服务,一样可以消费}
#      defaultZone: http://localhost:8761/eureka,http://localhost:8762/eureka
      #eureka服务端单机版{不需要自我注册和发现}(下面是服务暴露的地址)
      defaultZone: http://localhost:8761/eureka
  server:
    # 服务失效剔除时间间隔,默认60秒
    eviction-interval-timer-in-ms: 60000
    # 关闭自我保护模式(默认是打开的)
    enable-self-preservation: false

3、启动类配置

import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.trace.http.HttpTraceRepository;
import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.annotation.Bean;

@SpringBootApplication //启动类注解
@EnableEurekaServer //表示此类为eureka的服务端
public class EurekaServerApplication {
    //因为 springboot 2.2.0.M3开始不再自动配置HttpTraceRepository,所以这里手动注入进IOC
    @ConditionalOnMissingBean
    @Bean
    public HttpTraceRepository httpTraceRepository() {
        return new InMemoryHttpTraceRepository();
    }
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class,args);
    }
}

配置服务端常见错误

1.@org.springframework.beans.factory.annotation.Qualifier(value=“httpTraceFilter”)

No qualifying bean of type ‘javax.servlet.Filter’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value=“httpTraceFilter”)}

原因: 从springboot 2.2.0.M3开始,HttpTraceRepository将变成条件式声明,不再通过自动配置声明,这里我们就得需要手动配置HttpTraceRepository


解决方法: 把此对象注入ioc

    @ConditionalOnMissingBean
    @Bean
    public HttpTraceRepository httpTraceRepository() {
        return new InMemoryHttpTraceRepository();
    }

---------------------------------------------------------------------------------------------------

2. Correct the classpath of your application so that it contains a single, compatible version of org.springframework.boot.actuate.health.CompositeHealthIndicator

***************************
APPLICATION FAILED TO START
**************************

Description:

An attempt was made to call a method that does not exist. The attempt was made from the following location:

org.springframework.cloud.client.discovery.health.DiscoveryCompositeHealthIndicator.(DiscoveryCompositeHealthIndicator.java:42)

The following method did not exist:

org.springframework.boot.actuate.health.CompositeHealthIndicator.(Lorg/springframework/boot/actuate/health/HealthAggregator;)V

The method’s class, org.springframework.boot.actuate.health.CompositeHealthIndicator, is available from the following locations:

jar:file:/F:/Develop/MAVEN/resp/org/springframework/boot/spring-boot-actuator/2.3.2.RELEASE/spring-boot-actuator-2.3.2.RELEASE.jar!/org/springframework/boot/actuate/health/CompositeHealthIndicator.class

The class hierarchy was loaded from the following locations:

org.springframework.boot.actuate.health.CompositeHealthIndicator: file:/F:/Develop/MAVEN/resp/org/springframework/boot/spring-boot-actuator/2.3.2.RELEASE/spring-boot-actuator-2.3.2.RELEASE.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.boot.actuate.health.CompositeHealthIndicator

Disconnected from the target VM, address: ‘127.0.0.1:3156’, transport: ‘socket’

Process finished with exit code 1

原因: springcloud和springboot的版本对应关系,springcloud使用英文单词作为版本,springboot是用数字作为版本


解决方法: 需要查看springcloud和springboot的版本对应关系,springcloud使用英文单词作为版本,springboot是用数字作为版本。
链接: https://spring.io/projects/spring-cloud/.

经过查询官方文档springCloud版本是Hoxton.SR10
在这里插入图片描述
在这里插入图片描述

---------------------------------------------------------------------------------------------------

3、eureka客户端只要去注册服务端,Eureka 注册中心就一直报Connect to localhost:8761 time out 的问题(但是还能正常使用)

com.sun.jersey.api.client.ClientHandlerException: org.apache.http.conn.ConnectTimeoutException: Connect to localhost:8761 timed out

原因: application.yml配置中 server.port和defaultZone的端口号没有保持一致

解决方法: server.port和defaultZone的端口号保持一致就可以了






\

创建eureka-client链接: https://blog.csdn.net/JAVA_MHH/article/details/114233926








\

下载eureka-server服务端源代码地址:https://download.csdn.net/download/JAVA_MHH/15907923

可获得此源代码和最新更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟浩浩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值