@Service
public class MyService implements Service {
@Override
public String sayHello(String name) {
return "Hello, " + name + "!";
}
}
public class ServiceProvider {
public static void main(String[] args) {
ServiceConfig<HelloService> serviceConfig = new ServiceConfig<>();
serviceConfig.setInterface(HelloService.class);
serviceConfig.setRef(new HelloServiceImpl());
serviceConfig.export();
}
}
@Service
public class MyClient {
@Reference
private HelloService helloService;
public String sayHello(String name) {
return helloService.sayHello(name);
}
}
public class ServiceConsumer {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ServiceConsumer.class);
context.start();
MyClient myClient = context.getBean(MyClient.class);
String result = myClient.sayHello("John");
System.out.println(result);
context.close();
}
}
yaml
dubbo:
application:
name: service-provider
registry:
address: zookeeper://localhost:2181
server:
transport: netty4
yaml
dubbo:
protocol:
name: dubbo
port: 20880
provider:
timeout: 5000
yaml
dubbo:
consumer:
check: false
reference:
timeout: 5000