Coder Social home page Coder Social logo

slimrpc's Introduction

slimrpc


基于netty,jdk本身的动态代理.结合spring封装出一个简单高可靠性的rpc框架. 目标:一切从简,轻量级,高可维护性. 使用只需要依赖jar包,简单配置即可.

特点

  1. 简洁
  2. 高效
  3. 去中心化
  4. 双向调用
  5. 客户端/服务端双向认证

demo


1.添加依赖

<dependency>
  <groupId>io.github.lmxy1990</groupId>
  <artifactId>slimrpc-core</artifactId>
  <version>1.0.0</version>
</dependency>

2.创建接口

public interface UserService {

    String sayHello(String name) ;


    UserResult login(UserOption userOption) ;

}

3.服务端实现接口

@Component
@EnableSlimRpc
public class UserServiceImpl implements UserService {


    @Override
    public String sayHello(String name) {
        return "hello ," + name;
    }


    @Override
    public UserResult login(UserOption userOption) {

        System.out.println("用户:" + userOption.getName());
        System.out.println("年龄:" + userOption.getAge() + "正在请求登录");
        UserResult result = new UserResult();
        result.setName(userOption.getName());
        result.setLoginInfo("登录信息:" + RandomStringUtils.randomAlphabetic(20));
        return result;
    }
}

  1. 服务端,添加配置
    <!--扫描路径,也可以直接配置-->
    <context:component-scan base-package="github.slimrpc" />


    <!--开启rpc 服务-->
    <bean class="github.slimrpc.core.api.SlimRpcServer" init-method="start" destroy-method="close" >
        <property name="listenPort" value="6300" />
    </bean>
  1. 客户端,添加配置
    <!--rpc服务器配置-->
    <bean name="rpc" class="github.slimrpc.core.api.SlimRpcClient" init-method="start" destroy-method="close" >
        <property name="serverList">
            <props>
                <prop key="127.0.0.1" >6300</prop>
            </props>
        </property>
    </bean>

    <!--创建bean-->
    <bean name="userService" factory-bean="rpc" factory-method="createConsumerProxy">
        <!--接口路径-->
        <constructor-arg value="github.slimrpc.service.UserService" />
    </bean>
    
  1. 启动服务端
@ContextConfiguration({"/spring-context-server.xml"})
public class TestServer extends AbstractJUnit4SpringContextTests {


    @Test
    public void testStartServer(){

        try {
            Thread.sleep(100000000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


}

7.客户端发起请求

@ContextConfiguration({"/spring-context-client.xml"})
public class TestClient extends AbstractJUnit4SpringContextTests {

    @Autowired
    @Qualifier("userService")
    private UserService userService ;


    @Test
    public void testSayHello(){

        String mayun = userService.sayHello("mayun");

        System.out.println(mayun);

    }


    @Test
    public void testMyClassParam() {
        UserOption option  = new UserOption("zhangsan","32") ;

        UserResult login = userService.login(option);

        System.out.println(login.getLoginInfo());

    }


}

slimrpc's People

Contributors

lmxy1990 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 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.