SpringBoot集成feign
核心
- 引入jar:
spring-cloud-starter-openfeign
- 配置yml:
feign.service.url: https://www.baidu.com/doc
- 启动feign-client:
@EnableFeignClients
- 编写feign服务: FeignService.java
- 调用feign服务
示例代码
在pom.xml
添加feign
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.0</version>
</dependency>
在application.yml
添加URL
weather.api.url: https://tianqiapi.com
在启动类添加@EnableFeignClients
@EnableFeignClients
@SpringBootApplication
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
编写feign服务
@FeignClient(url = "${weather.api.url}", name = "weather")
public interface WeatherService {
@GetMapping("/api?version=v6&appid={appId}")
JSONObject getEngineMessage(@PathVariable("appId") String appId, @RequestParam("appsecret") String appSecret);
}
调用feign服务
@Service
public class FeignService {
@Resource
private WeatherService weatherService;
public JSONObject getWeatherMessage() {
return weatherService.getEngineMessage("appId", "appSecret");
}
}
参考