【feign】demo

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 {

    /**
     * 查询指定城市天气状态
     * @param appId     AppId
     * @param appSecret AppSecret
     * @return 天气状况
     */
    @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");
  }

}

参考

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值