OpenFeign实现服务调用—SpringCloud(H版)微服务学习教程(22)

IT 文章5年前 (2021)发布 小编
0 0 0

通过之前的学习,我们知道可以使用OpenFeign代替Ribbon+RestTemplate实现服务的负载均衡调用,下面我们来准备好演示环境。

第1步:创建Module

我们在mscloud下创建名为cloud-consumer-feign-order80模块

第2步:改pom

pom.xml如下:

ad

程序员导航

优网导航旗下整合全网优质开发资源,一站式IT编程学习与工具大全网站



    
        mscloud
        com.panziye.springcloud
        1.0-SNAPSHOT
    
    4.0.0

    cloud-consumer-feign-order80
    
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
        
            com.panziye.springcloud
            cloud-api-commons
            ${project.version}
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-actuator
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        

        
            org.projectlombok
            lombok
            true
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

    


我们除了Eureka依赖还添加了OpenFeign依赖(如果你的依赖报错,请加入版本号),我们打开依赖查看,发现OpenFeign依赖以及集成了Ribbon
OpenFeign实现服务调用—SpringCloud(H版)微服务学习教程(22)

第3步:写yml

application.yml如下:

server:
  port: 80

spring:
  application:
    name: cloud-order-feign-service

eureka:
  client:
    #表示是否将自己注册进eureka服务中心,默认true
    register-with-eureka: false
    #表示是否从EurekaServer抓取已有注册信息,默认true。单节点无所谓,集群必须设置true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:  #指向eureka集群
      defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka

第4步:主启动

新建OrderFeignMain80主启动类:

package com.panziye.springcloud;

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

@SpringBootApplication
@EnableFeignClients
public class OrderFeignMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMain80.class,args);
    }
}

[v_blue]提示:@EnableFeignClients用于激活OpenFeign客户端[/v_blue]

ad

AI 工具导航

优网导航旗下AI工具导航,精选全球千款优质 AI 工具集

第5步:业务类

1)在service包下新建OrderFeignService接口:
[v_blue]注意:这里定义的接口应与服务提供者的service层接口对应,我这里只演示get,另外其返回值与服务提供者的controller对应,这就是我们希望的面向微服务接口编程。[/v_blue]

package com.panziye.springcloud.service;

import com.panziye.springcloud.entities.CommonResult;
import com.panziye.springcloud.entities.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface OrderFeignService {
    //匹配服务提供者请求Mapping
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id);
}

[v_warn]注意:@FeignClient注解表明此接口使用OpenFeign发请求,value指定服务提供者应用名称,@PathVariable不能丢[/v_warn]
2)在controller包下新建OrderFeignController类:

package com.panziye.springcloud.controller;

import com.panziye.springcloud.entities.CommonResult;
import com.panziye.springcloud.entities.Payment;
import com.panziye.springcloud.service.OrderFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderFeignController {
    @Resource
    private OrderFeignService orderFeignService;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPayment(@PathVariable("id") Long id){
        return orderFeignService.getPaymentById(id);
    }
}

第6步:测试

我们先启动7001、7002、7003这3个eureka注册中心,再启动8001和8002这两个服务提供者,最后启动我们新建的OrderFeignMain80,访问http://localhost/consumer/payment/get/1测试,发现正常访问,默认使用的是Ribbon的轮询策略。

© 版权声明

相关文章

暂无评论

暂无评论...