SpringCloud(二):声明式RestClient—Feign

 (编写不易,转载请注明:http://shihlei.iteye.com/blog/2399457

 

一 概述

 

        feign: 声明式rest 客户端,spring cloud 扩展了feign,提供了springmvc的标签支持,替代RestTemplate简化操作。

        git:https://github.com/OpenFeign/feign

 

二 项目

 

        服务提供者:《 SpringCloud(一): SpringBoot 创建简单的微服务》中的时间微服务——spring-cloud-microservice

        服务消费者:《 SpringCloud(一): SpringBoot 创建简单的微服务》改进——spring-cloud-webfront 中 TimeService 实现

 

三 代码实现

(1)添加依赖

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

 

(2)资源文件添加时间微服务服务器配置:

server:
  port: 20001

# 自定义配置
timeMisroService:
  server: http://localhost:10001

 

 

(3)定义feign接口

package x.demo.springcloud.webfront.service.impl.feign;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import x.demo.springcloud.webfront.service.impl.ProtocolResult;

@FeignClient(name = "microservice-time", url = "${timeMisroService.server}")
public interface TimeV1MicroServiceClient {

    @RequestMapping(method = RequestMethod.GET, value = "/time/v1/now", consumes = MediaType.APPLICATION_JSON_VALUE)
    ProtocolResult<String> now(@RequestParam(name = "format", required = false) String format);
}

 

(4)实现TimeService

package x.demo.springcloud.webfront.service;

public interface TimeService {
    /**
     * 获取当前时间
     * @return 当前时间,格式:yyyy-MM-dd HH:mm:ss
     */
    String now();
}

 

package x.demo.springcloud.webfront.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import x.demo.springcloud.webfront.service.TimeService;
import x.demo.springcloud.webfront.service.impl.feign.TimeV1MicroServiceClient;

@Service("timeV1FeignImpl")
public class TimeV1FeignImpl implements TimeService {

    @Resource
    private TimeV1MicroServiceClient timeV1MicroServiceClient;

    /**
     * 获取当前时间
     *
     * @return 当前时间,格式:yyyy-MM-dd HH:mm:ss
     */
    @Override
    public String now() {
        ProtocolResult<String> result = timeV1MicroServiceClient.now(null);
        return result.getBody();
    }
}

(5)controller 调用服务

 

package x.demo.springcloud.webfront.web;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import x.demo.springcloud.webfront.service.TimeService;

@RestController
@RequestMapping("/time")
public class TimeController {

    @Resource(name = "timeV1FeignImpl")
    private TimeService timeService;

    @GetMapping("/now")
    public String now() {
        return timeService.now();
    }
}

 

 

(6)开启FeignClient支持:主要是添加@EnableFeignClients,用于Spring Cloud 生成 实现

 

package x.demo.springcloud.webfront;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class SpringCloudWebfrontApplication {

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

   

(7)效果:同上篇

四 在普通的项目中使用 feign

区别:因为么有spring cloud 实例FeignClient,所以只能自己实例化FeignClient

 

package x.demo.springcloud.webfront.conf;

import feign.Feign;
import feign.Request;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.netflix.feign.support.SpringMvcContract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import x.demo.springcloud.webfront.service.impl.feign.TimeV1MicroServiceClient;

@Configuration
public class SpringConfiguration {

    @Value("${timeMisroService.server}")
    private String timeMisroServiceServer;

    @Bean
    public TimeV1MicroServiceClient timeV1MicroServiceClient() {
        return Feign.builder()
                .encoder(new GsonEncoder())
                .decoder(new GsonDecoder())
                .contract(new SpringMvcContract())
                .options(new Request.Options(5000, 5000))
                .target(TimeV1MicroServiceClient.class, timeMisroServiceServer);
    }

}

 

五 其他

(1)feign 目前不支持Mvc注解 @GetMapping @PostMapping 会报错

 

(2)提供json序列化支持

依赖:

        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-gson</artifactId>
        </dependency>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值