SpringCloud study notes (4) load balancing --Feign

Load balancing --Feign

1 difference, Ribbon and the Feign

Ribbon and Feign are calling for other services, but in different ways.

Different Notes 1.1 startup class use, Ribbon use the @ RibbonClient, Feign using a @EnableFeignClients.

1.2 Different services specified position, Ribbon is @RibbonClient annotations on declaration, Feign @FeignClient statement is used in the definition of the abstract interface methods.

1.3 calls different ways, Ribbon need to build their own http requests, simulate http request is then sent to other services using RestTemplate, steps quite tedious. Feign is carried out on the basis of an improved Ribbon, using the interface the way, the other method of service is defined as the need to call an abstract method, do not need to build their own http request. Note, however, that notes abstract methods, methods and method signatures to provide exactly the same services.

 

Ribbon-- service-oriented

Feign-- oriented interfaces

2, Feign use

The introduction of the jar package

 <!-- Feign相关 -->
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-feign</artifactId>
   </dependency>

Adding an interface:

@FeignClient(value="MICROSERVICECLOUD-DEPT")
   public interface DeptClientService {

       @RequestMapping(value="/dept/get/{id}", method= RequestMethod.GET)
       public Dept get(@PathVariable("id") long id);

       @RequestMapping(value="/dept/list", method= RequestMethod.GET)
       public List<Dept> list();

       @RequestMapping(value="/dept/add", method= RequestMethod.POST)
       public boolean add(Dept dept);
  }

Note: The access path interface corresponding to the service provider's External exposure

Start class add annotations:

@EnableFeignClients(basePackages = {"com.springcloud"})
@ComponentScan("com.springcloud")

 

Guess you like

Origin www.cnblogs.com/qzt666/p/11364064.html