A, led package

Create two new projects: Dubo-server and dubo-client. Pom.xml adds the following reference

<! -- dubbo dependency --> <dependency> <groupId>com.alibaba.spring. Boot </groupId> < artifactId > dubbo - spring - the boot - starter < / artifactId > < version > 2.0.0 < / version > < / dependency > < the dependency > < the groupId > org. Apache. Zookeeper < / groupId > < artifactId > zookeeper < / artifactId > < version > 3.3.3 < / version > < / dependency > < the dependency > < groupId > com. Making. Sgroschupf < / groupId > < artifactId > zkclient < / artifactId > < version > 0.1 < / version > </dependency>Copy the code

Both dubo-server and dubo-client are referenced.

Define interfaces

Interfaces must be added to both ends of dubbo-server and Dubbo-client. The package names and class names of interfaces on both ends must be the same.

package com.dubbo.api;

public interface OrderService {
    String query(a);
}

Copy the code

Application annotations at both ends

If @enableDubbo is added to both ends, dubbo is enabled.

Iv. Application. Yml

Server:

dubbo:
  application:
    name:  dubbo-server
  registry:
    address: 127.00.1:2181
    protocol: zookeeper
    check: false
  protocol:
    name: dubbo
    port: 30003
  monitor:
    protocol: register
  consumer:
    check:  false
    timeout: 3000

server:
  port: 8081

Copy the code

Client:

dubbo:
  application:
    name:  dubbo-consumer
  registry:
    address: 127.00.1:2181
    protocol: zookeeper
    check: false
  monitor:
    protocol: register
  consumer:
      check:  false
      timeout: 3000

server:
  servlet:
    context-path: /consumer
  port: 8082
Copy the code

Five, the dubbo – server

Add the @service annotation to declare it to be called remotely. Note that this is dubbo’s @service, not Spring’s @Service

import com.alibaba.dubbo.config.annotation.Service;

@Service
public class OrderServiceImpl implements OrderService {
    @Override
    public String query(a) {
        return "{\" MSG \":\" Hello, I am the server of dubbo remote call, congratulations on your success \"}"; }}Copy the code

Six, dubbo – client

Inject remote objects using @Reference

@RestController
class HelloController {

    @Reference
    OrderService mService;

    @GetMapping("/hello")
    public String hello(a) {
        returnmService.query(); }}Copy the code

Seven, start,

  1. Start the zookeeper
  2. Start the dubbo – server
  3. Start the dubbo – client
  4. Browser to localhost: 8082 / consumer/hello