🌈 Column Introduction

Thank you for reading, I hope to help you, if there is a flaw in the blog, please leave a message in the comment area or add my private chat in the home page, thank you for every little partner generous advice. I am XiaoLin, a boy who can both write bugs and sing rap. This column mainly introduces the most mainstream micro services solution, SpringCloudAlibaba, which will be introduced in groups. Column address: SpringCloudAlibaba

  • 3️ Ribbon (suggested collection)
  • 3️ Nacos (suggested collection)
  • 2️ retail (suggested collection)
  • 1️ retail (suggested collection)

Remote call: Feign

7.1 Introduction to Feign

Feign is a declarative pseudo Http client provided by Spring Cloud that makes calling remote services as simple as calling local services, creating an interface and adding an annotation.

Nacos is very compatible with Feign, which integrates the Ribbon by default, so using Fegin in Nacos is load balancing by default.

7.2 Feign Combat

7.2.1. Adding a Dependency

Add Fegin’s dependency to the POM file of the shop-order-server project.

<! - fegin component - >
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
Copy the code

7.2.2 Add annotations

We need to add the @enableFeignClients annotation on the startup class, which will scan only if it is present.

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

  @Bean
  @LoadBalanced
  public RestTemplate getInstance(a){
    return newRestTemplate(); }}Copy the code

7.2.3 Added ProductFeignApi

Add an interface to shop-order-server.

// Name specifies the name of FeignClient. If the project uses the Ribbon, the name attribute will be used as the name of the microservice for service discovery
@FeignClient(name = "product-service")
public interface ProductFeignApi {
  @RequestMapping("/product")
  Product findById(@RequestParam("productId") Long productId);
}
Copy the code

7.2.4. Modify Controller

  @Autowired
  ProductFeignApi productFeignApi;
  @Override
  public Order getById(Long oid, Long pid) {
    Product product = productFeignApi.findById(pid);
    Order order = orderDao.getOne(oid);
    order.setPname(product.getPname());
    return order;
  }
Copy the code

7.3 Important attributes of Feign

We can configure the timeout property.

feign:
  client:
    config:
      default:
        connectTimeout: 5000
        readTimeout: 5000
Copy the code

7.4 Implementation principle of Feign

  1. After startup, the @SpringBootApplication configured on the startup class scans the @FeignClient annotated class and creates a proxy object for it.
  2. Get the interface implemented by the proxy class via reflection: ProductFeignApi.
  3. Get the interface annotation by reflection and take the name attribute from the annotation center: product-Service.
  4. Use reflection to get the method in the interface, and get the annotation @requestMapping on the method in the interface, and get the value: /product.
  5. Also pull out the values in the method’s parameter annotations. This is the parameter we passed in: productId.
  6. Concatenated path: http://product-service/product? ProductId = 1.
  7. Locate the node based on the local service list.
  8. Select nodes based on the ribbon load balancing policy you have configured.
  9. Replace the product-service with the node information and port.
  10. Use RestTemplate to send the request.