OpenFeign是一个Web声明式的Http客户端调用工具,提供接口和注解形式调用。开发微服务,免不了需要服务间调用。Spring Cloud框架提供了RestTemplate和FeignClient两个方式完成服务间调用,本文简要介绍如何使用OpenFeign完成服务间调用,下面开始进行整合:
首先基于IDEA建立maven聚合项目如下图:
其中,springcloud-alibaba-parent是父项目,存放一些共同依赖,service-api,service-impl是springcloud-alibaba-parent的module子模块,service-api-member,service-api-order是service-api的module子模块,service-impl-member,service-impl-order是service-impl的module子模块。
在springcloud-alibaba-parent引入如下公共依赖:
4.0.0
com.xyy
springcloud-alibaba-parent
pom
1.0-SNAPSHOT
service-api
service-impl
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-alibaba-nacos-discovery
0.2.2.RELEASE
org.springframework.cloud
spring-cloud-starter-openfeign
2.0.0.RELEASE
service-impl-member依赖如下:
com.xyy
service-api-member
1.0-SNAPSHOT
service-impl-order依赖如下:
com.xyy
service-api-member
1.0-SNAPSHOT
com.xyy
service-api-order
1.0-SNAPSHOT
service-impl-member配置文件如下:
spring:
application:
# 服务名称
name: service-member
cloud:
nacos:
discovery:
# nacos注册地址
server-addr: 127.0.0.1:8848
server:
port: 8081
service-impl-order配置文件如下:
spring:
application:
# 服务名称
name: service-order
cloud:
nacos:
discovery:
# nacos注册地址
server-addr: 127.0.0.1:8848
server:
port: 8090
在service-api-member编写Member服务:
package com.xyy;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
public interface MemberService {
/**
* 提供我们发布的接口
* @param userId
* @return
*/
@GetMapping("/getUser")
String getUser(@RequestParam("userId") Integer userId);
}
在service-impl-member编写实现类:
package com.xyy.impl;
import com.xyy.MemberService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MemberServiceImpl implements MemberService {
@Value("${server.port}")
private String serverPort;
@Override
public String getUser(Integer userId) {
return "我是会员服务" + serverPort;
}
}
在service-impl-order编写openFeign客户端(直接实现会员服务接口即可):
package com.xyy.openfeign;
import com.xyy.MemberService;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("service-member")
public interface MemberServiceFeign extends MemberService {
}
在service-impl-order编写订单实现:
package com.xyy.impl;
import com.xyy.openfeign.MemberServiceFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderService {
@Autowired
private MemberServiceFeign memberServiceFeign;
/**
* 基于我们的feign客户端形式实现rpc远程调用
* @return
*/
@RequestMapping("/orderFeignToMember")
public String orderFeignToMember() {
String result = memberServiceFeign.getUser(1);
return "我是订单服务调用会员服务的接口,返回结果" + result;
}
}
分别启动service-impl-member(8080),service-impl-member(8081),service-impl-order(8090),直接请求orderFeignToMember接口,会发现,底层默认封装了Ribbon进行客户端负载均衡:
【小结】:
· 订单调会员的时候,会员服务接口的方法参数,一定要加@RequestParam注解,订单可以不用加。
· 在微服务架构中Nacos服务的名称命名不能够有下划线(service_order是错误写法,openfeign调用的时候报错)。