SpringCloud -- Feign & Hystrix 介绍
这两个组件的作用是解决多个微服务之间的调用,一个是解决如何调用,一个解决调用时出现了错误怎么解决
在之前多个服务进行调用可以直接使用http请求或者rest 请求去请求每一个微服务
Feign 时依赖于Ribbon可以使用appname 转 ip:port,Fegin是对 Ribbon的再次封装,使得微服务之间的调用更简单
创建一个 有Fegin和Hystrix的项目
pom.xml 文件
<dependencies> <!-- 引入 Web 功能 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Eureka 客户端, 客户端向 Eureka Server 注册的时候会提供一系列的元数据信息, 例如: 主机, 端口, 健康检查url等 Eureka Server 接受每个客户端发送的心跳信息, 如果在某个配置的超时时间内未接收到心跳信息, 实例会被从注册列表中移除 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!-- 引入 Feign, 可以以声明的方式调用微服务 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!-- 引入服务容错 Hystrix 的依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <!-- Java Persistence API, ORM 规范 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- MySQL 驱动, 注意, 这个需要与 MySQL 版本对应 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> <scope>runtime</scope> </dependency> <!-- 通用模块 --> <dependency> <groupId>com.imooc.homepage</groupId> <artifactId>homepage-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <!-- SpringBoot的Maven插件, 能够以Maven的方式为应用提供SpringBoot的支持,可以将 SpringBoot应用打包为可执行的jar或war文件, 然后以通常的方式运行SpringBoot应用 --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
启动类的配置
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.data.jpa.repository.config.EnableJpaAuditing; @EnableJpaAuditing @EnableFeignClients @EnableCircuitBreaker @EnableEurekaClient @SpringBootApplication public class HomepageUserApplication { public static void main(String[] args) { SpringApplication.run(HomepageUserApplication.class,args); } }
配置文件的配置application.yml
server: port: 7000 servlet: context-path: /homepage-user spring: main: allow-bean-definition-overriding: true application: name: eureka-client-homepage-user jpa: show-sql: true hibernate: ddl-auto: none properties: hibernate.format_sql: true open-in-view: false datasource: url: jdbc:mysql://127.0.0.1:3306/imooc_homepage_sc?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useSSL=false username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver tomcat: max-active: 4 min-idle: 2 initial-size: 2 eureka: client: service-url: defaultZone: http://server1:8000/eureka/ feign: hystrix: enabled: true
为服务的目录结构
Feign接口的定义
import com.imooc.homepage.CourseInfo; import com.imooc.homepage.CourseInfosRequest; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import java.util.List; /** * 通过 feign 访问 其他的微服务 * 用户微服务访问 课程微服务 */ @FeignClient(value = "eureka-client-homepage-course", fallback = CourseClientHystrix.class) public interface CourseClient { @RequestMapping(value = "/homepage-course/get/course", method = RequestMethod.GET) CourseInfo getCourseInfo(Long id); @RequestMapping(value = "/homepage-course/get/courses", method = RequestMethod.POST) List<CourseInfo> getCourseInfos(@RequestBody CourseInfosRequest request); }
熔断策略类的编写要和Fegin接口定义的方法名称要一样,所以去实现其接口
import com.imooc.homepage.CourseInfo; import com.imooc.homepage.CourseInfosRequest; import org.springframework.stereotype.Component; import java.util.Collections; import java.util.List; /** * 熔断降级策略 */ @Component public class CourseClientHystrix implements CourseClient{ @Override public CourseInfo getCourseInfo(Long id) { return CourseInfo.invalid(); } @Override public List<CourseInfo> getCourseInfos(CourseInfosRequest request) { return Collections.emptyList(); } }