Idea build SpringCloud (four) ------ use Feign to achieve load balancing

Why does Feign appear? Isn't there a Ribbon before load balancing?

The load balancing we implemented before is to use Ribbon+RestTemplate, and use RestTemplate in the controller to access the service provider according to the url, but usually we call our declared service instance in the controller to call our logic, which does not comply with We usually face programming specifications, so the emergence of feign is to solve this specification problem.

We are still based on the previous project, create a new module named eureka-server-consumer-feign, add feign, ribbon and eureka dependencies to pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

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

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

Create a new service package, create a new ITestService interface in it, and add @FeignClient annotations. The name attribute is which set of service providers the service calls.

@FeignClient(name = "EUREKA-SERVER-PROVIDER")
public interface ITestService {

    @RequestMapping("/provider/test")
    String test();
}

Introduce ITestService in the controller:

@RestController
public class ConsumerController {

    @Autowired
    private ITestService iTestService;

    @RequestMapping("/consumer/test3")
    public String test3(){
        return iTestService.test();
    }

}

Startup class, plus @EnableFeignClients annotation, @EnableEurekaClient annotation, @EnableDiscoveryClient annotation:

@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
@EnableDiscoveryClient
public class EurekaServerConsumerFeignApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerConsumerFeignApplication.class, args);
    }

}

Configuration file application.yml:

server:
  port: 80

eureka:
  client:
    register-with-eureka: false
    service-url:
      defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/

Start Eureka clusters 7001, 7002 and 7003, service providers 8001, 8002 and 8003, and the consumer feign above us 

It turned out that the load balancing was also successfully implemented using feign. In fact, the feature of Ribbon is integrated in feign.

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_37689658/article/details/88603101