使用OpenFeign代替RestTemplate来请求接口

在使用RestTemplate来调用接口服务的时候,需要写一个调用地址的参数,这样做不太好管理接口地址,OpenFeign就提供了一个比较好的管理方式,类似于写一个Mapper的接口,把调用的地址也写一个接口来管理。

Feign是Spring Cloud组件中的一个轻量级RESTful的HTTP服务客户端,Feign本身不支持Spring MVC的注解,它有一套自己的注解,OpenFeign是Spring Cloud 在Feign的基础上支持了Spring MVC的注解,Feign内置了Ribbon,用来做客户端负载均衡。只有是客户端的项目才会发起接口调用,所以OpenFeign是用于客户端的。

OpenFeign = Feign(Robbin) + 注解

RestTemplate方式的控制器方法:

使用OpenFeign的步骤:

  1. 引入jar包
  2. 启动类加注解
  3. 编写Api接口
  4. 控制器调用
  5. 配置Ribbon随机轮询

1.引入jar包

找包

导包

把这块包的声明放入项目pom.xml文件中的<dependencies>节点中,保存

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

 2.启动类加注解 @EnableFeignClients

package com.example;

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

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class Consumer8203Application {

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

}

3.编写Api接口 ProductApi.java

package com.example.api;

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

// 指明要调的接口在哪个服务上的服务名(找Eureka)
@FeignClient(value = "PRODUCT-SERVICE")
public interface ProductApi {
    
    // 指明要调用的接口的路径
    @RequestMapping("/index")
    public String index();
}

4.控制器调用

package com.example.controller;

import com.example.api.ProductApi;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class IndexController {
    
    @Resource
    RestTemplate restTemplate;
    
    @Autowired
    ProductApi productApi;
    
    @GetMapping("/a")
    public String index(){
        
        // 直接使用注册中心,服务提供项目的项目名称去请求就可以了
        String url = "http://PRODUCT-SERVICE/index";
        
        String result = restTemplate.getForObject(url, String.class);
        
        return result;
    }
    
    @GetMapping("/feign")
    public String feign(){
        String result = productApi.index();
        return result;
    }
}

到这里就可以启动消费者的项目了,访问 http://localhost:8203/feign 的时候,可以看到返回的接口不断的交替。这就是Ribbon默认的负载方式。

 接下来改一下Ribbon的负载方式,改成随机的方式:

5.配置Ribbon随机轮询

修改application.yml

spring:
  application:
    name: consume # 消费者的实例名称
    
server:
  port: 8203 # 消费者实例的端口号

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8201/eureka/,http://localhost:8221/eureka/  # 注册中心的服务地址,用于将消费者注册进去

# 需要改变ribbon轮询调用方式的服务名
PRODUCT-SERVICE:
  ribbon:
    # 改为随机的方式
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

 保存,重启项目再次访问 http://localhost:8203/feign ,不断的刷新就能够发现输入的接口是随机的了。

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值