一、依赖
Spring Cloud版本信息
1 <properties> 2 <spring-cloud.version>Greenwich.SR2</spring-cloud.version> 3 </properties>
openfeign依赖
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-openfeign</artifactId> 5 </dependency> 6 </dependencies> 7 <dependencyManagement> 8 <dependencies> 9 <dependency> 10 <groupId>org.springframework.cloud</groupId> 11 <artifactId>spring-cloud-dependencies</artifactId> 12 <version>${spring-cloud.version}</version> 13 <type>pom</type> 14 <scope>import</scope> 15 </dependency> 16 </dependencies> 17 </dependencyManagement>
二、Application注解:
@EnableFeignClients
三、FeignService:
可以单独使用value/name,也可以使用value/name和url组合的方法来使用;
1 @FeignClient(name = "org-feign-service", url = "http://localhost:8080/o/org")
1 /** 2 * @return The service id with optional protocol prefix. Synonym for {@link #value() 3 * value}. 4 */ 5 @AliasFor("value") 6 String name() default "";
1 /** 2 * @return an absolute URL or resolvable hostname (the protocol is optional). 3 */ 4 String url() default "";
1 /** 2 * The name of the service with optional protocol prefix. Synonym for {@link #name() 3 * name}. A name must be specified for all clients, whether or not a url is provided. 4 * Can be specified as property key, eg: ${propertyKey}. 5 * @return the name of the service with optional protocol prefix 6 */ 7 @AliasFor("name") 8 String value() default "";
注意:FeignService中方法的传参参数。形式要和被调用Controller中的保持一致
1 /** 2 * @param org_id 3 * @return 4 */ 5 @GetMapping("/{org_id}") 6 Org findById(@PathVariable(name = "org_id") int org_id);
1 /** 2 * @param org_id 3 * @return 4 */ 5 @GetMapping("/{org_id}") 6 public List<Org> findById(@PathVariable(name = "org_id") int org_id) { 7 ...处理操作... 8 return orgList; 9 }
四、在Controller中使用FeignService:
1 @Autowired 2 private OrgFeignService orgFeignService;