springCloud-21 基于feign 的服务调用

目录

一,引入依赖

二,定义feign 调用接口

三,在服务消费者启动类上激活feign注解

四,通过自动的接口调用远程微服务


feign 的用法

一,引入依赖

谁使用feign,或者谁是服务消费者谁添加依赖

        <!--springCloud整合feign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

二,定义feign 调用接口

package com.zjk.order.feign;

import com.zjk.order.entity.TbProduct;
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.RequestMethod;

//指定需要调用的服务名称
@FeignClient(name = "service-product")
public interface productFeignHttpClient {

    //调用的请求路径
    @RequestMapping(value = "/product/{id}",method = RequestMethod.GET)
    public TbProduct findById(@PathVariable("id") Long id);
}
  • 定义各参数绑定时,@PathVariable@RequestParam@RequestHeader等可以指定参数属性,在Feign中绑定参数必须通过value属性来指明具体的参数名,不然会抛出异常
  • @FeignClient:注解通过name指定需要调用的微服务的名称,用于创建Ribbon的负载均衡器。所以Ribbon会把 shop-service-product 解析为注册中心的服务。

三,在服务消费者启动类上激活feign注解

@EnableFeignClients

package com.zjk.order;

import com.netflix.discovery.DiscoveryClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EntityScan("com.zjk.order.com.zjk.product.entity")
@EnableEurekaClient
//通过@EnableFeignClients 激活feign
@EnableFeignClients
public class OrderApplication {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

四,通过自动的接口调用远程微服务

修改 OrderController ,添加 ProductFeginClient 的自动注入,并在 order 方法中使用
ProductFeginClient 完成微服务调用
    @Autowired
    private productFeignHttpClient productFeignHttpClient;
    /**
     * 基于ribbon 的服务调用,使用服务提供者的服务名称
     * @param Id
     * @return
     */
    @GetMapping(value = "/findByIdByRibbon/{Id}")
    public TbProduct findByIdByRibbon(@PathVariable Long Id){
        try {
            TbProduct byId = productFeignHttpClient.findById(Id);
            return byId;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

vegetari

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

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

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

打赏作者

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

抵扣说明:

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

余额充值