SpringCloud之初入江湖-Feign实现服务间的调用

Feign简介

Feign是简化Java HTTP客户端开发的工具(java-to-httpclient-binder),它的灵感来自于Retrofit、JAXRS-2.0和WebSocket。Feign的初衷是降低统一绑定Denominator到HTTP API的复杂度,不区分是否为restful。

快速体验

我们现在在问答微服务调用基础微服务的方法(根据ID查询标签)

1)在tensquare_qa模块添加依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

2)修改tensquare_qa模块的启动类,添加注解

@EnableEurekaClient
@EnableDiscoveryClient
package com.tensquare.qa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import util.IdWorker;
import util.JwtUtil;

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class QaApplication {
	public static void main(String[] args) {
		SpringApplication.run(QaApplication.class, args);
	}
	@Bean
	public IdWorker idWorkker(){
		return new IdWorker(1, 1);
	}
	@Bean
	public JwtUtil jwtUtil(){
		return new JwtUtil();
	}
}

3)在tensquare_qa模块创建 com.tensquare.qa.client包,包下创建接口

package com.tensquare.qa.client;

import com.tensquare.qa.client.impl.BaseClientImpl;
import entity.Result;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "tensquare-base")
public interface BaseClient {
    @RequestMapping(value = "/label/{labelId}", method = RequestMethod.GET)
    public Result findById(@PathVariable("labelId") String labelId);
}

说明:
1)@FeignClient注解用于指定从哪个服务中调用功能 ,注意里面的名称与被调用的服务名保持一致,并且不能包含下划线。
2)@RequestMapping注解用于对被调用的微服务进行地址映射。注意 @PathVariable注解一定要指定参数名称,否则出错

4)修改tensquare_qa模块的 ProblemController

	@Autowired
	private BaseClient baseClient;

	@RequestMapping(value = "/label/{labelId}", method = RequestMethod.GET)
	public Result findByLabelId(@PathVariable String labelId){
		Result result = baseClient.findById(labelId);
		return result;
	}

5)测试

http://192.168.2.10:9003/problem/label/1 能看到标签的信息

{"flag":true,"code":20000,"message":"查询成功","data":{"id":"1","labelname":"java","state":"1","count":null,"fans":null,"recommend":null}}

负载均衡

测试:同时开启多个基础微服务,看是否是轮流调用。
修改tensquare_base工程LabelController的findById方法

    @RequestMapping(value = "/{labelId}", method = RequestMethod.GET)
    public Result findById(@PathVariable("labelId") String id){
        System.out.println("NO.1");
        Label label = labelService.findById(id);
        return new Result(true,StatusCode.OK, "查询成功", label);
    }

启动基础微服务后,修改端口和输出信息,再次启动基础微服务
启动问答微服务,浏览器执行http://192.168.2.10:9003/problem/label/1 看是否轮流启动。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值