参考博客: Springboot 整合 SpringCloud组件-Feign(Ribbon/Hystrix) (三)
由于SpringCloud版本不同,上述文档中feign
开启hystrix
失效!
参考博客: Spring Cloud 之五:Feign使用Hystrix
相关配置项 入口启动类 package com.mumu.springcloud_feign; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.openfeign.EnableFeignClients; @EnableEurekaClient @EnableDiscoveryClient @EnableHystrix @EnableHystrixDashboard @EnableFeignClients @SpringBootApplication public class SpringcloudFeignApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudFeignApplication.class, args); } }
yml配置文件 eureka: instance: preferIpAddress: true instance-id: ${spring.cloud.client.ip-address}:${server.port} client: serviceUrl: defaultZone: http://localhost:8080/eureka/ server: port: 8765 spring: application: name: feign feign: # hystrix的配置此处不生效!可能由于springCloud版本的原因,启用`circuitbreaker`开启熔断 # hystrix: # enabled: true circuitbreaker: enabled: true
pom配置文件 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.12</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.mumu</groupId> <artifactId>springcloud_feign</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springcloud_feign</name> <description>springcloud_feign</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2021.0.7</spring-cloud.version> </properties> <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.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
代码相关类 Feign - Service方法接口 package com.mumu.springcloud_feign.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; //@FeignClient(value = "client-01",fallback = SchedualServiceHiHystric.class) @FeignClient(value = "client-01",fallbackFactory = SchedualServiceHiHystricFacotry.class) @Component public interface SchedualServiceHi { @RequestMapping(value = "/haveatry",method = RequestMethod.GET) String sayHiFromClientOne(@RequestParam(value = "name") String name); }
Feign - Hystrix @FeignClient(fallback=…) package com.mumu.springcloud_feign.service; import com.mumu.springcloud_feign.service.SchedualServiceHi; import org.springframework.stereotype.Component; @Component public class SchedualServiceHiHystric implements SchedualServiceHi { @Override public String sayHiFromClientOne(String name) { return "Fallback | sorry! 网络异常,服务暂时无法访问。 请求的name为:"+name; } }
@FeignClient(fallbackFactory=…) 支持打印出异常日志(推荐)
package com.mumu.springcloud_feign.service; import org.springframework.cloud.openfeign.FallbackFactory; import org.springframework.stereotype.Component; @Component public class SchedualServiceHiHystricFacotry implements FallbackFactory<SchedualServiceHi> { @Override public SchedualServiceHi create(Throwable cause) { return new SchedualServiceHi() { @Override public String sayHiFromClientOne(String name) { System.out.println("异常日志error:"+cause); return "FallbackFactory | sorry! 网络异常,服务暂时无法访问。 请求的name为:"+name; } }; } }
问题记录 错误日志: Could not find artifact org.springframework.cloud:spring-cloud-starter-netflix-hystrix:pom:unknown in central (https://repo.maven.apache.org/maven2)
参考博客解决方式: Could not find artifact org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:pom
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> <version>2.2.1.RELEASE</version> </dependency>