Spring boot整合Spring cloud Feign

目录

0.序言---版本

Spring boot 2.5.6------spring-cloud-starter-openfeign   3.0.3

1.新建Spring boot 项目 导入Spring Cloud Feign 依赖

2.启动类开启 Feign 注解  @EnableFeignClients

3.新建interface服务调用接口

4.新建Controller 调用服务接口

5.下面新建一个服务方接口,测试Feign远程调用接口


0.序言---版本

Spring boot 2.5.6------spring-cloud-starter-openfeign   3.0.3

Springboot 和 Cloud  版本冲突是个很让人头疼的问题,作者以前项目中 Spring boot 版本是1.5.9,现在因为一些原因boot版本 升到2.5.6,但是Feign的版本 便与之不匹配,爆了各种错最后才发现是版本冲突的问题,友情提示一点,Spring boot 和 Spring Cloud 整合的时候,报错一定要考虑到是不是,版本冲突的问题。

1.新建Spring boot 项目 导入Spring Cloud Feign 依赖

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

2.启动类开启 Feign 注解  @EnableFeignClients

3.新建interface服务调用接口

package com.wlh.web.feign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "fegin-service",url = "http://localhost:9091/")
public interface CallFeginClientService {

	@GetMapping("/testFegin")
	public String  getFeginMessage();
	
	//参数
	@GetMapping("/pushMessage.action")
	public String  testPutStringToMq(@RequestParam (value = "id") int id);

}

@FeignClient 注解 name:服务名(此处demo随意定义)

url:访问服务路径

第一个方法,没有参数;第二个方法带有参数

4.新建Controller 调用服务接口


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wlh.web.feign.service.CallFeginClientService;

@RestController
public class CallFeginClientController { 

	@Autowired
	private CallFeginClientService feginervice;

	@GetMapping("/testFeginClientToMessage")
	public String getFeginMessage(int id){
		Object feginMessage = feginervice.testPutStringToMq(id);
		System.out.println(feginMessage);
		return feginMessage.toString();
	}

}

1234都是调用方

5.下面新建一个服务方接口,测试Feign远程调用接口


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MessageController {
 
    @GetMapping("/pushMessage.action")
    public Object get(@RequestParam("id") int id) {
    	System.out.println("调用成功,参数id为======"+id);
        return "调用成功";
    }
 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值