当前位置:   article > 正文

SpringCloud——Eureka_spring-cloud feignclients并发调用eureka服务+jsonmappinge

spring-cloud feignclients并发调用eureka服务+jsonmappingexception: read timed

Eureka client启动后会以默认30秒的周期想Eureka server发送心跳;而Eureka Server默认在3个周期内也就是90秒没有收到client的心跳,就会把该client从注册中心移除。

快速搭建一个注册中心(Eureka服务端):

1.创建SpringBoot项目,引入Eureka-server和springCloud依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.2.0.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>
  7. <properties>
  8. <java.version>1.8</java.version>
  9. <spring-cloud.version>Hoxton.M3</spring-cloud.version>
  10. </properties>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-web</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  19. </dependency>
  20. </dependencies>
  21. <dependencyManagement>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-dependencies</artifactId>
  26. <version>${spring-cloud.version}</version>
  27. <type>pom</type>
  28. <scope>import</scope>
  29. </dependency>
  30. </dependencies>
  31. </dependencyManagement>

2.在启动类上添加@EnableEurekaServer注解

  1. @SpringBootApplication
  2. @EnableEurekaServer
  3. public class EurekaApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaApplication.class, args);
  6. }

3.application.yml配置文件

  1. server:
  2. port: 8761
  3. eureka:
  4. client:
  5. fetch-registry: false
  6. register-with-eureka: false
  7. service-url:
  8. defaultZone: http://127.0.0.1:8761/eureka

以上搭建了服务端,启动就可以了,下面创建客户端

 

同样,新建boot项目

1,引入客户端依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.2.0.RELEASE</version>
  5. <relativePath/>
  6. </parent>
  7. <properties>
  8. <java.version>1.8</java.version>
  9. <spring-cloud.version>Hoxton.M3</spring-cloud.version>
  10. </properties>
  11. <dependencies>
  12. <dependency>
  13. <groupId>org.springframework.cloud</groupId>
  14. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. </dependencies>
  21. <dependencyManagement>
  22. <dependencies>
  23. <dependency>
  24. <groupId>org.springframework.cloud</groupId>
  25. <artifactId>spring-cloud-dependencies</artifactId>
  26. <version>${spring-cloud.version}</version>
  27. <type>pom</type>
  28. <scope>import</scope>
  29. </dependency>
  30. </dependencies>
  31. </dependencyManagement>

2,在启动类上添加@EnableEurekaClient 或者@EnableDiscoveryClient注解,区别是后者是SpringClou的,不仅可以用于Eureka还可以是服务作为客户端注册到其他的注册中心中

  1. @SpringBootApplication
  2. @EnableEurekaClient
  3. public class EurekaApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(EurekaApplication.class, args);
  6. }
  7. }

3,配置文件

  1. server:
  2. port:8070
  3. eureka:
  4. client:
  5. service-url:
  6. defaultZone: http://127.0.0.1:8761/eureka/

以上创建了一个eureka客户端,启动上面的服务端,再启动客户端,就可以成功的注册到服务端

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/1016586
推荐阅读
相关标签
  

闽ICP备14008679号