SpringBoot + openFeign實現(xiàn)遠程接口調(diào)用的過程
SpringBoot服務之間通過openFeign實現(xiàn)遠程接口調(diào)用
現(xiàn)在的微服務項目不少都使用的是springboot+spring cloud構建的項目,微服務之間的調(diào)用都離不開feign來進行遠程調(diào)用。那么我們一個服務需要調(diào)用第三方的服務的時候,我們常??赡苁褂?code>httpclient或者restTemplate等客戶端api來實現(xiàn)遠程調(diào)用,其實我們可以在微服務沒有適用spring cloud框架的情況下,想調(diào)用第三方服務,也可以通過feign組件實現(xiàn)http的遠程調(diào)用。
實現(xiàn)過程
1.首先創(chuàng)建服務端項目,提供數(shù)據(jù)接口
1.1添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>1.2 配置application.yml
application.yml:
server:
port: 8080
spring:
application:
name: serviceDemo1.3 實體類
User:
package com.example.servicedemo.entity;
import lombok.Data;
/**
* 用戶信息
* @author qzz
*/
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}1.4 添加控制器方法
UserController:
package com.example.servicedemo.controller;
import com.example.servicedemo.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
/**
* @author qzz
*/
@RestController
public class UserController {
@RequestMapping("/api/user/getUserList")
public List<User> getUserList(){
//模擬數(shù)據(jù)庫請求數(shù)據(jù)
List<User> list = new ArrayList<>();
User user = new User();
user.setId(1);
user.setName("Jack");
user.setAge(31);
list.add(user);
return list;
}
}1.5 啟動類
package com.example.servicedemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author qzz
*/
@SpringBootApplication
public class ServiceDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceDemoApplication.class, args);
}
}瀏覽器訪問:http://localhost:8080/api/user/getUserList

2.創(chuàng)建客戶端項目,調(diào)用服務端接口
2.1添加依賴
<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>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>2.2 配置application.yml
application.yml:
server:
port: 8081
spring:
application:
name: clientName2.3 實體類
User:
package com.example.clientdemo.entity;
import lombok.Data;
/**
* @author qzz
*/
@Data
public class User {
private Integer id;
private String name;
private Integer age;
}2.4 創(chuàng)建OpenFeign接口
注意:@FeignClient的name和value屬性必填其一,另外url必填。
package com.example.clientdemo.feign;
import com.example.clientdemo.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* openFeign接口
* URL:就是遠程端需要調(diào)用接口的服務URL路徑,name:就是服務名,value和name一樣
* @author qzz
*/
@FeignClient(name = "serviceDemo",url = "http://localhost:8080")
public interface ServiceDemoFeign {
/**
* 獲取用戶列表
* @return
*/
@RequestMapping("/api/user/getUserList")
List<User> getUserList();
}2.5 添加控制器方法
UserController:
/**
* @author qzz
*/
@RestController
public class UserController {
/**
* 注入OpenFeign接口
*/
@Autowired
private ServiceDemoFeign serviceDemoFeign;
@RequestMapping("/api/client/user/getUserList")
public List<User> getUserList(){
return serviceDemoFeign.getUserList();
}
}2.6 啟動類
啟動類需要添加@EnableFeignClients注解。加入EnableFeignClients開啟Feign注解,使Feign的bean可以被注入
package com.example.clientdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author qzz
*/
@EnableFeignClients
@SpringBootApplication
public class ClientDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ClientDemoApplication.class, args);
}
}
2.7 測試效果
瀏覽器訪問:http://localhost:8081/api/client/user/getUserList

返回結果成功,說明服務調(diào)用成功。
完整代碼
點擊此處進行下載
到此這篇關于SpringBoot + openFeign實現(xiàn)遠程接口調(diào)用的文章就介紹到這了,更多相關SpringBoot openFeign遠程接口調(diào)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot+dubbo+validation 進行rpc參數(shù)校驗的實現(xiàn)方法
這篇文章主要介紹了springboot+dubbo+validation 進行rpc參數(shù)校驗的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-09-09
SpringBoot集成tika實現(xiàn)word轉html的操作代碼
Tika是一個內(nèi)容分析工具,自帶全面的parser工具類,能解析基本所有常見格式的文件,得到文件的metadata,content等內(nèi)容,返回格式化信息,本文給大家介紹了SpringBoot集成tika實現(xiàn)word轉html的操作,需要的朋友可以參考下2024-06-06

