您现在的位置是:首页 >技术教程 >SpringCloud-OpenFeign案例实战网站首页技术教程
SpringCloud-OpenFeign案例实战
简介SpringCloud-OpenFeign案例实战
关于Spring Cloud Open Feign的介绍可以参考这两篇博客
OpenFeign服务接口调用
使用Feign作为服务消费者
本博客参考gitee开源项目代码,结合自己的理解,记录下微服务场景下的使用。Talk is cheap. Show me the code!
一、项目结构
这里使用eureka作为注册中心,,person和equipment两个web服务作为业务中台,本例中会使用postman调用person服务,person服务中调用equipment服务。
registry -- 注册中心(当前采用eureka)
person -- 人员服务
person-api -- 人员相关api提供, 包括 req, resp, service 等
person-biz -- 人员相关服务接口具体实现, 依赖person-api
person-provider -- 人员相关微服务启动类, 依赖person-biz
equipment -- 设备服务
equipment-api -- 设备相关api提供, 包括 req, resp, service 等
equipment-biz -- 设备相关服务接口具体实现, 依赖equipment-api
equipment-provider -- 设备相关微服务启动类, 依赖equipment-biz
源码下载地址,欢迎star!
springboot-openfeign
二、registry – 注册中心(当前采用eureka)
1、application.yml
server:
port: 8001 # 该服务端口
eureka:
instance:
hostname: registry # eureka的实例名称
client:
registerWithEureka: false # false表示当前项目不以客户端注册到服务中心(因为该项目本身就是注册中心)
fetchRegistry: false # false表示当前项目不需要从注册中心拉取服务配置(因为该项目本身就是注册中心)
serviceUrl:
defaultZone: http://localhost:8001/eureka/
spring:
application:
name: server-registy # 当前项目的实例名称(很重要)
2、核心pom.xml
<!-- 引入eureka-server依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<!-- gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
3、主启动类RegistryApplication
@SpringBootApplication
@EnableEurekaServer
public class RegistryApplication {
public static void main(String[] args) {
SpringApplication.run(RegistryApplication.class, args);
}
}
三、person – 人员服务
1、application.yml
server:
port: 8002
spring:
application:
name: person-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8001/eureka/
feign:
httpclient:
enabled: true
2、核心pom.xml
cloud-person-provider
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
cloud-person-biz
<!-- 使用web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!-- person-api -->
<dependency>
<groupId>tca</groupId>
<artifactId>cloud-person-api</artifactId>
<version>1.0.0</version>
</dependency>
<!-- equipment-api -->
<dependency>
<groupId>tca</groupId>
<artifactId>cloud-equipment-api</artifactId>
<version>1.0.0</version>
</dependency>
cloud-person-api
<!-- openFeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
3、PersonFeign和PersonController
@FeignClient(value = "person-provider")
public interface PersonFeign {
/**
* 根据id获取人员
* @param personReq
* @return
*/
@PostMapping("/person/get")
PersonResp get(PersonReq personReq);
}
@RestController
@RequestMapping(value = "/person")
@Slf4j
public class PersonController {
@Autowired
private EquipmentFeign equipmentFeign;
/**
* 获取人员
* @param personReq
* @return
*/
@PostMapping("/get")
public PersonResp get(@Validated @RequestBody PersonReq personReq) {
PersonResp personResp = new PersonResp();
personResp.setId(personReq.getId());
personResp.setAge(30);
personResp.setName("Messi");
EquipmentReq equipmentReq = new EquipmentReq();
equipmentReq.setId(personReq.getId());
EquipmentResp equipmentResp = equipmentFeign.get(equipmentReq);
log.info("equipmentResp = {}", equipmentResp);
return personResp;
}
}
4、项目结构
四、equipment – 设备服务
1、application.yml
server:
port: 8003
spring:
application:
name: equipment-provider
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8001/eureka/
2、核心pom.xml
cloud-equipment-api
<!-- openFeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
cloud-equipment-biz
<!-- 使用web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<!-- equipment-api -->
<dependency>
<groupId>tca</groupId>
<artifactId>cloud-equipment-api</artifactId>
<version>1.0.0</version>
</dependency>
<!-- person-api -->
<dependency>
<groupId>tca</groupId>
<artifactId>cloud-person-api</artifactId>
<version>1.0.0</version>
</dependency>
cloud-equipment-provider
<dependency>
<groupId>tca</groupId>
<artifactId>cloud-equipment-biz</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
3、EquipmentFeign和EquipmentController
@FeignClient(value = "equipment-provider")
public interface EquipmentFeign {
/**
* 根据id获取人员
* @param equipmentReq
* @return
*/
@PostMapping("/equipment/get")
EquipmentResp get(EquipmentReq equipmentReq);
}
@RestController
@RequestMapping(value = "/equipment")
@Slf4j
public class EquipmentController {
@Autowired
private PersonFeign personFeign;
/**
* 获取人员
* @param equipmentReq
* @return
*/
@PostMapping("/get")
public EquipmentResp get(@Validated @RequestBody EquipmentReq equipmentReq) {
EquipmentResp equipmentResp = new EquipmentResp();
equipmentResp.setId(equipmentReq.getId());
equipmentResp.setName("平板设备");
return equipmentResp;
}
}
4、项目结构
五、测试
分别启动这三个服务,
利用postman访问localhost:8002/person/get,其中body中id传不同参数进行测试
可以发现能够通过openfeign在微服务之间进行接口调用!
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。