博客
关于我
spring cloud 中 eureka、ribbon、fegin踩过的坑
阅读量:388 次
发布时间:2019-03-05

本文共 3783 字,大约阅读时间需要 12 分钟。

Eureka、Ribbon和Feign常见问题解析

一、Eureka常见问题

1. 系统状态信息修改

在Eureka注册中心的项目上进行状态信息修改时,可以按照以下步骤操作:

  • 服务器端配置

    • 修改eureka:server下的属性:
      enable-self-preservation: false  # 设置为false,关闭自我保护功能eviction-interval-timer-in-ms: 4000  # 设置服务清理时间间隔为4秒
  • 客户端配置

    • 修改eureka:client下的属性:
      lease-renewal-interval-in-seconds: 10  # 设置租期更新时间间隔为10秒lease-expiration-duration-in-seconds: 30  # 设置租期到期时间为30秒
  • 通过上述配置,Eureka服务器会更快地感知到服务租期到期,并及时清理不再响应的节点。


    2. Eureka剔除心跳失败的节点

    为了实现Eureka Server直接踢出已关停的节点,可以按照以下配置进行:

  • 服务器端配置

    • 关闭自我保护功能,并设置服务清理时间间隔:
      eureka:  server:    enable-self-preservation: false    eviction-interval-timer-in-ms: 4000
  • 客户端配置

    • 设置租期更新和到期时间:
      eureka:  lease-renewal-interval-in-seconds: 10  lease-expiration-duration-in-seconds: 30
  • 通过上述设置,Eureka会更加及时地感知服务状态变化,并相应地管理服务列表。


    3. Eureka配置instanceId显示IP

    为了让Eureka记录的实例ID包含IP地址,可以按照以下步骤配置:

  • 配置参数

    • 在Spring Boot应用中,确保版本为2.0及以上:
      spring.cloud.client.ip-address  # 注意:在Spring Cloud 2.0之前使用 ${spring.cloud.client.ipAddress}
  • Eureka配置

    • 修改eureka:instance下的属性:
      eureka:  instance:    prefer-ip-address: true  # 设置为true,优先使用IP地址    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}
  • 通过上述配置,Eureka会在实例ID中包含相应的IP地址信息。


    二、Ribbon常见问题

    1. 自定义配置中的注解冲突

    在自定义配置Ribbon时,需注意以下事项:

    • 自定义配置类

      • 必须使用@Configuration注解标注,确保配置类被正确识别。
      • 不应与主应用上下文的组件扫描范围重叠,否则自定义配置会被所有Ribbon客户端共享。
    • 示例说明

      • 自定义配置类应单独放在一个包下,而不是放在与主应用上下文同一包中。
      • 启动类应使用@SpringBootApplication注解,确保独立于自定义配置类的上下文。

    2. RestTemplate获取List问题

    在使用RestTemplate获取List数据时,需要注意以下事项:

    • 服务端返回List类型

      • 服务端应返回完整的List类型数据,确保Ribbon能够正确反序列化。
    • 客户端处理List数据

      • 在调用服务端接口时,使用List类型作为目标实体类。
    • 示例代码

      • 服务端接口:

        @GetMapping("/list-all")public List
        listAll() { List
        userList = new ArrayList<>(); // 依次创建用户对象并添加到列表中 return userList;}
      • 客户端调用:

        @GetMapping("/list-all")public List
        listAll() { return restTemplate.getForObject("http://microserver-provider-user/list-all/", List.class);}

    3. Ribbon自定义配置示例

    在实际项目中,Ribbon的自定义配置可以按照以下方式实现:

    • 自定义配置类

      @Configurationpublic class UserRibbonConfig {  @Autowired  private UserRibbon userRibbon;  @Bean(name = "userServiceRibbon")  public UserRibbon userRibbon() {    return new UserRibbon();  }}
    • 使用示例

      @Autowired@Qualifier("userServiceRibbon")private RestTemplate restTemplate;

    三、Feign常见问题

    1. FeignClient注解使用注意事项

    在使用Feign时,需注意以下事项:

    • 注解支持

      • @FeignClient所在接口中,@GetMapping等组合注解是不支持的。
      • 必须使用@RequestMapping@RequestLine注解来定义HTTP方法。
    • 示例验证

      @FeignClient(value = "http://localhost:7901", context = "movieService")public interface MovieFeignClient {  @RequestMapping(method = RequestMethod.GET, path = "/list-all")  List
      listAll();}

    2. 使用@PathVariable时注意事项

    在Feign中使用@PathVariable时,需要注意以下事项:

    • 参数别名

      • 即使参数未使用别名,也需要为@PathVariable注解指定value属性。
    • 示例代码

      @GetMapping("/test-path-variable")public void testPathVariable(@PathVariable(value = "id") Long id) {  System.out.println("id = " + id);}

    3. Feign对复杂对象的支持

    Feign暂不支持直接使用复杂对象作为参数,但可以通过URL拼接的方式实现参数传递:

    • 服务端接口

      @GetMapping("/test-pull-user")public String testPullUser(@RequestParam(value = "id", required = false) Long id,                           @RequestParam(value = "username", required = false) String username,                           @RequestParam(value = "name", required = false) String name,                           @RequestParam(value = "age", required = false) Integer age,                           @RequestParam(value = "balance", required = false) Double balance) {  System.out.println("id = " + id + ", username = " + username + ", name = " + name + ", age = " + age + ", balance = " + balance);  return "成功";}
    • 客户端调用

      @GetMapping("/test-pull-user")public String testPullUser() {  return restTemplate.getForObject("http://localhost:7901/testPullUser?id=888&username=杰克&name=jack&age=25&balance=2500", String.class);}

    通过以上配置和优化,Eureka、Ribbon和Feign的常见问题可以得到有效解决。

    转载地址:http://touzz.baihongyu.com/

    你可能感兴趣的文章
    npm ERR! ERESOLVE could not resolve报错
    查看>>
    npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
    查看>>
    npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
    查看>>
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>