feign
pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
application.yml
server:
port: 9002
spring:
application:
name: consumerfeign
eureka:
client:
# 表示是否将自己注册进入EurekaServer默认为true
register-with-eureka: false
# 是否从EurekaServer抓取已有的注册信息,默认为true
# 单节点不会有影响,集群环境必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
# 注册到的EurekaServer路径
service-url:
defaultZone: http://localhost:7001/eureka
编写接口
FeignClient的value为微服务名字
方法上加上路径
package com.springcloud.service;
import com.springcloud.domain.Empt;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
@Component
@FeignClient(value = "PROVIDER")
public interface EmptFeignService {
@GetMapping("empt")
List<Empt> getAll();
}
启用注解
@SpringBootApplication
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
使用
@RestController
public class AppController {
@Resource
private EmptFeignService service;
@GetMapping
public List<Empt> getAll() {
return service.getAll();
}
}