一、什么是Feign,有什么作用?
Feign是一个伪http客户端,它可以是服务调用变得简单。只需要创建一个接口,并注解,就可以用它。Feign支持可插拔特性。它继承了Ribbon,和Eureka配合使用,并默认实现负载均衡,它还整合了熔断器Hytrix。
二、环境准备需要什么?
1、启动eureka服务,端口8088
2、创建并启动服务提供者,服务名称:provider-name
3、查看服务
图:注册
三、如何创建Feign项目?
1、新建一个springboot项目
图:新建
2、依赖引入以下组件:
图:组件
3、进行主类配置
package com.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
4、创建feign接口
package com.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "provider-name")
public interface FeignInterface {
@RequestMapping(value = "/get",method = RequestMethod.GET)
String FromClientOne();
}
5、注入并实现接口
package com.feign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignController {
@Autowired
FeignInterface feignInterface;
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String sayHi(){
return feignInterface.FromClientOne();
}
}
6、配置application.properties
eureka.client.serviceUrl.defaultZone: http://localhost:8088/eureka/
server.port: 9300
spring.application.name: service-feign
7、访问localhost:9300/test
图:结果
本文暂时没有评论,来添加一个吧(●'◡'●)