Spring cloud 之Feign基本使用

首先导入feign的依赖:

<!-- 添加feign声明式webservice client -->
<dependence>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-feign</artifactId>
</dependence>

主程序:

@SpringBootApplication
@EnableEurekaClient
// 支持声明式webService client
@EnableFeignClients
public class SpringCloudMovieApplication {
    public static void main(String[] args) {
          SpringApplication.run(SpringCloudMovieApplication.class,args);
      }  
}    

  

Feign客户端接口:

// name属性表示请求哪个微服务,这里请求的是用户微服务,name表示用户微服务的vip
@FeignClient(name="spring-cloud-user")
public interface IUserService {
    @RequestMapping(value="/user/{id}",method=RequestMethod.GET) 
     // 注意这里不支持@GetMapping 
    // @PathVariable("id")不这么写,会报错:
    // PathVariable annotation was empty on param 0
     public User findUserById(@PathVariable("id")Long id);
}

  

 

posted on 2018-09-18 09:56  zzzhouheng  阅读(209)  评论(0编辑  收藏  举报

导航