添加依赖

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

启动类

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

OrderController

@RestController
public class OrderController {
  @Autowired
  private UserFeignClient userFeignClient;
  @Autowired
  private RefactorUserService refactorUserService;

  @GetMapping("/user/{id}")
  public User findById(@PathVariable Long id) {
    return userFeignClient.findById(id);
  }
  
  @GetMapping("/user-extends/{id}")
  public User findById2(@PathVariable Long id) {
	  return refactorUserService.getUser(id);
  }
  
}

feign

@FeignClient(name = "microservice-provider-user")
public interface UserFeignClient {
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public User findById(@PathVariable("id") Long id);
}

user服务端配置

server:
  port: 8010
spring:
  application:
    name: microservice-consumer-order
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true

Eureka配置

server:
  port: 8761                    # 指定该Eureka实例的端口
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

# 参考文档:http://projects.spring.io/spring-cloud/docs/
            1.0.3/spring-cloud.html#_standalone_mode

使用feign建立抽象接口工程

//另外的工程,先构建,后引入
public interface UserService {
  @GetMapping("/user/{id}")
  public User getUser(@PathVariable(value = "id") Long id);
}

主工程引入

import com.tuling.cloud.study.service.UserService;
import org.springframework.cloud.netflix.feign.FeignClient;
@FeignClient(name = "microservice-provider-user")
public interface RefactorUserService extends UserService {

}