Skip to content

muyinchen/migo-RPC

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

此项目的开发细节 请参考以下博文:

一个轻量级分布式 RPC 框架 上

一个轻量级分布式 RPC 框架 下

示例使用

这里我们通过一个SpringbootDemo来演示如何使用:

具体代码结构请看源码

定义一个测试service接口:
package com.nia.rpc.example.service;

/**
 * Author  知秋
 * Created by Auser on 2017/2/19.
 */
public interface HelloWorld {
    String say(String hello);

    int sum(int a, int b);
    int max(Integer a, Integer b);
}
编写其实现类:
package com.nia.rpc.example.service;

/**
 * Author  知秋
 * Created by Auser on 2017/2/19.
 */
public class HelloWorldImpl implements HelloWorld {
    @Override
    public String say(String hello) {
        return "server: "+hello;
    }

    @Override
    public int sum(int a, int b) {
        return a+b;
    }

    @Override
    public int max(Integer a, Integer b) {
        return a <= b ? b : a;
    }
}
编写Springboot服务端启动类:
SpringServerConfig
package com.nia.rpc.example.server;

import com.nia.rpc.core.utils.NetUtils;
import com.nia.rpc.example.service.HelloWorld;
import com.nia.rpc.example.service.HelloWorldImpl;
import com.nia.rpc.factory.ServerFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/**
 * Author  知秋
 * Created by Auser on 2017/2/19.
 */
@SpringBootApplication
public class SpringServerConfig {
    @Bean
    public HelloWorld hello() {
        return new HelloWorldImpl();
    }

    @Bean
    public ServerFactoryBean serverFactoryBean() {
        final ServerFactoryBean serverFactoryBean = new ServerFactoryBean();
        serverFactoryBean.setPort(9090);
        serverFactoryBean.setServiceInterface(HelloWorld.class);
       //此处自定义的注册名字就相当于注解了,未来迭代的时候会加入自定义注解方式
        serverFactoryBean.setServiceName("hello");
        serverFactoryBean.setServiceImpl(hello());
        serverFactoryBean.setZkConn("127.0.0.1:2181");

        new Thread(() -> {
            try {
                serverFactoryBean.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "RpcServer").start();
        return serverFactoryBean;
    }

    public static void main(String[] args) {
       
        SpringApplication.run(SpringServerConfig.class, "--server.port=8082");
    }
}
编写服务调用端启动类:
SpringClientConfig:
package com.nia.rpc.example.client;

import com.nia.rpc.example.service.HelloWorld;
import com.nia.rpc.factory.ClientFactoryBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Author  知秋
 * Created by Auser on 2017/2/19.
 */
@Configuration
@RestController
@SpringBootApplication
@RequestMapping("/test")
public class SpringClientConfig {
    @Bean
    public HelloWorld clientFactoryBean() throws Exception {
        ClientFactoryBean<HelloWorld> clientFactoryBean = new ClientFactoryBean<>();
        clientFactoryBean.setZkConn("127.0.0.1:2181");
        clientFactoryBean.setServiceName("hello");
        clientFactoryBean.setServiceInterface(HelloWorld.class);
      return clientFactoryBean.getObject();
    }
    @Resource
    private HelloWorld helloWorld;

    @RequestMapping("/hello")
    public String hello(String say) {
        return helloWorld.say(say);
    }
    public static void main(String[] args) {
        SpringApplication.run(SpringClientConfig.class, "--server.port=8081");
    }
}

测试截图:

About

一个Java实现的轻量级的rpc小框架

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages