1、引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2、启动类添加注解 @EnableFeignClients
@SpringBootApplication
@EnableFeignClients
public class OpenfeignApplication {
public static void main(String[] args) {
SpringApplication.run(OpenfeignApplication.class, args);
}
}
3、创建feign service
@FeignClient中参数说明:
value : 服务名称,如果是通过url方式的,随便起个名字就行,如果是调用nacos服务,则填写nacos服务名称
url: 远程调用地址
path: 统一前缀
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "tpt-server",url = "http://192.168.0.23:8080",path = "/order")
public interface OpenfeignService {
@GetMapping("/baseParam")
Object getAddBaseParam();
@GetMapping("/baseParam2")
Object getAddBaseParam2(@RequestParam("name") String name);
@PostMapping("/writeValue")
Result writeValue(@RequestBody WriteValueDTO dto);
}
注意:一般写Get方法时,入参可以省略@RequestParam注解,但使用Feign调用时不可省略