openfeign

集成OpenFeign

  • 添加pom依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
  • 新增feign接口
    client新增feign接口,声明server服务端的接口方法
package com.zzuhai.client.feign;


import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

// server-nacos是服务端名称,/server是服务端controller接口路径
@FeignClient(value = "server-nacos", path = "/server")
public interface ServerFeignService {

    // 下面是服务端方法的接口
    @RequestMapping("/index")
    String index();
}
  • 修改调用方式
    修改client调用server方法代码,引入刚声明的接口,直接调用
@RestController
@RequestMapping("/client")
public class ClientController {

    @Autowired
    ServerFeignService serverFeignService;

    @RequestMapping("/index")
    public String index() {
        String msg = serverFeignService.index();
        return "Hello, Client Nacos!" + msg;
    }

}
  • 新增feign注解
    启动类新增注解@EnableFeignClients
@SpringBootApplication
@EnableFeignClients
public class ClientApplication{
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值