项目结构

dubbo学习笔记二(服务调用)

代码示例

由于之前的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

浏览器访问

dubbo学习笔记二(服务调用)

相关文章:

  • 2021-10-15
  • 2021-12-25
  • 2021-04-26
  • 2021-08-25
  • 2021-09-08
  • 2021-11-08
  • 2021-08-08
猜你喜欢
  • 2021-10-17
  • 2021-05-01
  • 2021-10-05
  • 2021-09-18
  • 2021-04-05
  • 2021-06-04
  • 2021-12-18
相关资源
相似解决方案