Spring Cloud项目(五)——使用openFeign作为服务调用

项目结构

在这里插入图片描述

模块代码

springcloud-provider模块

和之前一样

springcloud-consumer模块

1、pom文件

        <dependency>
            <groupId>org.example</groupId>
            <artifactId>springcloud-feign</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

2、启动类
在这里插入图片描述
3、controller

package com.lx.controller;


import com.alibaba.nacos.client.naming.net.HttpClient;
import com.lx.User;
import com.lx.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
public class UserController {

    // 1. 发请求到http://localhost:8080/user/findById/1  HttpUrlConnection / HttpClient
    // 2. 负载均衡问题: nacos-provider(n个) 获取服务(nacos-provider/ip)

//    /**
//     * 发送请求
//     */
//    @Autowired
//    private RestTemplate restTemplate;
//
//    /**
//     * 负载均衡
//     */
//    @Autowired
//    private LoadBalancerClient client;


    @Autowired
    private UserFeign userFeign;

    @RequestMapping("/findById/{userId}")
    public User findById(@PathVariable Integer userId){
        // 根据注册到nacos上面名称,从众多的provider选择一个
//        ServiceInstance instance = client.choose("nacos-provider");
//        String url = "http://"+instance.getHost()+":"+instance.getPort()+"/user/findById/"+userId;
//        return restTemplate.getForObject(url, User.class);
        return userFeign.findById(userId);
    }
}
springcloud-feign模块

1、pom文件

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>springcloud-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

2、接口

package com.lx.feign;

import com.lx.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

// Http客户端 --> 发请求(provider位置)  serverId(名称)
// 负载均衡功能(ribbon)
@FeignClient(value = "nacos-provider")  // 寻找ip和端口
public interface UserFeign {

    @RequestMapping("/user/findById/{userId}")
    User findById(@PathVariable Integer userId);

    @RequestMapping("/user/getParams")
    String getParams(@RequestParam Integer userid,@RequestParam String password);
}

测试结果

在这里插入图片描述

什么是openFeign

OpenFeign是一个显示声明式的WebService客户端。使用OpenFeign能让编写Web Service客户端更加简单。使用时只需定义服务接口,然后在上面添加注解。

OpenFeign也支持可拔插式的编码和解码器。spring cloud对feign进行了封装,使其支持MVC注解和HttpMessageConverts。和nacos(服务注册中心)和ribbon组合可以实现负载均衡。

在Spring Cloud中使用OpenFeign,可以做到使用HTTP请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程方法,更感知不到在访问HTTP请求,非常的方便。

结语

下面关于Spring Cloud学习系列的博客的连接有的是不能使用的,因为博主还在努力创作中,敬请期待

附:Spring Cloud学习系列

Spring Cloud项目(一)——集成Nacos作为注册中心
Spring Cloud项目(二)——集成Nacos作为配置中心
Spring Cloud项目(三)——实现Nacos数据信息持久化到MySQL
Spring Cloud项目(四)——使用Ribbon作为负载均衡
Spring Cloud项目(五)——使用openFeign作为服务调用
Spring Cloud项目(六)——使用sentinel作为流量管理
Spring Cloud项目(七)——使用sentinel作为限流和熔断
Spring Cloud项目(八)——使用gateway作为服务网关

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

--流星。

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

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

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

打赏作者

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

抵扣说明:

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

余额充值