【springcloud系列】springcloud整合openfeign

大家好,我是walker
一个从文科自学转行的程序员~
爱好编程,偶尔写写编程文章和生活
欢迎关注公众号【I am Walker】,回复“电子书”,就可以获得200多本编程相关电子书哈~
我的gitee:https://gitee.com/shen-chuhao/walker.git 里面很多技术案例!

集成openfeign

1、引入依赖,记得版本一致【可以优先引入dependencyManagement】,然后引入openfeign和loadbalancer


<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2020.0.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

<!--openfeign和loadbalancer是配套使用的,需要一块引入-->
   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>

2、将服务注册到nacos或者eureka等注册中心

3、启动类添加注解@EnableFeignClients


@SpringBootApplication(scanBasePackages = {"com.walker"})
@MapperScan("com.walker.infrastructure.mapper")
@EnableFeignClients //添加该注解
public class FunctionLearnApplication {

    public static void main(String[] args) {
        SpringApplication.run(FunctionLearnApplication.class, args);
    }

}

4、新增FeignService服务类,写上注解@FeignClient,@Service/@Component注册容器

@FeignClient("boring-school")  //被调用的服务名,和注册中心的保持一致
@Service 
public interface TestFeignService {

    //将被调用服务的接口方法复制过来,补全调用方法路径
    @GetMapping("/boring/test/test1")
    R test();
}

//被调用服务接口的代码
    @GetMapping("test1")
    public R test(){
        return R.success("hello weclome to cloud");
    }

image

5、测试

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    //1、注入FeignService
    @Autowired
    private TestFeignService testFeignService;

    @GetMapping("/test")
    public void test(){
        //2、调用方法
        R test = testFeignService.test();
        log.info("openfeign返回结果:{}",test);
    }
}

//返回结果:
//2021-10-18 11:06:28.478  INFO 1992 --- [io-10024-exec-1] c.w.interfaces.client.UserController     : openfeign返回结果:R(code=200, data=hello weclome to cloud, msg=操作成功)</pre>

超时控制

最新的openfeign整合的loadbalancer,而不是ribbon,ribbon的话默认1s会超时,这里需要去了解一下

遇到问题

No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer?

缺少loadbalancer依赖,将其引入即可,因为在高版本入springcloud 2020.0.2中的openfeign是结合loadbalancer进行负载均衡的

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WalkerShen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值