设为首页 加入收藏

TOP

Spring Cloud Demo(一)
2023-08-06 07:49:57 】 浏览:147
Tags:Spring Cloud Demo

Spring Cloud Demo

本文介绍Spring Cloud 常用的组件的demo代码。gitee代码:https://gitee.com/Aes_yt/spring-cloud-demo

包括Spring Cloud Eureka,Spring Cloud Feign,Spring Cloud Hystrix,Spring Cloud Ribbon,Spring Cloud Zuul,Spring Cloud Config,Spring Cloud Sleuth。

Spring Cloud Eureka

Server

  1. pom引入:

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    
  2. 代码:

    SpringBootApplication 启动类加入 @EnableEurekaServer注解。

  3. 配置:

    server:
      port: 8761
    
    eureka:
      instance:
        hostname: localhost
      client:
        register-with-eureka: false
        fetch-registry: false
        service-url:
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      server:
        wait-time-in-ms-when-sync-empty: 0
        enable-self-preservation: false
    

Client

  1. pom引入:

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    
  2. 代码:

    SpringBootApplication 启动类加入 @EnableDiscoveryClient注解。

  3. 配置:

    server:
      port: 8081
    
    spring:
      application:
        name: demo-client1
    
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    

TIPS. 非java语言也可使用Eureka提供的REST API接口接入Server。 wiki

常见问题

  1. 为什么服务上线了,Eureka Client 不能及时获取到。

    Eureka Server 的 REST API 有response cache,需要等缓存过期后才能更新数据。

  2. 为什么服务下线了,Eureka Server 接口返回的信息还会存在。

    应用实例异常挂掉,没有在挂掉之前告知Server 要下线。这个就需要依赖Eureka Server的EvictionTask 去剔除。

  3. 其他:

    springcloud对springboot的版本是有要求的,如果不一致,会启动不起来的。详细可以看官网的版本要求。

Spring Cloud Feign + Spring Cloud Hystrix

demo-hello

  1. 首先先创建一个 demo 的 module,接入Eureka配置(eureka-client )。设置端口8082,里面只有一个get方法,我们用postman调用 http://localhost:8082/hello/testGet?param=hello,能够正常返回。

    @RestController
    @RequestMapping("/hello")
    @Slf4j
    public class HelloController {
        @GetMapping("/testGet")
        public BaseResp<String> testGet(@RequestParam("param") String param) {
            log.info("[demo-hello] testGet:{}", param);
            return BaseResp.success(param);
        }
    }
    

    yml:

    server:
      port: 8082
    spring:
      application:
        name: demo-hello
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    

feign-client

  1. 创建一个新module。引入Eureka-client依赖,feign依赖和hystrix依赖。

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <!-- cloud 2020.0.x 之后无此依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    

    注意:hystrix 在 spring-cloud 2020.0.x 版本废弃,之后的版本推荐使用 Resilience4j 进行服务的熔断。

  2. yaml 配置

    server:
      port: 8083
    spring:
      application:
        name: feign-client
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    
    feign:
    #  hystrix熔断开关
      hystrix:
        enabled: true
    #  feign-client的超时时间
      client:
        config:
          default:
            connect-timeout: 3000
            read-timeout: 3000
            logger-level: basic
    
    #
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 1/7/7
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇EasyExcel使用 下一篇quarkus依赖注入之一:创建bean

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目