Spring Cloud 2-Feign 声明式服务调用(三)

时间:2022-11-25 01:04:50
Spring Cloud Feign 

简化RestTemplate调用形式

1. pom.xml

<!-- feign 声明式服务调用 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2. application.yml

spring:
application:
name: feign-client server:
port: 8086

3. Application.java

@SpringBootApplication
@EnableFeignClients
public class FeginClientApplication { public static void main(String[] args) {
SpringApplication.run(FeginClientApplication.class, args);
} }

@EnableFeignClients 启动Feign客户端

4. Client.java

@Service
@FeignClient("hello-service")
public interface HelloServiceClient { @RequestMapping(method = RequestMethod.GET, value = "/hello")
String hi(); }
  • @FeignClient("hello-service") 指定注册服务的名字
  • /hello 服务请求地址

注意:

@RequestMapping(method = RequestMethod.GET, value = "/hello")
  1. 不能使用GetMapping
  2. 需通过value指定请求路径

访问: http://localhost:8086/feign/hi

Hello World!