SpringCloud Learning Series -Feign load balancing (2)

Feign use the steps

1. Reference microservicecloud-consumer-dept-80 New microservicecloud-consumer-dept-feign

  Modify the master boot class name DeptConsumer80_Feign_App

2.microservicecloud-consumer-dept-feign project pom.xml modifications, mainly to add support for the feign

<dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-feign</artifactId>
   </dependency>

3. Modify microservicecloud-api project

POM

< Project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: the xsi = "http://www.w3.org/2001/XMLSchema-instance" 
  the xsi: the schemaLocation = "HTTP: / http://maven.apache.org/xsd/maven-4.0.0.xsd /maven.apache.org/POM/4.0.0 " > 
  < modelVersion > 4.0.0 </ modelVersion > 
 
  < parent > <! - subclass which shows a clear statement in order to inherit the performance, no accident is the default version of the parent class or their own definition -> 
   < groupId > com.atguigu.springcloud </ groupId > 
   < artifactId > microservicecloud </artifactId>
   <Version > 0.0.1-SNAPSHOT </ Version > 
  </ parent > 
 
  < artifactId > microservicecloud-API </ artifactId > <-! Current Module myself what was the name -> 
 
  < the Dependencies > <-! Current Module needed to jar package, according to their own needs to add, if the parent class already included, you can not write the version number -> 
   < dependency > 
     < groupId > org.projectlombok </ groupId > 
     < artifactId > Lombok </ artifactId > 
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-feign</artifactId>
   </dependency>
  </dependencies>
 
 
 
</project>

New DeptClientService interfaces and add comments @FeignClient

package com.atguigu.springcloud.service;
 
import java.util.List;
 
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import com.atguigu.springcloud.entities.Dept;
 
@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);
}
 

mvn clean

mvn install

4.microservicecloud-consumer-dept-feign Engineering Change Controller, step on the new interfaces added DeptClientService

package com.atguigu.springcloud.controller;
 
import java.util.List;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import com.atguigu.springcloud.entities.Dept;
import com.atguigu.springcloud.service.DeptClientService;
 
@RestController
public class DeptController_Feign
{
  @Autowired
  private DeptClientService service = null;
 
  @RequestMapping(value = "/consumer/dept/get/{id}")
  public Dept get(@PathVariable("id") Long id)
  {
   return this.service.get(id);
  }
 
  @RequestMapping(value = "/consumer/dept/list")
  public List<Dept> list()
  {
   return this.service.list();
  }
 
  @RequestMapping(value = "/consumer/dept/add")
  public Object add(Dept dept)
  {
   return this.service.add(dept);
  }
}
 

5.microservicecloud-consumer-dept-feign engineering to modify the master boot class

package com.atguigu.springcloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
 
 
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages= {"com.atguigu.springcloud"})
@ComponentScan("com.atguigu.springcloud")
public class DeptConsumer80_Feign_App
{
  public static void main(String[] args)
  {
   SpringApplication.run(DeptConsumer80_Feign_App.class, args);
  }
}
 
 

6. Test

Start three clusters eureka

Start three departments microService 8001/8002/8003

Start their own start Feign

http://localhost/consumer/dept/list

Feign own load-balancing configuration items

7. Summary

Feign interface method by calling Rest service (before a Ribbon + RestTemplate),
send the request to the Eureka server (http: // MICROSERVICECLOUD-DEPT / dept / list),
find the service interface directly through Feign, due to the time during the service call combines Ribbon technology, it also supports load balancing effect.

Guess you like

Origin www.cnblogs.com/XiangHuiBlog/p/12095823.html