1.pom.xml 2.UserConsumerDemoApplication.java 3.UserClient.java 4.UserFController.java ...
1.pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.0.2.RELEASE</version> </dependency>
2.UserConsumerDemoApplication.java
@EnableFeignClients
3.UserClient.java
package cn.itcast.user.client; import cn.itcast.user.pojo.User; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @FeignClient("user-service") public interface UserClient { @GetMapping("{id}") User getUserQueryInfo(@PathVariable("id") Long id); }
4.UserFController.java
package cn.itcast.user.controller; import cn.itcast.user.client.UserClient; import cn.itcast.user.pojo.User; import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("consumerF") @DefaultProperties(defaultFallback = "queryUserByIdFallback") public class UserFController { @Autowired private UserClient userClient; @GetMapping("{id}") public User queryUserById(@PathVariable("id") Long id){ return userClient.getUserQueryInfo(id); } public String queryUserByIdFallback(){ return "用戶信息查詢出現異常!"; } }