foreword
Recently learning how to use springcloud, access to Feign and Ribbon when learning to cross-service call interface, online many articles is a distinction here between the two of them, are interested can look at this paper and the recording operation is recommended to use Feign process.
Feign and Ribbon comparison
Ribbon
Ribbon is a load balancer based on HTTP and TCP clients
It can configure ribbonServerList (server list) on the client side, and then poll requests to achieve load balancing
It is used in conjunction with Eureka
ribbonServerList will be rewritten by DiscoveryEnabledNIWSServerList and extended to get server list from Eureka registry
It also replaces IPing with NIWSDiscoveryPing, which delegates the responsibility to Eureka to determine if the server has been started
Feign
Spring Cloud Netflix's microservices are all exposed in the form of HTTP interfaces, so they can be called with Apache's HttpClient or Spring's RestTemplate
Feign is a more convenient HTTP client to use
To sum up: the server interface published to the registry is HTTP, and it can be accessed directly from a browser without Ribbon or Feign.
It's just that Ribbon or Feign are more convenient to call. The most important thing is: they both support soft load balancing
Note: spring-cloud-starter-feign already includes spring-cloud-starter-ribbon (Ribbon is also used in Feign)
From a practical point of view, it is more elegant to use feign (feign also uses ribbon for load balancing).
operate
1. Introduce feign dependency package
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2. Enable feign: @EnableFeignClients
@SpringBootApplication
// 资源保护服务
@EnableResourceServer
// 服务发现
@EnableDiscoveryClient
// 启用feign
@EnableFeignClients
@RefreshScope
public class NofityServiceApplication {
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
public static void main(String[] args) {
SpringApplication.run(NofityServiceApplication.class, args);
}
}
3. Configure feign to pass the token
@Configuration
public class FeignConfig implements RequestInterceptor {
@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String authorization = request.getHeader("authorization");
log.info("--- authorization ---:{}", authorization);
if (authorization != null) {
log.info(" ---- set authorization ---");
//添加token
requestTemplate.header("authorization", authorization);
}
}
}
4. Use feign to call other
Create a new AccountFeignClient.java file
@FeignClient("account-service")
public interface AccountFeignClient {
@GetMapping("/userInfo/{userName}")
User getAccountInfoByUserName(@PathVariable("userName") String userName);
}
This completes all the operations of feign configuration, try to call is OK!
Summarize
1. When feign is called across services, there will be token transfer, so be sure to add the above config configuration
2. If the called service interface does not need a token, if the interface you call passes the token, it will report 401, so pay attention to this!
quote
difference between Ribbon and Feign of service consumption in SpringCloud
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。