Feign:负载均衡

官网地址
GitHub地址

Feign是什么?

  1. 声明式的REST客户端(伪RRC),它能使Web Service客户端更加简单;
  2. 采用基于接口的注解;
  3. Spring Cloud对Feign进行了封装,使其可以支持Spring MVC的标准注解和HTTPMessageConverters。
  4. Feign可以和EureKa和Ribbon结合使用

怎么用?

修改客户端的代码

1.POM.XML

引用相关依赖

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

2.修改主启动类

在启动类上添加注解
@EnableFeignClients

3.编写接口

demo

package com.mark.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * Created by Choisaaaa on 2018/7/10.
 */
@FeignClient(name = "PROVIDERPRODUCT")
public interface ProductClient {

    @GetMapping("/send")
    String msg();
}

接口上需要添加注解:@FeignClient(name = “PROVIDERPRODUCT”) ,name 属性值为服务中心上的服务名称。 @GetMapping(“/send”) 中的/send为提供者暴露的接口。

4.在controller层调用接口


    @Autowired
    private ProductClient productClient;

    @GetMapping("/getmsg4")
    public String msg4(){

        return productClient.msg();
    }

猜你喜欢

转载自blog.csdn.net/ycd500756/article/details/80991794
今日推荐