Code Monkey home page Code Monkey logo

hermes-java's Introduction

Hermes - Java

Version License

Overview

  • 基于spring-boot、Hermes中间件Java端SDK,需要JDK8及以上,2.3.0及以上版本需要hermes注册中心在2.1.0版本及以上
  • Hermes (https://github.com/cwdtom/hermes)

Configuration

  • application中添加新配置
    # 自身服务ID
    spring.application.name=client
    # 注册中心地址,多个地址以逗号隔开
    hermes.center-zone=127.0.0.1:8080

Usage

  1. 启用hermes

    • 添加maven依赖
    <dependencies>
        <dependency>
            <groupId>com.github.cwdtom</groupId>
            <artifactId>hermes</artifactId>
            <version>2.3.0</version>
        </dependency>
    </dependencies>
    • 添加@EnableHermes注解
    @SpringBootApplication
    @EnableHermes
    public class ApplicationMain {
        public static void main(String[] args) {
            // 启动Spring Boot项目的唯一入口
            SpringApplication.run(ApplicationMain.class, args);
        }
    }
  2. 调用远程方法

    /**
     * 测试调用
     */
    @RestController
    @RequestMapping(method = RequestMethod.GET)
    public class MainController {
        @Resource
        private TestService testService;
    
        @RequestMapping("/callAdd")
        public Integer callAdd(Integer num) {
            return testService.add(num);
        }
    
        @RequestMapping("/callSub")
        public Integer callSub(Integer num) {
            return testService.sub(num);
        }
    
        @RequestMapping("/callMul")
        public Integer callAdd(Integer num, Integer mul) {
            return testService.mul(num, mul);
        }
    }
    /**
     * 定义调用接口
     */
    @HermesClient(value = "client", fallback = FallBackServiceImpl.class)
    public interface TestService {
        @HermesMapping("testAdd")
        Integer add(@HermesParam("num") Integer num);
    
        @HermesMapping("testSub")
        Integer sub(Integer num);
    
        @HermesMapping("testMul")
        Integer mul(@HermesParam("num") Integer num, @HermesParam("mul") Integer mul);
    }
    /**
     * 失败回调
     */
    public class FallBackServiceImpl implements Service {
        @Override
        public Integer add(Integer num) {
            return -1;
        }
    
        @Override
        public Integer sub(Integer num) {
            return -1;
        }
    
        @Override
        public Integer mul(Integer num, Integer mul) {
            return -1;
        }
    
        @Override
        public Entity returnObject() {
            return null;
        }
    }
  3. 创建远程调用方法

    @HermesService
    public class TestServer {   
        @HermesMapping("testAdd")
        public Integer testAdd(@HermesParam("num") Integer num) {
            return num + 1;
        }
    
        @HermesMapping("testSub")
        public Integer testSub(String input) {
            return Integer.parseInt(input) - 1;
        }
    
        @HermesMapping("testMul")
        public Integer testMul(@HermesParam("num") Integer num, @HermesParam("mul") Integer mul) {
            return num * mul;
        }
    }

hermes-java's People

Contributors

cwdtom avatar

Stargazers

 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.