使用Feign 实现声名式 REST调用

使用Feign 实现声名式 REST调用

上一篇:springcloud认证security
下一篇:Feign对于压缩的支持

引入Fegin

前面的示例中是使用RestTemplate实现 REST API 调用的, 代码大致如下:

	@RequestMapping("user/{id}")
    public User findById(@PathVariable("id") Integer id){
    	User user = restTemplate.getForObject("http://provider/user/"+id, User.class);
        return user;
    }

我们是使用拼接字符串的方式构造 URL的,该 URL只有一个参数。然而在现实中, URL中往往有多个参数。如果这时还使用这种方式构造URL , 那么就会变得很低效, 并且难以维护。举个例子,想 要请求这样的 URL:

http://localhost:8010/search?name=张三&username=account1&age=20

在这里,URL 仅包含 3 个参数。如果URL 更加复杂 , 例如有10 个以上的参数, 那么代码会变得难以维护。

为消费者整合Feign

1 在pom.xml中添加添加Feign依赖

    <!--springboot1.5.9版本适用-->
    <!--
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-feign</artifactId>
                <version>1.4.7.RELEASE</version>
            </dependency>
    -->
      <!--springboot 2.1版本适用 -->
    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
        <version>2.1.3.RELEASE</version>
    </dependency>

2 创建一个Feign接口,并添加@FeignClent注解
该接口与提供者的方法保持一致。

import com.itzz.springcloudconsumer.pojo.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(name = "provider")
@Component
public interface UserFeignClient {
    @RequestMapping("user/{id}")
    public User findById(@PathVariable(value = "id") Integer id);
}

3 修改Controller,让其调用Feign接口
自动注入UserFeignClient接口,接口调用里面的findById方法

@RestController
public class UserController {
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${user.userServiceUrl}")
    private String userServiceUrl;
    @RequestMapping("user/{id}")
    public User findById(@PathVariable("id") Integer id){
//        return restTemplate.getForObject("http://provider/user/"+id, User.class);
//        return restTemplate.getForObject(userServiceUrl+id,User.class);
        return userFeignClient.findById(id);
    }
}

4 修改启动类,为其添加@EnableFeignClients注解。

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class SpringcloudConsumerApplication {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    /**自定义配置ribbon负载均衡算法
     * @return
     */
    @Bean
    public IRule myRule(){
//        return new RoundRobinRule();//轮询
        return new RandomRule();
//        return new RetryRule();//重试

//        return new BestAvailableRule();
    }

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

}

5 测试重启consumer项目
可以访问到就说明没什么问题
在这里插入图片描述

url中多个参数的REST调用

在provider中添加多参数的url方法
一种是restful风格,一种是正常的那种方式传参如下所示:


import com.itzz.springcloudprovider.dao.UserRepository;
import com.itzz.springcloudprovider.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.soap.SOAPBinding;
import java.util.List;


@RestController
public class UserController {
    @Autowired
    private UserRepository userRepository;
    @GetMapping("user/{id}")
    public User findById(@PathVariable(value = "id") Integer id){
        return userRepository.getOne(id);
    }
    //restful传多个参
    //http://localhost:8000/search/1/张三/zhangsan
    @GetMapping("search/{id}/{username}/{name}")
    public User search(@PathVariable(value = "id") Integer id,@PathVariable("username") String username,@PathVariable("name") String name){
        return userRepository.getOne(id);
    }
    //http://localhost:8000/search2?id=1&username=zhangsan&name=张三
    @GetMapping("search2")
    public User search2(Integer id,String username,String name){
        return userRepository.getOne(id);
    }

}

2 在消费者项目的UserFeignClient 接口中添加相关接口。

	//restful传多个参
    @RequestMapping("search/{id}/{username}/{name}")
    public User search(@PathVariable(value = "id") Integer id,@PathVariable("username") String username,@PathVariable("name") String name);
    @RequestMapping("search2")
    public User search2(@RequestParam("id") Integer id, @RequestParam("username") String username, @RequestParam("name") String name);

注意:feign接口:如果有多个参数,需要通过@RequestParam 注解指定参数名。
3 在消费者的controller中实现调用UserFeignClient相关接口

@RestController
public class UserController {
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private RestTemplate restTemplate;
    @Value("${user.userServiceUrl}")
    private String userServiceUrl;
    @RequestMapping("user/{id}")
    public User findById(@PathVariable("id") Integer id){
//        return restTemplate.getForObject("http://provider/user/"+id, User.class);
//        return restTemplate.getForObject(userServiceUrl+id,User.class);
        return userFeignClient.findById(id);
    }
    @RequestMapping("search/{id}/{username}/{name}")
    public User search(@PathVariable("id") Integer id,@PathVariable("username") String username,@PathVariable("name") String name){
        return userFeignClient.search(id, username, name);
    }
    @RequestMapping("search2/{id}/{username}/{name}")
    public User search2(@PathVariable("id") Integer id,@PathVariable("username") String username,@PathVariable("name") String name){
        return userFeignClient.search2(id, username, name);
    }
}

5 测试
先用提供者访问看是否能得到数据
http://localhost:8001/search/3/zhangsan/zz
在这里插入图片描述
然后再用消费者去访问
http://localhost:8011/search/3/zhangsan/zz
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值