九、SpringCloud之Feign的使用

一、Feign简介

  • 声明式Rest客户端(伪RPC)
  • 采用基于接口的注解(定义接口,然后在接口上添加注解‘)

二、使用步骤

客户端:

@RestController
public class ServerController {
    @GetMapping("/msg")
    public String msg(){
        return "this is product msg";
    }
}

服务端:

  • 1.引入 Feign 依赖
                <!-- 引入feign 应用通信-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
			<version>1.4.5.RELEASE</version>
		</dependency>
  • 2在启动的主类上加 @EnableFeignClients 注解
@EnableDiscoveryClient //开启服务发现功能
@SpringBootApplication
@EnableFeignClients//开启Feign功能
public class OrderApplication {
	public static void main(String[] args) {
		SpringApplication.run(OrderApplication.class, args);
	}
}
  • 3.声名调用哪个应用的哪个方法
/**
 * 调用客户端接口
 * */
@FeignClient(name="product") //name是指客户端的名字
public interface ProductClient {
    @GetMapping("/msg")//调用客户端的哪个方法
    String productMsg(); // 方法名可以不一样
}
  • 4.调用
@RestController
@Slf4j
public class ClientController {

    @Autowired
    private ProductClient productClient;
    @GetMapping("/getProductMsg")
    public String msg(){
        String response = productClient.productMsg();
        log.info("response={}",response);
        return response;
    }
}
  • 5.测试

测试地址:http://localhost:8866/getProductMsg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值