Eureka客戶端-服務消費者(Feign)
阿新 • • 發佈:2019-01-14
1、pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.0.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>Finchley.SR1</spring-cloud.version> </properties> <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> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <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-actuator</artifactId> <!--客戶端只有當我們有了這個依賴之後,才能有那些狀態頁面的檢視,否則會報ErrorPage--> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> </dependencies>
注意:spring-boot-starter-web依賴必須要有,如果沒有的話,會報錯
feign的依賴在Finchley版本已經需要使用openfeign,不再是之前的依賴。
2、application.yml
spring: application: name: eureka-client-consumer-feign server: port: 8603 eureka: instance: prefer-ip-address: true client: service-url: defaultZone: http://localhost:8761/eureka/
3、程式碼測試
(1)服務提供者部分程式碼
@Value("${server.port}") private String port; @RequestMapping("/getUUid/{i}") public String getUUid(@PathVariable("i") Integer i){ UUID uuid = UUID.randomUUID(); String s = i + "==="+uuid.toString(); return s; } @RequestMapping("/getParam") public String getParam(@RequestBody String param){ return param; } @RequestMapping("/getServerPort") public String getServerPort(){ return port; }
(2)feign客戶端程式碼
a、啟動類程式碼
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
public class EurekaClientCustomerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientCustomerFeignApplication.class, args);
}
}
b、feign介面程式碼
@FeignClient("eureka-client-provider")
public interface StoreClient {
@RequestMapping(method = RequestMethod.POST,value = "/getUUid/{i}")
public String getUUid(@PathVariable("i") Integer i);
@RequestMapping("/getParam")
public String getParam(String param);
}
注:在@FeignClient下不能使用@GetMappingz這些合成註解,@PathVariable中需要設定值不能省略
c、feign提供服務的程式碼
@Autowired
StoreClient storeClient;
@RequestMapping("/getResult")
public String getResult(){
String uUid = storeClient.getUUid(3);
return uUid;
}
@RequestMapping("getParamTest")
public String getParamTest(){
String ceshi = storeClient.getParam("你好");
return ceshi;
}
注:
訪問返回結果是:
也就是說,我們要攜帶引數的話,位址列是可以的。如果要在方法引數中攜帶引數,那麼我們就需要使用@RequestBody的註解。
4、和ribbon一樣,feign同樣能夠覆蓋預設的配置
修改的程式碼:
@FeignClient(name="eureka-client-provider",configuration = FootConfigration.class)
public interface StoreClient {
@RequestLine("GET /getUUid/{i}")
public String getUUid(@PathVariable("i") Integer i);
@RequestLine("POST /getParam")
public String getParam(String param);
}
注:請求部分的註解必須要使用feign自己的註解,不能再使用springmvc的註解,否則就會報錯
修改配置的程式碼:
@Configuration
public class FootConfigration {
@Bean
public Contract feignContract() {
return new feign.Contract.Default();
}
}
注:這個配置類不能放在啟動類的掃描範圍之下,否則將會全域性起作用。
在中文文件中有針對於哪些配置可以進行更改進行了羅列: