文档

SOFARPC API 服务

更新时间:
一键部署

网关提供服务端 SOFARPC Demo 代码供用户下载测试,下载 API 提供者 Demo 后,SOFARPC Demo 代码位置为:com/alipay/gateway/sofarpc/GatewayRpcServiceImpl.java。

RPC 接入配置说明(必填)

#服务实例标识,instanceId
com.alipay.instanceid=J79FOTMP8886
#acvip地址用作订阅注册中心
com.antcloud.antvip.endpoint=100.88.93.7
#操作员ak/sk
com.antcloud.mw.access=AC8imi57g6Ti****
com.antcloud.mw.secret=bL6QgJzDh1vuLFWftsWmoEBi1qPr****

#rpc服务端口
com.alipay.sofa.rpc.bolt-port=12200

发布服务

说明

服务可以通过注解的形式或者通过 XML 配置文件进行发布。

XML 配置形式

<bean id="gatewayRpcService" class="com.alipay.gateway.sofarpc.GatewayRpcServiceImpl"/>
    <sofa:service ref="gatewayRpcService" interface="com.alipay.gateway.facade.rpc.GatewayRpcService"
                  unique-id="rpc-test-1112">
        <sofa:binding.bolt/>
    </sofa:service>

注解形式

87.png

Demo 案例

@Service
@SofaService(bindings = {@SofaServiceBinding(bindingType = "bolt")}, uniqueId = "rpc-test-1112")
public class GatewayRpcServiceImpl implements GatewayRpcService {

    @Override
    public String hello() {
        return "hello noParamForString";
    }

    @Override
    public void testVoid(String param) {

    }

    @Override
    public void testException() throws Exception {

    }

    @Override
    public Map<String, String> noParamForMap() {
        System.out.println("rpc-test");
        Map<String, String> map = Maps.newHashMap();
        map.put("result", "noParamForMap");
        return map;
    }

    @Override
    public String simpleParam(String param) {
        return "hello simpleParamForString " + param;
    }

    @Override
    public Map<String, String> simpleParamForMap(String param) {
        Map<String, String> map = Maps.newHashMap();
        map.put("result", "simpleParamForMap " + param);
        return map;
    }

    @Override
    public String jsonParam(JSONObject param) {
        return "hello jsonParam";
    }

    @Override
    public Map<String, String> jsonParamForMap(JSONObject param) {
        Map<String, String> map = Maps.newHashMap();
        map.put("result", "jsonParamForMap");
        return map;
    }

    @Override
    public User jsonParamForUser(User user) {
        return user;
    }

    @Override
    public List<String> listNoParam() {
        List<String> list = new ArrayList<>();
        list.add("aaa");
        return list;
    }

    @Override
    public List<User> listSimpleParam(List<User> list) {
        return list;
    }

    @Override
    public String registryQuestionTest() throws UnknownHostException {
        return JSONObject.toJSONString(InetAddress.getLocalHost());
    }

    @Override
    public User user1(User user) {
        return user;
    }

    @Override
    public User user2(String name) {
        User user = new User(name, 20);
        return user;
    }

    @Override
    public AccountQueryResult queryAccountBalance(String accountNo) {

        AccountQueryResult accountQueryResult = new AccountQueryResult();
        Account account = new Account(accountNo, new BigDecimal(11111111), new BigDecimal(22222222), new BigDecimal(33333333));
        accountQueryResult.setSuccess(true);
        accountQueryResult.setAccount(account);
        accountQueryResult.setMsgCode("200");
        accountQueryResult.setMsgText("success");

        return accountQueryResult;
    }
}  		

  • 本页导读 (0)