服务接口定义:

  1. 服务接口要继承org.x3.rpc.intf.IMicroService
  2. 接口上需要添加@Service注解
  3. 接口的每个方法上都需要添加@Method注解
  4. 方法中的参数需要添加@Param注解

服务接口代码:

import org.x3.annotation.Method;
import org.x3.annotation.Param;
import org.x3.annotation.Service;
import org.x3.rpc.intf.IMicroService;

import java.util.List;

@Service
public interface HelloService extends IMicroService {
    @Method
    String printPerson(@Param(name="person")Person person);

    @Method(path = "/person")
    Person getPerson(@Param(name="name")String name);

    @Method
    List<Person> getAll();
}

服务实现类定义:

  1. 服务实现类继承MicroService<AppContext>并实现相应的服务接口

服务实现代码:


import com.google.common.collect.Maps;
import org.x3.rpc.common.MicroService;

import java.util.HashMap;
import java.util.List;

/**
 * Created by liuquan on 2017/10/27.
 */
public class HelloServiceImpl extends MicroService<AppContext> implements HelloService {
    @Override
    public String printPerson(Person person) {
        return person.toString();
    }

    @Override
    public Person getPerson(String name) {
        HashMap<String, Person> persons = Maps.newHashMap();
        persons.put("star", new Person("star", 20));
        persons.put("zhangsan", new Person("zhangsan", 21));
        persons.put("lisi", new Person("lisi", 22));
        persons.put("wangwu", new Person("wangwu", 23));
        persons.put("zhaoliu", new Person("zhaoliu", 24));
        return persons.get(name);
    }

    @Override
    public List<Person> getAll() {
        return null;
    }

注意

  • @Service注解中的name,不允许重复
  • @Service注解的类中方法名,不允许重复

results matching ""

    No results matching ""