概述
本文使用Dubbo提供的ServiceConfig、ReferenceConfig配置类演示怎样提供服务、消费服务、注册服务、调用服务等
一、接口及公共类
1. GreetingService
public interface GreetingService { String sayHello(String name); Result<String> testGeneric(PoJo poJo); }
2. GreetingService
public class PoJo { private String id; private String name; }
3. Result
public class Result<T> implements Serializable { private static final long serialVersionUID = 1L; private boolean sucess; private String msg; private T data; public T getData() { return data; } public void setData(T data) { this.data = data; } public boolean isSucess() { return sucess; } public void setSucess(boolean sucess) { this.sucess = sucess; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
二、服务提供者
1. GreetingServiceImpl
public class GreetingServiceImpl implements GreetingService { @Override public String sayHello(String name) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } return "Hello " + name + " " + RpcContext.getContext().getAttachment("company"); } @Override public Result<String> testGeneric(PoJo poJo) { Result<String> result = new Result<>(); result.setSucess(true); try { result.setData(JSON.json(poJo)); } catch (IOException e) { e.printStackTrace(); } return result; } }
2. ApiProvider
public class ApiProvider { public static void main(String[] args) throws IOException { // 1.创建ServiceConfig实例 ServiceConfig<GreetingService> serviceConfig = new ServiceConfig<GreetingService>(); // 2.设置应用程序配置 serviceConfig.setApplication(new ApplicationConfig("dubbo-provider")); // 3.设置服务注册中心信息 RegistryConfig registryConfig = new RegistryConfig("zookeeper://127.0.0.1:2181"); serviceConfig.setRegistry(registryConfig); // 4.设置接口与实现类 serviceConfig.setInterface(GreetingService.class); serviceConfig.setRef(new GreetingServiceImpl()); // 5.设置服务分组与版本 // 在Dubbo中,"服务接口+服务分组+服务版本"唯一地确定一个服务,同一个服务接口可以有不同的版本以便服务升级等, // 另外每个服务接口可以属于不同分组,所以当调用方消费服务时一定要设置正确的分组与版本 serviceConfig.setVersion("1.0.0"); serviceConfig.setGroup("dubbo"); // 6.设置线程池策略 //HashMap<String, String> parameters = new HashMap<>(); //parameters.put("threadpool", "mythreadpool"); //serviceConfig.setParameters(parameters); // 7.导出服务 serviceConfig.export(); // 8.挂起线程,避免服务停止 System.out.println("server is started"); System.in.read(); } }
三、服务消费者
1. ApiConsumer
public class ApiConsumer { public static void main(String[] args) throws InterruptedException { // 1.创建服务引用对象实例 ReferenceConfig<GreetingService> referenceConfig = new ReferenceConfig<GreetingService>(); // 2.设置应用程序信息 referenceConfig.setApplication(new ApplicationConfig("first-dubbo-consumer")); // 3.设置服务注册中心 referenceConfig.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181")); //直连测试 //referenceConfig.setUrl("dubbo://192.168.0.109:20880"); // 4.设置服务接口和超时时间 referenceConfig.setInterface(GreetingService.class); referenceConfig.setTimeout(5000); // 5.设置自定义负载均衡策略与集群容错策略 referenceConfig.setLoadbalance("myroundrobin"); referenceConfig.setCluster("myCluster"); RpcContext.getContext().set("ip", "30.10.67.231"); // 6.设置服务分组与版本 referenceConfig.setVersion("1.0.0"); referenceConfig.setGroup("dubbo"); // 7.引用服务 GreetingService greetingService = referenceConfig.get(); // 8. 设置隐式参数 // 设置隐式参数,然后服务提供者就可以在服务实现类方法里获取参数值 RpcContext.getContext().setAttachment("company", "alibaba"); // 9. 调用服务 // 同步发起远程调用,然后当前线程会被阻塞直到服务提供方把结果返回 System.out.println(greetingService.sayHello("world")); Thread.currentThread().join(); }