Eureka服务-提供-消费

    技术2025-07-19  6

    父类模块

    在父类pom文件中指定版本依赖<dependencyManagement> 

    <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

    EurekaServier模块

    在本模块中添加eureka依赖,jar

    <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>

     

    编写注册中心

    ## 启动顺序: #1 spring.application.name=eureka-server server.port=20000 eureka.instance.hostname=localhost eureka.client.register-with-eureka=false //拉取出册表 eureka.client.fetch-registry=false

     

    启动类 

    @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { new SpringApplicationBuilder(EurekaServerApplication.class) .web(WebApplicationType.SERVLET) .run(args); } }

    build 

    <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.imooc.EurekaServerApplication</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>

     

    EurekaUI

    Renews threshold:每分钟最少的续约数量

    Renews:上一分钟的续约数

     

    EurekaClient模块(提供者)

    在本模块中添加eureka依赖,jar

    <dependencies> <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-web</artifactId> </dependency> </dependencies>

    启动类 

    @SpringBootApplication @EnableDiscoveryClient public class EurekaClientApplication { public static void main(String[] args) { new SpringApplicationBuilder(EurekaClientApplication.class) .web(WebApplicationType.SERVLET) .run(args); } }

    编写注册 

    spring.application.name=eureka-client server.port=30000 eureka.client.serviceUrl.defaultZone=http://localhost:20000/eureka/

    EurekaConsumer模块(消费者)

    在本模块中添加eureka依赖,jar

    <dependencies> <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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>

    配置文件 

    spring.application.name=eureka-consumer server.port=40000 eureka.client.serviceUrl.defaultZone=http://localhost:20000/eureka/

     

    开启心跳健康检查

    注册端

    #每隔5秒钟中,向服务中心发送一条续约指令 eureka.instance.lease-renewal-interval-in-seconds= 5 #如果30秒内,依然没有收到续约请求,判定服务过期(上西天) eureka.instance.lease-expiration-duration-in-seconds= 30

    服务端 

    #强制关闭服务自保 eureka.server.enable-self-preservation=false #每隔多久触发服务剔除 1秒 eureka.server.eviction-interval-timer-in-ms=10000

     

     

     

     

     

     

     

     

     

    Processed: 0.011, SQL: 9