Feign的简单使用

springCloud提供的微服务接口调用有 Ribbon+RestTemplate和feign两种,Feign是一种负载均衡的HTTP客户端, 使用Feign调用API就像调用本地方法一样,从避免了调用目标微服务时,需要不断的解析/封装json 数据的繁琐。Feign集成了Ribbon。Ribbon+eureka是面向微服务编程,而Feign是面向接口编程。Feign同样具有负载均衡功能。

项目demo构建参考:https://blog.csdn.net/f123147/article/details/115904274
在Ppom文件中假如Feign的依赖

 <!-- 微服务组件-feign -->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>${spring.cloud.netflix.version}</version>
    </dependency>

在前端front工程启动类上写上@EnableFeignClients注解

package com.gmf.briup;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class FrontApplication {

    public static void main(String[] args) {

        SpringApplication.run(FrontApplication.class, args);
    }

}

在前端front工程中service层创建接口

package com.gmf.briup.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/*
*  这是一个接口,Feign会通过动态代理,帮我们生成实现类
*/
@FeignClient(value = "order-service")  //调用哪个微服务
public interface FeignService {

    @ResponseBody
    @RequestMapping("/orderService/service/getPort")
    String getPort();
}


  

在前端接口front工程web层创建对外暴露的接口

package com.gmf.briup.controller;

import com.gmf.briup.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/feign")
public class FeignController {

    @Autowired
    FeignService feignService;

    @ResponseBody
    @RequestMapping(value = "/getOrderServicePort")
    public String sayHi(){
    //调用service层的方法,该方法和order-service微服务的接口绑定在一起
        String port = feignService.getPort();
         System.out.println(port);
       return port;
    }
}

在浏览器中输入front工程对外暴露的接口:http://127.0.0.1:8090/feign/getOrderServicePort
结果如下
在这里插入图片描述
从这里可以看出,Feign的负载均衡算法默认也是轮询。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Feign是一个HTTP客户端,它可以将HTTP请求转发到其他微服务。使用Feign可以使得微服务之间的调用更加简单和优雅。下面是使用Feign的详细步骤: 1. 添加依赖 在使用Feign之前,需要先在pom.xml文件中添加Feign的依赖,如下所示: ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> ``` 2. 创建Feign接口 在使用Feign时,需要先创建一个接口,用于定义需要调用的其他微服务的API。这个接口的方法签名和被调用的微服务的API方法签名必须一致。例如,如果需要调用另一个微服务的getUserInfo方法,那么Feign接口的定义如下所示: ```java @FeignClient(name = "user-service") public interface UserService { @GetMapping("/user/getUserInfo") public UserInfo getUserInfo(@RequestParam("userId") String userId); } ``` 在这个例子中,@FeignClient注解用于指定需要调用的微服务的名称,name属性的值为"user-service",这个值需要和被调用的微服务的spring.application.name属性的值一致。接着,定义了一个getUserInfo方法,这个方法的签名与被调用的微服务的getUserInfo方法的签名一致,使用@GetMapping注解标注请求的路径,这里的路径为"/user/getUserInfo"。最后,定义了一个UserInfo类型的返回值,用于封装被调用的微服务的返回结果。 3. 注入Feign接口 在需要使用Feign调用其他微服务的地方,可以直接注入Feign接口,例如在Service层中注入上面定义的UserService接口: ```java @Service public class UserServiceImpl implements UserService { @Autowired private UserService userService; public UserInfo getUserInfo(String userId) { return userService.getUserInfo(userId); } } ``` 在这个例子中,UserServiceImpl类注入了UserService接口,并且调用了getUserInfo方法。在实际运行时,Feign会根据接口的定义动态生成一个HTTP客户端,并将请求转发到其他微服务。 4. 启用Feign使用Feign时,需要在Spring Boot应用程序的启动类上添加@EnableFeignClients注解,如下所示: ```java @SpringBootApplication @EnableFeignClients public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 在这个例子中,@EnableFeignClients注解用于启用Feign客户端。在启用Feign之后,Spring Boot会自动扫描所有标注了@FeignClient注解的接口,并为它们动态生成HTTP客户端。 以上就是使用Feign的详细步骤。使用Feign可以使得微服务之间的调用更加简单和优雅,提高代码的复用性和可维护性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值