继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

SpringCloud-Feign接口转换服务

哔哔one
关注TA
已关注
手记 340
粉丝 93
获赞 543

Feign是通过提供面向接口操作的方式来替代RestTemplate API的Rest操作。

使用Feign

Feign这种技术应用在服务消费端

  1. 修改pom.xml配置文件,加入Feign的依赖包

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-feign</artifactId></dependency>
  1. 由于Fegin是将Rest的操作转换成接口的形式,所以我们需要新建一个接口,并在接口上声明@FeignClient注解

@FeignClient(value = "DEPT-PROVIDER",configuration = FeignClientConfig.class)public interface DeptClientService {    @RequestMapping(method= RequestMethod.GET,value="/dept/get/{id}")    public Dept get(@PathVariable("id") long id) ;    @RequestMapping(method=RequestMethod.GET,value="/dept/list")    public List<Dept> list() ;    @RequestMapping(method=RequestMethod.POST,value="/dept/add")    public boolean add(Dept dept) ;
}@Configurationpublic class FeignClientConfig {    @Bean
    public Logger.Level getFeignLoggerLevel() {        return feign.Logger.Level.FULL ;
    }    @Bean
    public BasicAuthRequestInterceptor getBasicAuthRequestInterceptor() {        return new BasicAuthRequestInterceptor("admin", "admin");
    }
}

其中configuration = FeignClientConfig.class不是必须的,将configuration属性去除仍然能work。

  1. 将之前的Rest操作的API,替换成面向DeptClientService接口的形式

@RestController@RequestMapping("/consumer/dept")public class ConsumerDeptController {    @Autowired
    private DeptClientService deptClientService;    @RequestMapping(value = "/get")    public Dept get(long id) {        return this.deptClientService.get(id);
    }    @RequestMapping("/list")    public List<Dept> list(){        return this.deptClientService.list();
    }    @RequestMapping("/add")    public boolean add(Dept dept){        return this.add(dept);
    }/*
    public static final String DEPT_GET_URL = "http://DEPT-PROVIDER/dept/get/";
    public static final String DEPT_LIST_URL = "http://DEPT-PROVIDER/dept/list/";
    public static final String DEPT_ADD_URL = "http://DEPT-PROVIDER/dept/add";

    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private HttpHeaders httpHeaders;
    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping(value = "/get")
    public Dept get(long id) {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("DEPT-PROVIDER") ;
        System.out.println(
                "【*** ServiceInstance ***】host = " + serviceInstance.getHost()
                        + "、port = " + serviceInstance.getPort()
                        + "、serviceId = " + serviceInstance.getServiceId());
        //Dept dept = restTemplate.getForObject(DEPT_GET_URL + id, Dept.class);
        Dept dept = restTemplate.exchange(DEPT_GET_URL+id, HttpMethod.GET,new HttpEntity<Object>(this.httpHeaders),Dept.class).getBody();
        return dept;
    }

    @RequestMapping("/list")
    public List<Dept> list(){
        //List<Dept> deptList = restTemplate.getForObject(DEPT_LIST_URL, List.class);
        List<Dept> deptList = this.restTemplate.exchange(DEPT_LIST_URL,HttpMethod.GET,new HttpEntity<Object>(this.httpHeaders),List.class).getBody();
        return deptList;
    }

    @RequestMapping("/add")
    public boolean add(Dept dept){
        //Boolean flag = restTemplate.postForObject(DEPT_ADD_URL, dept, Boolean.class);
        Boolean flag = this.restTemplate.exchange(DEPT_ADD_URL,HttpMethod.POST,new HttpEntity<Object>(this.httpHeaders),Boolean.class).getBody();
        return flag;
    }*/}
  1. 在启动类中加入@EnableFeignClients注解

@SpringBootApplication@EnableEurekaClient@EnableFeignClients(basePackages = {"cn.zgc.service"})public class FeignConsumer_80_StartSpringCloudApplication {    public static void main(String[] args) {
        SpringApplication.run(FeignConsumer_80_StartSpringCloudApplication.class,args);
    }
}

Feign自带了负责均衡特性,所以使用Feign之后可以不用使用Ribbon。

Feign的配置

Feign 最重要的功能就是将 Rest 服务的信息转换为接口,但是在实际的使用之中也需要考虑到一些配置情况,例如:数据压缩, Rest 的核心本质在于: JSON 数据传输( XML、文本),于是就必须思考一种情况,万一用户发送的数据很大呢? 所以这个时候可以考虑修改application.yml 配置文件对传输数据进行压缩;

feign:
 compression:
 request:
 mime-types:  # 可以被压缩的类型
 - text/xml
 - application/xml
 - application/json
 min-request-size: 2048 # 超过2048的字节进行压缩

开启Feign的日志(默认是不开启的)

logging: level: cn.zgc.service: DEBUG



作者:Coding小聪
链接:https://www.jianshu.com/p/48ebe8ec5152

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP