bug01:在其他模块调用找不到bean时,在api模块
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.hmall.api.client.UserClient\
4.1.1.引入依赖
在cart-service
服务的pom.xml中引入OpenFeign
的依赖和loadBalancer
依赖:
<!--openFeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--负载均衡器-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
4.1.2.启用OpenFeign
接下来,我们在cart-service
的CartApplication
启动类上添加注解,启动OpenFeign功能:
4.1.3.编写OpenFeign客户端
@FeignClient(value = "user-service", configuration = UserClientFallbackFactory.class)
public interface UserClient {
@PutMapping("/users/money/deduct")
void deductMoney(@RequestParam("pw") String pw,@RequestParam("amount") Integer amount);
@PutMapping("/users/lock")
void lockUser(@RequestBody LockUserDTO dto);
}
1.写一个client
@FeignClient(value = "user-service", configuration = UserClientFallbackFactory.class)
public interface UserClient {
@PutMapping("/users/money/deduct")
void deductMoney(@RequestParam("pw") String pw,@RequestParam("amount") Integer amount);
@PutMapping("/users/lock")
void lockUser(@RequestBody LockUserDTO dto);
}
2.写对应的fallback
public class UserClientFallbackFactory implements FallbackFactory<UserClient> {
@Override
public UserClient create(Throwable cause) {
return new UserClient() {
@Override
public void deductMoney(String pw, Integer amount) {
log.error("扣减余额异常,异常原因:", cause);
throw new RuntimeException(cause);
}
@Override
public void lockUser(LockUserDTO dto) {
}
public List<RoleDTO> queryUserRoles(Long userId) {
log.error("解锁用户异常,异常原因:", cause);
return Collections.emptyList();
}
};
}
}
3.
标签:调用,pw,OpenFeign,void,amount,UserClient,public From: https://www.cnblogs.com/dragon-proud-sky/p/18312003