SpringCloud study notes (six, SpringCloud Netflix Feign)

table of Contents:

  • feign Profile
  • feign application

feign profile:

feign open source Netflix is ​​a declarative, template-based http client, it can be more convenient and elegant calling http api; SpringCloud of Netflix's feign been enhanced to support spring and integrated ribbon, eureka to provide load balanced http call.

feign Application:

1, rely on the introduction openfeign

1 <dependency>
2     <groupId>org.springframework.cloud</groupId>
3     <artifactId>spring-cloud-starter-openfeign</artifactId>
4 </dependency>

2, start the classes with the feign comment (eureka need support, so this module you first need to eureka clients)

) A first, feign api for scanning type

@EnableFeignClients(clients = {Xxx1.class, Xxx2.class})

) The second, for packet scanning feign api

@EnableFeignClients(basePackages = {"com.xxx.xxx"})

3, the definition of feign api

Just feign api and api module consistent on it

) Module:

1 @RestController
2 @RequestMapping("/ad")
3 public class AdApi {
4 
5     @GetMapping("/getUserAd/{account}")
6     public String getUserAd(@PathVariable(name = "account") String account) {
7         return "这是" + account + "的广告";
8     }
9 }

)feign api:

1 @FeignClient(name = "ad-model")
2 public interface AdRemoteService {
3 
4     @GetMapping("/ad/getUserAd/{account}")
5     String getUserAd(@PathVariable(name = "account") String account);
6 }

4, call

Call is very simple, just like calling methods on it

 1 @Autowired
 2 private AdRemoteService adRemoteService;
 3 
 4 @GetMapping("/login/{account}/{password}")
 5 public String login(@PathVariable String account, @PathVariable String password) {
 6     UserDTO userDTO = USER_INFO.get(account);
 7     if (userDTO == null) {
 8         return "FAILED";
 9     }
10 
11     boolean result = userDTO.getPassword().equalsIgnoreCase(password);
12     if (!result) {
13         return "FAILED" ;
 14      }
 15  
16      // call the interface advertising 
. 17      String ADResult = adRemoteService.getUserAd (Account);
 18 is      System.err.println (ADResult);
 . 19  
20 is      return "SUCCESS" ;
 21 is }

Guess you like

Origin www.cnblogs.com/bzfsdr/p/11661968.html