前面的章节虽然完成了服务的注册以及相互间的调用,但是调用方发起调用时,还是硬编码的形式,如果服务比较多,代码不好管理。如,用户服务调用订单服务:
@PostMapping("/findOrderList")
public JSONArray findOrderList() throws Exception {
String serviceId = "ORDER";
String url = "http://" + serviceId + "/order/findOrderList";
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
HttpEntity<String> formEntity = new HttpEntity<String>(null, headers);
JSONArray responseEntity
= restTemplate.postForObject(url, formEntity, JSONArray.class);
return responseEntity;
}
feign很好的封装了这些调用细节,可以使得客户端像调用本地方法一样调用远程服务。
service-api模块
新增service-api模块,统一管理接口,用户服务和订单服务都依赖该模块。
- pom文件配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>eureka-parent</artifactId>
<groupId>com.ft</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>service-api</artifactId>
<dependencies>
<!-- 引入SpringBoot 依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.2.2</version>
</dependency>
</dependencies>
</project>
- OrderControllerApi.java
@RequestMapping("order")
@FeignClient(value = "order") // 当前类视为客户端,并指定服务别名
public interface OrderControllerApi {
/**
* 查询所有订单列表
*
* @return
* @throws Exception
*/
@PostMapping("/findOrderList")
public JSONArray findOrderList() throws Exception;
@GetMapping("/hello")
public String hello();
}
- UserControllerApi.java
@RequestMapping("user")
public interface UserControllerApi {
/**
* 查询所有订单列表
*
* @return
* @throws Exception
*/
@PostMapping("/findOrderList")
public JSONArray findOrderList() throws Exception;
@GetMapping("/hello")
public String hello();
}
订单服务
- 订单服务pom.xml依赖service-api
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>eureka-parent</artifactId>
<groupId>com.ft</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order</artifactId>
<dependencies>
<!-- 引入service-api依赖-->
<dependency>
<groupId>com.ft</groupId>
<artifactId>service-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- 订单服务OrderController实现service-api里的OrderControllerApi
@RestController
public class OrderController implements OrderControllerApi {
@Value("${server.port}")
private String port;
@Override
public JSONArray findOrderList() throws Exception {
JSONArray orderList = new JSONArray();
JSONObject o1 = new JSONObject();
o1.put("ordNo", "001");
o1.put("ordName", "鼠标");
JSONObject o2 = new JSONObject();
o2.put("ordNo", "002");
o2.put("ordName", "键盘");
JSONObject o3 = new JSONObject();
o3.put("port", port);
System.out.println("port=" + port);
orderList.add(o1);
orderList.add(o2);
orderList.add(o3);
return orderList;
}
@Autowired
public String hello() {
return "hello order!!!";
}
}
用户服务
- 用户服务pom.xml依赖service-api
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>eureka-parent</artifactId>
<groupId>com.ft</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>user</artifactId>
<dependencies>
<!-- 引入service-api依赖-->
<dependency>
<groupId>com.ft</groupId>
<artifactId>service-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
- 用户服务UserController实现service-api里的UserControllerApi
@RestController
public class UserController implements UserControllerApi {
@Autowired
private OrderControllerApi orderControllerApi;
@Override
public JSONArray findOrderList() throws Exception {
// 发起调用订单服务
JSONArray result = orderControllerApi.findOrderList();
return result;
}
@Override
public String hello() {
return "hello user!!!";
}
}
测试
postman测试结果跟之前的完全一致。
feign日志调试
用户服务使用feign调用订单服务,如果需要查看feign的调用日志,那么就需要开启相关配置。
- 用户服务application.yml增加如下配置:
logging:
level:
# 针对订单api设置日志级别
com.ft.api.controller.OrderControllerApi: debug
# 配置feign
feign:
client:
config:
# 配置服务提供方的名称
order:
loggerLevel: FULL
- postman发起调用后,查看用户服务控制台日志。
2022-02-08 21:28:40.393 DEBUG 22352 --- [nio-8001-exec-1] c.ft.api.controller.OrderControllerApi : [OrderControllerApi#findOrderList] <--- HTTP/1.1 200 (290ms)
2022-02-08 21:28:40.393 DEBUG 22352 --- [nio-8001-exec-1] c.ft.api.controller.OrderControllerApi : [OrderControllerApi#findOrderList] connection: keep-alive
2022-02-08 21:28:40.394 DEBUG 22352 --- [nio-8001-exec-1] c.ft.api.controller.OrderControllerApi : [OrderControllerApi#findOrderList] content-type: application/json
2022-02-08 21:28:40.394 DEBUG 22352 --- [nio-8001-exec-1] c.ft.api.controller.OrderControllerApi : [OrderControllerApi#findOrderList] date: Tue, 08 Feb 2022 13:28:40 GMT
2022-02-08 21:28:40.394 DEBUG 22352 --- [nio-8001-exec-1] c.ft.api.controller.OrderControllerApi : [OrderControllerApi#findOrderList] keep-alive: timeout=60
2022-02-08 21:28:40.394 DEBUG 22352 --- [nio-8001-exec-1] c.ft.api.controller.OrderControllerApi : [OrderControllerApi#findOrderList] transfer-encoding: chunked
2022-02-08 21:28:40.394 DEBUG 22352 --- [nio-8001-exec-1] c.ft.api.controller.OrderControllerApi : [OrderControllerApi#findOrderList]
2022-02-08 21:28:40.398 DEBUG 22352 --- [nio-8001-exec-1] c.ft.api.controller.OrderControllerApi : [OrderControllerApi#findOrderList] [{"ordNo":"001","ordName":"鼠标"},{"ordNo":"002","ordName":"键盘"},{"port":"8012"}]
2022-02-08 21:28:40.398 DEBUG 22352 --- [nio-8001-exec-1] c.ft.api.controller.OrderControllerApi : [OrderControllerApi#findOrderList] <--- END HTTP (87-byte body)
2022-02-08 21:28:41.239 INFO 22352 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty : Flipping property: order.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2022-02-08 21:32:18.383 INFO 22352 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration
2022-02-08 21:37:18.385 INFO 22352 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka endpoints via configuration