1、JsonRpc概念
JSON-RPC是一种基于JSON的跨语言远程调用协议。有文本传输数据小,便于调试扩展的特点。
2、实现例子
a)项目1-api 编写rpc接口
ProductRpc.java
package com.imooc.api;
import java.util.List;
import com.googlecode.jsonrpc4j.JsonRpcService;
import com.imooc.api.domain.ProductRpcReq;
import com.imooc.entity.Product;
/**
* 产品相关的rpc服务
* @author zemel
*
*/
@JsonRpcService("rpc/products")
public interface ProductRpc {
/**
* 查询多个产品
* @param req
* @return
*/
List query(ProductRpcReq req);
/**
* 查询单个产品
* @param id
* @return
*/
Product findOne(String id);
}
package com.imooc.api.domain;
import java.math.BigDecimal;
import java.util.List;
/**
* 产品相关rpc请求对象
* @author zemel
*
*/
public class ProductRpcReq {
private List idList;
private BigDecimal minRewardRate;
private BigDecimal maxRewardRate;
private List statusList;
public List getIdList() {
return idList;
}
public void setIdList(List idList) {
this.idList = idList;
}
public BigDecimal getMinRewardRate() {
return minRewardRate;
}
public void setMinRewardRate(BigDecimal minRewardRate) {
this.minRewardRate = minRewardRate;
}
public BigDecimal getMaxRewardRate() {
return maxRewardRate;
}
public void setMaxRewardRate(BigDecimal maxRewardRate) {
this.maxRewardRate = maxRewardRate;
}
public List getStatusList() {
return statusList;
}
public void setStatusList(List statusList) {
this.statusList = statusList;
}
@Override
public String toString() {
return "ProductRpcReq [idList=" + idList + ", minRewardRate=" + minRewardRate + ", maxRewardRate="
+ maxRewardRate + ", statusList=" + statusList + "]";
}
}
b)项目2-manager(rpc实现工程)
ProductRpcImpl.java
package com.imooc.rpc;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
import com.imooc.api.ProductRpc;
import com.imooc.api.domain.ProductRpcReq;
import com.imooc.entity.Product;
import com.imooc.service.ProductService;
/**
* rpc服务实现类
* @author zemel
*
*/
@AutoJsonRpcServiceImpl
@Service
public class ProductRpcImpl implements ProductRpc{
private static Logger log = LoggerFactory.getLogger(ProductRpcImpl.class);
@Autowired
private ProductService productService;
@Override
public List query(ProductRpcReq req) {
log.info("查询多个产品,请求:{}", req);
Pageable pageable = new PageRequest(0, 1, Sort.Direction.DESC, "rewardRate");
Page page = productService.query(req.getIdList(), req.getMinRewardRate(),
req.getMaxRewardRate(), req.getStatusList(), pageable);
log.info("查询多个产品,结果:{}", page);
return page.getContent();
}
@Override
public Product findOne(String id) {
log.info("查询产品详情,请求:{}", id);
Product result = productService.findOne(id);
log.info("查询产品详情,结果:{}", result);
return result;
}
}
RpcConfig.java
package com.imooc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImplExporter;
/**
* rpc相关配置
* @author zemel
*
*/
@Configuration
public class RpcConfig {
@Bean
public AutoJsonRpcServiceImplExporter rpcServiceImplExporter(){
return new AutoJsonRpcServiceImplExporter();
}
}
#服务器
server.port=80
server.servlet.contextPath=/manager
# DataSource
spring.datasource.url=jdbc:mysql://localhost/manager?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#jackson自定义日期格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
# JPA
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
# 配置swagger
swagger.groupName=manager
swagger.basePackage=com.imooc.controller
c)rpc客户端-saller
ProductRpcService.java
package com.imooc.seller.service;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import com.imooc.api.ProductRpc;
import com.imooc.api.domain.ProductRpcReq;
import com.imooc.entity.Product;
import com.imooc.entity.enums.ProductStatus;
/**
* 产品服务
* @author zemel
*
*/
@Service
public class ProductRpcService {
private static Logger log = LoggerFactory.getLogger(ProductRpcService.class);
@Autowired
private ProductRpc productRpc;
/**
* 查询全部产品
* @return
*/
public List findAll(){
ProductRpcReq req = new ProductRpcReq();
List status = new ArrayList<>();
// status.add(ProductStatus.IN_SELL.getCode());
// req.setStatusList(status);
log.info("rpc查询全部产品,请求:{}", req);
List result = productRpc.query(req);
log.info("rpc查询全部产品,结果:{}", result);
return result;
}
@PostConstruct
public void testFindAll(){
findAll();
}
}
RpcConfig.java
package com.imooc.seller.config;
import java.net.MalformedURLException;
import java.net.URL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcClientProxyCreator;
import com.imooc.api.ProductRpc;
/**
* rpc 相关配置
* @author zemel
*
*/
@Configuration
@ComponentScan(basePackageClasses={ProductRpc.class})
public class RpcConfig {
private static Logger log = LoggerFactory.getLogger(RpcConfig.class);
@Bean
public AutoJsonRpcClientProxyCreator rpcClientProxyCreator(@Value("${rpc.manager.url}") String url){
AutoJsonRpcClientProxyCreator creator = new AutoJsonRpcClientProxyCreator();
try {
creator.setBaseUrl(new URL(url));
} catch (MalformedURLException e) {
log.error("创建rpc服务地址错误", e);
e.printStackTrace();
}
creator.setScanPackage(ProductRpc.class.getPackage().getName());//扫描rpc所在包
return creator;
}
}
application.properties
server.servlet.contextPath=/seller
server.port=8082
rpc.manager.url=http://localhost:80/manager/
# DataSource
spring.datasource.url=jdbc:mysql://localhost/manager?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.driver-class-name=com.mysql.jdbc.Driver