1.导入maven依赖:
com.github.briandilley.jsonrpc4j
jsonrpc4j
1.5.3
2.jsonRpc接口开发:
请求参数实体类:
/**
* jsonRpc基本参数封装
*/
@Data
public class ProductRpcReq {
private List idList;
private BigDecimal minRewardRate;
private BigDecimal maxRewardRate;
private List statusList;
//jsonRPC不能接受复杂参数
//private Pageable pageable;
private Integer pageNum;
private Integer pageSize;
private Sort.Direction sort;
private String orderBy;
}
@JsonRpcService("jsonRpc/products") 这里注意,不能以 /开头,后面修改源码解决这个问题
public interface IProductRpcService {
/**
* 根据请求参数,获取请求内容
* @param req
* @return
*/
List findProduct(ProductRpcReq req);
/**
* 根据id获取产品
* @param id
* @return
*/
Product findProductById(String id);
}
3.jsonRpc服务端代码:
@AutoJsonRpcServiceImpl
@Service("productRpcService")
public class ProductRpcService implements IProductRpcService {
private static Logger logger = LoggerFactory.getLogger(ProductRpcService.class);
@Autowired
private ProductService productService;
@Override
public List findProduct(ProductRpcReq req) {
logger.info("findProduct rpc请求参数:{}",req);
Pageable pageable = new PageRequest(req.getPageNum(),req.getPageSize(),req.getSort(),req.getOrderBy());
Page products = productService.queryPage(req.getIdList(), req.getMinRewardRate(), req.getMaxRewardRate(), req.getStatusList(), pageable);
logger.info("findProduct rpc结果:{}",products);
return products.getContent();
}
@Override
public Product findProductById(String id) {
logger.info("findProductById rpc请求参数:{}",id);
Product product = productService.getOneProduct(id);
logger.info("findProduct rpc结果:{}",product);
return product;
}
}
@Service("sellerProductRpcService")
public class SellerProductRpcService {
private static final Logger LOGGER = LoggerFactory.getLogger(SellerProductRpcService.class);
@Autowired
private IProductRpcService productRpcService;
/**
* 获取所有产品
* @return
*/
public List findAllProduct(){
ProductRpcReq req = new ProductRpcReq();
req.setPageNum(0);
req.setPageSize(1000);
req.setSort(Sort.Direction.DESC);
req.setOrderBy("rewardRate");
req.setStatusList(Arrays.asList(ProductStatus.IN_SELL.name()));
LOGGER.info("seller findAllProduct 请求参数:{}",req);
List products = productRpcService.findProduct(req);
LOGGER.info("seller findAllProduct 查询结果:{}",products);
return products;
}
/**
* 根据id获取产品
* @param id
* @return
*/
public Product getProductById(String id){
LOGGER.info("seller getProductById 请求参数:{}",id);
Product product = productRpcService.findProductById(id);
LOGGER.info("seller getProductById 查询结果:{}",product);
return product;
}
// @PostConstruct
// public void test(){
// getProductById("001");//测试
// }
}
公共模块,公共配置类,JSONRpcConfiguration
@Configuration
public class JSONRpcConfiguration {
/**
* jsonRpc服务端配置
* @return
*/
@Bean
public AutoJsonRpcServiceImplExporter serviceImplExporter(){
return new AutoJsonRpcServiceImplExporter();
}
/**
* 配置jsonRpc客户端
* @param url
* @return
*/
@Bean
@ConditionalOnProperty(value = {"rpc.client.url","rpc.client.basePackage"})
public AutoJsonRpcClientProxyCreator rpcClientProxyCreator(@Value("${rpc.client.url}") String url,@Value("${rpc.client.basePackage}") String basePackege){
AutoJsonRpcClientProxyCreator jsonRpcClientProxyCreator = new AutoJsonRpcClientProxyCreator();
try {
jsonRpcClientProxyCreator.setBaseUrl(new URL(url));
} catch (MalformedURLException e) {
e.printStackTrace();
}
jsonRpcClientProxyCreator.setScanPackage(basePackege);
return jsonRpcClientProxyCreator;
}
}
基于springBoot的自动装配,在resources文件下META-INF/spring.factories中,写入如下内容:
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.chukun.utils.rpcConfig.JSONRpcConfiguration
客户端配置的yml文件:
rpc:
client:
url: http://localhost:8081/manager/ 这里注意,必须以 /结尾,后面修改源码解决这个问题
basePackage: com.chukun.api.rpc
至此,jsonRpc基本环境搭建完毕。。。。