The role of these two components is to solve the calls between multiple micro-services, how to solve a call, a call to resolve an error occurred while how to solve
Called before the service can be used more directly to request http request or rest every micro service request
Feign when dependent on appname Ribbon can turn ip: port, Fegin is resealed on the Ribbon, such service calls between the micro simpler
Creating a Fegin and Hystrix project
pom.xml file
<Dependencies> <-! introduced Web function -> <dependency> <the groupId> org.springframework.boot </ the groupId> <the artifactId> Starter-Spring-Boot-Web </ the artifactId> </ dependency> <! - Eureka client, when the client registered to Eureka Server will provide a range of metadata information, such as: host, port, and other health checks url Eureka Server accepts heartbeat information for each client sent if a timeout configuration a heartbeat is not received within the time information, examples will be removed from the registration list -> <dependency> <the groupId> org.springframework.cloud </ the groupId> <the artifactId> Starter-Spring-Cloud-Netflix-Eureka-Client < / the artifactId> </ dependency> <-! introduced Feign,Can be called micro-services declaratively ->Can be called micro-services declaratively -> <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 driver, note that this requires the corresponding version of MySQL -> <dependency> <the groupId> MySQL </ the groupId> <the artifactId> MySQL-Connector-Java </ the artifactId> <Version> 5.1.47 </ Version> <scope> Runtime </ scope> </ dependency> <-! Universal Module -> <dependency> <the groupId> COM .imooc.homepage </ the groupId> <the artifactId> Common-Homepage </ the artifactId> <Version> the SNAPSHOT-1.0 </ Version> </ dependency> </ Dependencies> <-! Maven SpringBoot plug, it is possible in a manner Maven to support applications SpringBoot may be SpringBoot applications packaged as an executable jar or war files, and then run the application in the usual way SpringBoot -> <Build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Startup class configuration
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); } }
Configuration application.yml profile
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
Directory services structure
Feign defined interfaces
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; / ** * access other services through micro Feign * users access to micro-service courses micro services * / @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); }
Written fusing strategy and methods of the class to name Fegin interface definition to be the same, so to achieve its interface
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(); } }