SpringCloud学习之Feign服务调用


上一节 SpringCloud学习之RestTemplate中介绍了如何利用Spring提供的RestTemplate方法来完成微服务间的调用,本节将在上节的基础上介绍Feign的使用。Feign的使用更为简洁,提供一个接口即可完成对其他微服务接口的调用。

添加依赖

feign的依赖如下

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.0.RELEASE</version> 
</dependency>

更改service-consumer的主启动类

添加@EnableFeignClients注解,注入Spring容器

package com.rui;

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

/**
 * @author WangRui
 * @date 2020/10/20 16:26
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ServiceConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceConsumerApplication.class,args);
    }
}

service-consumer服务中添加UserServiceFeign

@FeignClient(value = "service-provider"):该注解可以为该方法找到对应的微服务名称
@RequestMapping("/user/selectAll"):映射对应的微服务的访问url

package com.rui.service;

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

/**
 * @author WangRui
 * @date 2020/10/21 15:44
 */
@FeignClient(value = "service-provider")
@Component
public interface UserServiceFeign {
    @RequestMapping("/user/selectAll")
    String selectAll();
}

更改service-consumer的UserService

添加selectAllFeign()方法用于调用UserServiceFeign接口

package com.rui.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author WangRui
 * @date 2020/10/21 14:29
 */
@Service
public class UserService {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserServiceFeign userServiceFeign;

    public String selectAll(){
        return restTemplate.getForObject("http://localhost:8082/user/selectAll",String.class);
    }

    public String selectAllFeign(){
        return userServiceFeign.selectAll();
    }

}

更改service-consumer的UserController

提供@RequestMapping("/selectAllFeign")来使用feign调用微服务接口

package com.rui.controller;

import com.rui.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author WangRui
 * @date 2020/10/21 14:32
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/selectAll")
    public String selectAll(){
        return userService.selectAll();
    }

    @RequestMapping("/selectAllFeign")
    public String selectAllFeign(){
        return userService.selectAllFeign();
    }
}

通过调用http://localhost:8083/user/selectAllFeign,同样可以实现微服务应用间的调用。

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值