Git Product home page Git Product logo

hsuanwudev / nexus Goto Github PK

View Code? Open in Web Editor NEW

This project forked from stephenfaust/nexus

0.0 0.0 0.0 145 KB

Nexus是一个简单的rpc框架,基本实现rpc的主要功能,目前须结合spring使用,其中使用Netty为通信框架,使用kryo作为默认序列化协议,通过spi来选配序列化协议、 负载均衡器、注册中心;

License: Apache License 2.0

Java 100.00%

nexus's Introduction

Nexus

介绍

Nexus是一个简单的rpc框架,基本实现rpc的主要功能,目前须结合spring使用,其中使用Netty为通信框架,使用kryo作为默认序列化协议,通过spi来选配序列化协议、 负载均衡器、注册中心;

使用教程

  • 将项目拉到本地,并安装到本地maven仓库;
  • 创建一个空的springboot项目;
  • pom引入依赖;
<dependency>
  <groupId>com.mao.nexus</groupId>
  <artifactId>nexus-spring-boot-start</artifactId>
  <version>1.0.0</version>
</dependency>
  • 在yml配置相关内容
nexus:
  service-name: server-example
  port: 1234
  registry:
    protocol: consul
  cluster:
    load-balance: random
  max-connection: 20
  • 在服务端编写服务;
@NexusService
public class TestServiceImpl implements TestService {

  @Value("${nexus.port}")
  private int port;

  private final List<User> userList = new ArrayList<>();

  private Random random = new Random();

  @PostConstruct
  public void init() {

    for (int i = 0; i < 10; i++) {
      User user = new User("Test" + i, i, random.nextInt(2), port);
      userList.add(user);
    }
  }

  @Override
  public String doTest(String var) {
    return String.format("收到了,%s,我的回答是:够了!~我是%d", var, port);
  }

  @Override
  public User getUser(String name) {
    return userList.stream().filter(user -> user.getName().equals(name)).findAny().orElse(null);
  }
}
  • 客户端controller;
@RestController
public class RpcController {

  @NexusClient(serviceName = "server-example")
  private TestService testService;


  @GetMapping("/test/{par}")
  public String test2(@PathVariable String par) throws IOException {
    Random random = new Random();
    long l = random.nextLong();
    return testService.doTest(String.valueOf(l));

  }

  @GetMapping("/test3/{name}")
  public User test3(@PathVariable String name) {
    return testService.getUser(name);
  }

}
  • 启动服务端和客户端,并在浏览器调用服务接口 img.png
  • 调用成功!!!
案例

更多详细案例在nexus-example中;

nexus's People

Contributors

stephenfaust 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.