项目结构
代码示例
由于之前的IEchoService 的一个方法只是在服务端控制台打印,不便在浏览器测试,所以新添加的方法
api和服务端代码变更
public interface IEchoService {
void echo();
String echo(String msg);
}
@Service
public class EchoServiceImpl implements IEchoService {
public void echo() {
System.out.printf("hello");
}
public String echo(String msg) {
return "echo: " + msg;
}
}
客户端代码
- [ClientService]
@Service
public class ClientService {
@Reference
private IEchoService echoService;
public String echo(String msg) {
return echoService.echo(msg);
}
}
- [EchoTestApp]
@RestController
@SpringBootApplication
public class EchoTestApp {
@Autowired
private ClientService clientService;
@GetMapping("/hi/{name}")
public String hello(@PathVariable(name = "name") String name) {
return clientService.echo(name);
}
public static void main(String[] args) {
System.getProperties().put("server.port", 7070);
SpringApplication.run(EchoTestApp.class, args);
}
@Configuration
@EnableDubbo(scanBasePackages = "consumer")
@PropertySource("classpath:/dubbo-consumer.properties")
static public class ConsumerConfiguration {
}
}
- [dubbo-consumer.properties]
# dubbo-consumer.properties
dubbo.application.name=annotation-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.consumer.timeout=3000