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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>service-feign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-feign</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!--讓feign支援斷路器添加hystrix依賴-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!--監控hystrix需要添加actuator依賴-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</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>
application.yml配置:
server:
port: 8300
spring:
application:
name: service-feign
management:
security:
enabled: false
eureka:
client:
serviceUrl:
defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/
feign:
hystrix:
enabled: true #開啟feign的hystrix支援,這個屬性idea智能補全提示不出來...
注意這裡如果要讓feign用戶端支援fallback的話,必須開啟feign.hystrix.enabled:true,不然不會執行熔斷保護邏輯
啟動類:
package com.example.servicefeign;
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.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker //要能監控hystrix必須聲明該注解,并添加spring-boot-starter-actuator依賴
public class ServiceFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceFeignApplication.class, args);
}
}
service 類:
package com.example.servicefeign.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@Service //強迫症範了,加個注解讓controller中的自動注入不會出現紅線提示無法自動注入,不加也是可以運作的
@FeignClient(name = "EUREKA-CLIENT-PRODUCER", fallback = MyServiceFallback.class)
public interface MyService {
@RequestMapping(value = "/hi", method = RequestMethod.GET)
String test(@RequestParam("username") String username, @RequestParam("password") String password);
@RequestMapping(value = "/hi2/{username}", method = RequestMethod.GET)
String test2(@PathVariable("username") String username);
}
@FeignClient注解表名該接口是一個聲明式的feign用戶端,name是前面注冊的服務提供者的服務名(serviceId),在eureka注冊中心可以看到,GET請求如果有多個參數的話,必須用@RequestParam分别聲明,不能用一個對象來封裝所有參數後将該對象作為請求參數,這樣雖然你聲明的是GET請求,但是最終feign會以post請求發送參數,因為這個參數會放到requestBody中,隻要request body中有内容,feign就會以post方式請求了。是以如果你的服務提供發限定了隻能以GET方式請求的話,就會報錯了。
fallback開啟熔斷保護,熔斷實作由MyServiceFallback類提供,該類實作了MyService接口,同名方法實作即為對應的方法調用的fallback方法:
package com.example.servicefeign.service;
import org.springframework.stereotype.Component;
//fallback實作必須聲明為spring 的bean,不然會提示fallback實作沒有被執行個體化的錯誤
@Component
public class MyServiceFallback implements MyService {
@Override
public String test(String username, String password) {
return "feign fallback:" + username + "," + password;
}
@Override
public String test2(String username) {
return "feign fallback:" + username;
}
}
MyServiceFallback類必須是一個spring bean,這裡用@Component注解了該類,元件掃描會自動将該類執行個體化為一個spring bean,不然觸發fallback時會報錯提示fallback類沒有執行個體化.
最後controller類:
package com.example.servicefeign.controller;
import com.example.servicefeign.model.UserInfo;
import com.example.servicefeign.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
@Autowired
private MyService myService;
@RequestMapping("/test")
public String test(UserInfo userInfo) {
return myService.test(userInfo.getUsername(), userInfo.getPassword());
}
@RequestMapping("/test2/{username}")
public String test2(@PathVariable("username") String username) {
return myService.test2(username);
}
}