Giter Club home page Giter Club logo

springboot-jsonrpc's Introduction

springboot-jsonrpc

本项目是基于SpringBoot和Json-Rpc(jsonrpc4j.jar)的测试案例。

JSON-RPC是一个无状态且轻量级的远程过程调用(RPC)协议。 本规范主要定义了一些数据结构及其相关的处理规则。它允许运行在基于socket,http等诸多不同消息传输环境的同一进程中。其使用JSON(RFC 4627)作为数据格式。

更多可以访问JSON-RPC 2.0 规范,理解相关概念和规范格式。

jsonrpc4j的调用格式

1、RPC调用接口

import com.googlecode.jsonrpc4j.JsonRpcService;
import com.plf.jsonrpc.entity.Book;

@JsonRpcService("rpc/books")
public interface BookRpcService {
   public Book findById(String id);
}

2、配置服务端

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImplExporter;


@Configuration
public class RpcConfiguration {

   @Bean
   public AutoJsonRpcServiceImplExporter rpcServiceImplExporter(){
       return new AutoJsonRpcServiceImplExporter();
   }
}

3、配置客户端

import java.net.URL;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.googlecode.jsonrpc4j.spring.AutoJsonRpcClientProxyCreator;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class RpcConfiguration {

   @Bean
   @ConditionalOnProperty(value={"rpc.client.url","rpc.client.basePackage"})
   public AutoJsonRpcClientProxyCreator rpcClientProxyCreator(@Value("${rpc.client.url}") String url,
   @Value("${rpc.client.basePackage}") String basePackage){
       log.info("加载rpc配置,url:{},basePackage:{}",url,basePackage);
   	AutoJsonRpcClientProxyCreator clientProxyCreator = new AutoJsonRpcClientProxyCreator();
       try{
           clientProxyCreator.setBaseUrl(new URL(url));
       }catch (Exception e){
       	log.error("创建rpc服务地址错误",e.getMessage());
       }

       clientProxyCreator.setScanPackage(basePackage);
     
       return clientProxyCreator;
   }
}

4、调用

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.plf.jsonrpc.entity.Book;
import com.plf.jsonrpc.rpc.service.BookRpcService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class BookConsumerService {

	@Autowired
	private BookRpcService bookRpcService;
	
	public Book getBook(String id) {
		log.info("访问消费端Service,参数:{}",id);
		return bookRpcService.findById(id);
	}
}

springboot-jsonrpc's People

Contributors

panlf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.