声明式HTTP客户端-Feign

feign是声明式的web service客户端,它让微服务之间的调用变得更简单了,类似controller调用service。Spring Cloud集成了Ribbon和Eureka,可在使用Feign时提供负载均衡的http客户端。

 

 

目录

 

 

 

 

1.使用 Feign

2.Feign的组成:

3.细粒度配置自定义:

1.使用Java代码配置方式:

2.使用属性方式:

4.全局配置:

        1.Java代码实现:

2.属性方式实现:

5.配置最佳实践总结:

6.feign的继承特性;

7. 多参数请求构造:

8.feign脱离Ribbon 使用:

9. RestTemplate  vs Feign:

10.Feign常见的问题总结:


 

 


1.使用 Feign

   1.在客户端的pom文件中添加依赖:

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

         2. 写注解: 在启动类上添加: @EnableFeignClients

3.编写一个接口:

@FeignClient(name = "user-center")
public interface UserCenterFeignClient {

    /**
     *   http://user-center/users/{id}
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    UserDTO findById(@PathVariable Integer id);
}

4.调用接口:

UserDTO userDTO =userCenterFeignClient.findById(userId);

原先的代码:

        UserDTO userDTO = restTemplate.getForObject(
                "http://user-center/users/{userId}",
                UserDTO.class,userId);

 

2.Feign的组成:

 

3.细粒度配置自定义:

 

BASIC用于生产环境 ,FULL 用于开发环境

 

1.使用Java代码配置方式:

   1.创建一个UserCenterFeignConfiguration类:


public class UserCenterFeignConfiguration {

    /**
     *    这里的Logger 是  feign.Logger 包下的
     * @return
     */
    @Bean
          public Logger.Level level(){
              //让 feign 打印所有请求的细节
              return Logger.Level.FULL;
          }
}

2. 在原有的UserCenterFeignClient类上 的  @FeignClient 注解上添加一些 字段:configuration = UserCenterFeignConfiguration.class

原有的代码不变


@FeignClient(name = "user-center",configuration = UserCenterFeignConfiguration.class)
public interface UserCenterFeignClient {

    /**
     *   http://user-center/users/{id}
     * @param id
     * @return
     */
    @GetMapping("/users/{id}")
    UserDTO findById(@PathVariable Integer id);
}

3.配置yml 文件:

feign的日志级别 是  建立在 feign的接口的日志级别上的

注意:在feign的配置类UserCenterFeignConfiguration上 : 不能添加  @Configuration 【若添加了@Configuration ,则需将UserCenterFeignConfiguration 移到启动类所在目录之外,否则 UserCenterFeignConfiguration类中的所有配置就会被所有的 Feign Client 共享】

2.使用属性方式:

 

1. 将上文的注解属性进行注释

2.在yml 文件上:

4.全局配置:

 

1.Java代码实现:

         1.注释掉上文的 yml的 feign的细粒度配置:

2.找到启动类上的 @EnableFeignClients 注解:

@EnableFeignClients(defaultConfiguration = GlobalFeignConfiguration.class)

     GlobalFeignConfiguration 和UserCenterFeignConfiguration 内容一样,只是改了名称,    UserCenterFeignConfiguration类上文已创建

2.属性方式实现:

 

 1.首先注释掉 上文的启动类上的注解:

2.在 yml文件中:

 

5.配置最佳实践总结:

优先级:  全局代码<全局属性<细粒度代码<细粒度属性

 

6.feign的继承特性;

 

 

7. 多参数请求构造:

 

参考: http://www.imooc.com/article/289000

8.feign脱离Ribbon 使用:

      1.创建一个feignclient 接口:

/**
 *  这里的 name 可以随意取值,但是不能为空
 */
@FeignClient(name = "baidu",url = "http://www.baidu.com")
public interface TestBaiduFeignClient {

    @GetMapping
    String index();
}

2.在测试 controller中:

@Autowired
private TestBaiduFeignClient testBaiduFeignClient;
@GetMapping("/baidu")
public String baiduIndex(){
     return   testBaiduFeignClient.index();
}

 

9. RestTemplate  vs Feign:

10.Feign常见的问题总结:

参考 :http://www.imooc.com/article/289005

猜你喜欢

转载自blog.csdn.net/weixin_42528855/article/details/115078603