Ribbon需要自己构建http请求,模拟http请求然后使用RestTemplate发送给其他服务,步骤相当繁琐。
Feign则是在Ribbon的基础上进行了一次改进采用接口的方式,将需要Feign客户端请求定义成抽象方法即可,不需要自己构建http请求。
:::info
服务中心由数据团队维护
:::
略…
:::info
生产者实例配置信息
:::
service:
eureka:
instance:
# 标注模块
tagserver:
name: ai-tagserver
context-path: /aiFactoryServer/api/aiTagserver
# 系统模块
system:
name: common-system
context-path: /aiFactoryServer/api/system
context-path2: /aiFactoryServer/system
# 资源模块
resource:
name: ai-resource
context-path: /aiFactoryServer/api/aiResource
# 超市模块
aiSupermarket:
name: ai-supermarket
context-path: /aiFactoryServer/api/aiSupermarket
# 训练模块
trainning:
name: ai-trainning
context-path: /aiFactoryServer/api/aiTraining
# cmdb模块
aiCmdb:
name: ai-cmdb
context-path: /aiFactoryServer/api/aiCmdb
# storage模块
aiStorage:
name: ai-storage
context-path: /aiFactoryServer/api/aiStorage
# gateway模块
gateway:
name: ai-gateway
context-path: /aiFactoryServer/api/aiGateway
# inference模块
inference:
name: ai-inference
context-path: /aiFactoryServer/inference
aiDevCenter:
name: ai-devcenter
context-path: /aiFactoryServer/api/aiDevcenter
:::info 消费者接口Fegin :::
package com.iwhalecloud.aiFactory.aiResource.system.service;
import com.iwhalecloud.aiFactory.common.util.ResponseUtil;
import com.iwhalecloud.aiFactory.system.service.api.dto.request.UserListRequest;
import com.iwhalecloud.aiFactory.system.service.api.dto.response.UserInfoDto;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.Map;
@ConditionalOnMissingClass("com.iwhalecloud.aiFactory.system.service.service.UserServiceImpl")
@FeignClient(
value = "${service.eureka.instance.system.name}",
path = "${service.eureka.instance.system.context-path2:''}"
)
public interface YunUserService {
/**
* 使用用户编码+密码方式获取token
* @param user
* @param artifact
* @return
*/
@GetMapping("/session/getToken")
ResponseUtil getTokenByArtifact(@RequestParam("user")String user, @RequestParam("artifact")String artifact);
/**
* 使用秘钥+用户编码方式获取token
* @param accountCode
* @param accountPwd
* @return
*/
@GetMapping("/session/getToken")
ResponseUtil getTokenByPassword(@RequestParam("accountCode")String accountCode, @RequestParam("accountPwd")String accountPwd);
/**
* 添加新用户,用户类型可为空
* @param appKey
* @param nonce
* @param timestamp
* @param sign
* @param userInfoDto
* @return
*/
@PostMapping("/user/saveUserInter")
UserInfoDto saveUserInter(@RequestHeader("x-signature-appkey")String appKey,
@RequestHeader("x-signature-nonce")String nonce,
@RequestHeader("x-signature-timestamp")String timestamp,
@RequestHeader("x-signature-value")String sign,
@RequestHeader("x-signature-sessionid")String sessionId,
@RequestBody UserInfoDto userInfoDto);
/**
* 查询所有用户类型为空的用户信息
* @param appKey
* @param nonce
* @param timestamp
* @param sign
* @param requestParams
* @return
*/
@PostMapping("/user/getAllUserTypeIsNull")
Object getAllUserTypeIsNull(@RequestHeader("x-signature-appkey")String appKey,
@RequestHeader("x-signature-nonce")String nonce,
@RequestHeader("x-signature-timestamp")String timestamp,
@RequestHeader("x-signature-value")String sign,
@RequestHeader("x-signature-sessionid")String sessionId,
@RequestBody Map<String, Object> requestParams);
/**
* 为无用户类型账号分配类型
* @param appKey
* @param nonce
* @param timestamp
* @param sign
* @param requestParams
* @return
*/
@PostMapping("/user/updateUserTypeByUserIds")
Object updateUserTypeByUserIds(@RequestHeader("x-signature-appkey")String appKey,
@RequestHeader("x-signature-nonce")String nonce,
@RequestHeader("x-signature-timestamp")String timestamp,
@RequestHeader("x-signature-value")String sign,
@RequestHeader("x-signature-sessionid")String sessionId,
@RequestBody Map<String, Object> requestParams);
/**
* 查询用户列表
* @param appKey
* @param nonce
* @param timestamp
* @param sign
* @param sessionId
* @param userListRequest
* @return
*/
@GetMapping("/user/listByUserNameOrUserCodeExt")
Object listByUserNameOrUserCodeExt(@RequestHeader("x-signature-appkey")String appKey,
@RequestHeader("x-signature-nonce")String nonce,
@RequestHeader("x-signature-timestamp")String timestamp,
@RequestHeader("x-signature-value")String sign,
@RequestHeader("x-signature-sessionid")String sessionId,
UserListRequest userListRequest);
/**
* 为多个用户添加一个角色
* @param appKey
* @param nonce
* @param timestamp
* @param sign
* @param sessionId
* @param requestParams
* @return
*/
@PostMapping("/user/addrel")
ResponseUtil batchAddRoles(@RequestHeader("x-signature-appkey")String appKey,
@RequestHeader("x-signature-nonce")String nonce,
@RequestHeader("x-signature-timestamp")String timestamp,
@RequestHeader("x-signature-value")String sign,
@RequestHeader("x-signature-sessionid")String sessionId,
@RequestBody Map<String, Object> requestParams);
}
消费者Controller直接调用Feign接口
@Autowire
YunUserService yunUserService;
private ResponseUtil getToken(String userCode, String encryptPwd) {
ResponseUtil tokenResp;
if (Boolean.valueOf(userPassword)) {
tokenResp = yunUserService.getTokenByPassword(userCode, encryptPwd);
}
else {
String yyyyMMddHHmm = DateUtil.formatInputDate(new Date(), "yyyyMMddHHmm");
String artifact = MD5Util.md5Hex(userCode + keyChain + yyyyMMddHHmm);
tokenResp = yunUserService.getTokenByArtifact(userCode, artifact);
}
return tokenResp;
}
主启动类开启@EnableFeignClients
// 开启远程调用服务
@EnableFeignClients
@SpringBootApplication
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
:::info
Feign的自定义配置
:::
Feign的自定义配置,一般情况下,默认值就能满足我们使用,如果要自定义时,只需要创建自定义的**@Bean覆盖默认Bean即可。
Feign的日志级别分为四种:
- NONE:不记录任何日志信息,默认
- BASIC:仅记录请求的方法,URL以及响应状态和执行时间
- HEADERS:在BASIC的基础上,加上请求与响应头信息
- FULL:记录所有请求和响应的明细,包括头信息、请求体、响应信息,元数据
基于配置文件修改feign的日志级别
#针对单个服务:
feign:
client:
config:
someservice: # 针对某个微服务的配置
loggerLevel: FULL # 日志级别
#针对针对所有服务
feign:
client:
config:
default: # 这里用default就是全局配置
loggerLevel: FULL # 日志级别
基于代码方式修改feign的日志级别:
全局生效放入@EnableFeignClients注解中:@EnableFeignClients(defaultConfiguration = DefaultFeignConfiguration .class)
局部生效则放入FeignClients 注解中:@FeignClient(value="userservice", configuration=DefaultFeignConfiguration .class)
import feign.Logger;
import org.springframework.context.annotation.Bean;
public class DefaultFeignConfiguration {
@Bean
public Logger.Level logLevel() {
return Logger.Level.BASIC;
}
}