Feign
我們?cè)谶M(jìn)行微服務(wù)項(xiàng)目的開發(fā)的時(shí)候轿钠,經(jīng)常會(huì)遇到一個(gè)問題举农,比如A服務(wù)是一個(gè)針對(duì)用戶的服務(wù)侦啸,里面有用戶的增刪改查的接口和方法梅鹦,而現(xiàn)在我有一個(gè)針對(duì)產(chǎn)品的服務(wù)B服務(wù)中有一個(gè)查找用戶的需求,這個(gè)時(shí)候我們可以在B服務(wù)里再寫一個(gè)查找用戶的接口鲁豪,可是就為了一個(gè)接口就得從控制層到持久層都寫一遍怎么看都不值當(dāng)潘悼,最關(guān)鍵的是這個(gè)接口在別的服務(wù)里面還有律秃,這就更不應(yīng)該做了,所以springCloud提供了服務(wù)調(diào)用的方法——feign治唤。
一棒动、Feign
1.Eureka注冊(cè)中心
注冊(cè)中心較為簡單,這里就不多贅述宾添,不清楚的可以參考系列第一篇船惨。
2.服務(wù)提供者
pom.xml文件如下:
<dependencies>
<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-web</artifactId>
</dependency>
</dependencies>
2.yml文件需要修改配置
server:
port: 9992
spring:
application:
name: spring-cloud-order-service-provider
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9991/eureka //這里是server工程地址
register-with-eureka: true # ??就是服務(wù)不需要注冊(cè)?? 集群模式下可以改成true
fetch-registry: true # ??就是服務(wù)不需要從Eureka Server獲取服務(wù)信息,默認(rèn)為true,集群模式下可以改成true
3.啟動(dòng)項(xiàng)需要添加的注解和server不一樣缕陕,使用@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
4.我們這邊寫一個(gè)方法粱锐,等下使用,新建一個(gè)controller文件夾扛邑,創(chuàng)建一個(gè)controller.
@RestController
@RequestMapping("/order/data")
public class OrderStatisticServiceController {
@Value("${server.port}")
private Integer port;
@GetMapping("/getResult/{id}")
public String getResult(@PathVariable("id") Integer id) {
return "getResult = " + id;
}
}
5.啟動(dòng)當(dāng)前服務(wù)提供者
這里在server頁面就可以看到已經(jīng)注冊(cè)成功怜浅。
3.服務(wù)調(diào)用者
Feign依賴于Eureka環(huán)境,所以我們?cè)谏厦娴幕A(chǔ)上新增一個(gè)子module即可
1.pom.xml文件如下,相比上面簡單調(diào)用的第四步多一個(gè)關(guān)于Feign的依賴
<dependencies>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
2.yml配置文件
server:
port: 9994
spring:
application:
name: spring-cloud-user-feign-consumer # 應(yīng)用名稱蔬崩,應(yīng)用名稱會(huì)在Eureka中作為服務(wù)名稱
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9991/eureka
register-with-eureka: true # ??就是服務(wù)不需要注冊(cè)?? 集群模式下可以改成true
fetch-registry: true # ??就是服務(wù)不需要從Eureka Server獲取服務(wù)信息,默認(rèn)為true恶座,集群模式下可以改成true
3.啟動(dòng)項(xiàng)相比上面的調(diào)用者多個(gè)@EnableFeignClients注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
4.我們使用Feign需要?jiǎng)?chuàng)建一個(gè)接口 這里舉個(gè)簡單的例子
@FeignClient(name = "spring-cloud-order-service-provider") //這里是服務(wù)提供者在Eureka中的名稱
public interface IResultInterface {
@RequestMapping(value = "/order/getResult/{id}") //服務(wù)提供者的接口路由
String getResult(@PathVariable("id") Integer id);
}
5.我們創(chuàng)建當(dāng)前module的Controller調(diào)用的接口
@RestController
@RequestMapping("/order")
public class ResultController {
@Autowired
IResultInterface iResultInterface; //這里使用接口來完成方法的請(qǐng)求
@RequestMapping("/getResult/{id}")
public String getResult(@PathVariable("id") Integer id) {
return iResultInterface.getResult(id);
}
}
6.啟動(dòng)并訪問看看效果
這里看到我們請(qǐng)求的是9994的新的服務(wù)調(diào)用者工程 但是返回值的服務(wù)提供者的結(jié)果,說明這里Feign使用成功沥阳。
代碼已上傳至Github