天天看點

技術幹貨|Dubbo服務調用和注冊中心功能應用

作者:運維木子李

#頭号有新人#

Dubbo提供了服務注冊中心,可以将服務提供者的資訊注冊到注冊中心,消費者可以通過注冊中心來發現可用的服務。

Java項目示例

下面是一個簡單的Java項目示例,展示了Dubbo在服務調用和注冊中心使用上的應用:

定義服務接口:

public interface UserService {
    User getUserInfo(String userId);
}           

實作服務接口:

public class UserServiceImpl implements UserService {
    public User getUserInfo(String userId) {
        // 從資料庫或其他地方擷取使用者資訊
        User user = new User();
        user.setUserId(userId);
        user.setName("John Doe");
        return user;
    }
}           

配置Dubbo服務提供者:

public class ProviderApp {
    public static void main(String[] args) throws IOException {
        // 建立服務執行個體
        UserService userService = new UserServiceImpl();

        // 配置Dubbo服務
        ServiceConfig<UserService> serviceConfig = new ServiceConfig<>();
        serviceConfig.setInterface(UserService.class);
        serviceConfig.setRef(userService);

        // 導出服務
        serviceConfig.export();

        // 阻塞主線程,保持服務運作
        System.in.read();
    }
}           

配置Dubbo服務消費者:

public class ConsumerApp {
    public static void main(String[] args) {
        // 配置Dubbo服務引用
        ReferenceConfig<UserService> referenceConfig = new ReferenceConfig<>();
        referenceConfig.setInterface(UserService.class);

        // 擷取服務代理
        UserService userService = referenceConfig.get();

        // 調用遠端服務
        User user = userService.getUserInfo("user123");

        // 處理傳回結果
        System.out.println(user);
    }
}           

以上代碼示例中,服務提供者通過Dubbo的ServiceConfig将服務接口和實作暴露為Dubbo服務,而服務消費者通過Dubbo的ReferenceConfig引用服務接口,并調用遠端服務。服務提供者和消費者可以通過Dubbo的配置檔案配置注冊中心、負載均衡政策等。

應用案例:

使用Dubbo進行服務調用:

// 服務接口
public interface HelloService {
    String sayHello(String name);
}

// 服務提供者
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

// 服務消費者
public class Consumer {
    public static void main(String[] args) {
        // 引用遠端服務
        ApplicationConfig application = new ApplicationConfig();
        application.setName("consumer");
        
        ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
        reference.setApplication(application);
        reference.setInterface(HelloService.class);
        reference.setUrl("dubbo://localhost:20880/com.example.HelloService");
        
        // 服務調用
        HelloService helloService = reference.get();
        String result = helloService.sayHello("world");
        System.out.println(result);
    }
}           

使用Dubbo進行服務注冊:

// 服務接口
public interface HelloService {
    String sayHello(String name);
}

// 服務提供者
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

// 服務注冊中心
public class Registry {
    public static void main(String[] args) {
        // 啟動注冊中心
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        registryConfig.setProtocol("zookeeper");
        
        // 注冊服務
        ServiceConfig<HelloService> service = new ServiceConfig<HelloService>();
        service.setRegistry(registryConfig);
        service.setInterface(HelloService.class);
        service.setRef(new HelloServiceImpl());
        service.export();
    }
}

// 服務消費者
public class Consumer {
    public static void main(String[] args) {
        // 引用遠端服務
        ApplicationConfig application = new ApplicationConfig();
        application.setName("consumer");
        
        ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
        reference.setApplication(application);
        reference.setInterface(HelloService.class);
        reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        
        // 服務調用
        HelloService helloService = reference.get();
        String result = helloService.sayHello("world");
        System.out.println(result);
    }
}           

使用Dubbo進行服務調用和注冊中心的負載均衡:

// 服務接口
public interface HelloService {
    String sayHello(String name);
}

// 服務提供者1
public class HelloServiceImpl1 implements HelloService {
    public String sayHello(String name) {
        return "Hello, " + name + " from Provider 1";
    }
}

// 服務提供者2
public class HelloServiceImpl2 implements HelloService {
    public String sayHello(String name) {
        return "Hello, " + name + " from Provider 2";
    }
}

// 服務注冊中心
public class Registry {
    public static void main(String[] args) {
        // 啟動注冊中心
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        registryConfig.setProtocol("zookeeper");
        
        // 注冊服務提供者1
        ServiceConfig<HelloService> service1 = new ServiceConfig<HelloService>();
        service1.setRegistry(registryConfig);
        service1.setInterface(HelloService.class);
        service1.setRef(new HelloServiceImpl1());
        service1.export();
        
        // 注冊服務提供者2
        ServiceConfig<HelloService> service2 = new ServiceConfig<HelloService>();
        service2.setRegistry(registryConfig);
        service2.setInterface(HelloService.class);
        service2.setRef(new HelloServiceImpl2());
        service2.export();
    }
}

// 服務消費者
public class Consumer {
    public static void main(String[] args) {
        // 引用遠端服務
        ApplicationConfig application = new ApplicationConfig();
        application.setName("consumer");
        
        ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
        reference.setApplication(application);
        reference.setInterface(HelloService.class);
        reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        reference.setLoadbalance("random");
        
        // 服務調用
        HelloService helloService = reference.get();
        String result = helloService.sayHello("world");
        System.out.println(result);
    }
}           

使用Dubbo進行服務調用和注冊中心的高可用:

// 服務接口
public interface HelloService {
    String sayHello(String name);
}

// 服務提供者
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

// 服務注冊中心
public class Registry {
    public static void main(String[] args) {
        // 啟動注冊中心
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        registryConfig.setProtocol("zookeeper");
        
        // 注冊服務
        ServiceConfig<HelloService> service = new ServiceConfig<HelloService>();
        service.setRegistry(registryConfig);
        service.setInterface(HelloService.class);
        service.setRef(new HelloServiceImpl());
        service.export();
    }
}

// 服務消費者
public class Consumer {
    public static void main(String[] args) {
        // 引用遠端服務
        ApplicationConfig application = new ApplicationConfig();
        application.setName("consumer");
        
        ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
        reference.setApplication(application);
        reference.setInterface(HelloService.class);
        reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        reference.setFailover(2);
        
        // 服務調用
        HelloService helloService = reference.get();
        String result = helloService.sayHello("world");
        System.out.println(result);
    }
}           

使用Dubbo進行服務調用和注冊中心的多協定支援:

// 服務接口
public interface HelloService {
    String sayHello(String name);
}

// 服務提供者
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

// 服務注冊中心
public class Registry {
    public static void main(String[] args) {
        // 啟動注冊中心
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        registryConfig.setProtocol("zookeeper");
        
        // 注冊服務
        ServiceConfig<HelloService> service = new ServiceConfig<HelloService>();
        service.setRegistry(registryConfig);
        service.setInterface(HelloService.class);
        service.setRef(new HelloServiceImpl());
        service.setProtocols(Arrays.asList(
            new ProtocolConfig("dubbo").setPort(20880),
            new ProtocolConfig("rest").setPort(8080)
        ));
        service.export();
    }
}

// 服務消費者
public class Consumer {
    public static void main(String[] args) {
        // 引用遠端服務
        ApplicationConfig application = new ApplicationConfig();
        application.setName("consumer");
        
        ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
        reference.setApplication(application);
        reference.setInterface(HelloService.class);
        reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        reference.setProtocol("dubbo");
        
        // 服務調用
        HelloService helloService = reference.get();
        String result = helloService.sayHello("world");
        System.out.println(result);
    }
}           

使用Dubbo進行服務調用和注冊中心的動态配置:

// 服務接口
public interface HelloService {
    String sayHello(String name);
}

// 服務提供者
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

// 服務注冊中心
public class Registry {
    public static void main(String[] args) {
        // 啟動注冊中心
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        registryConfig.setProtocol("zookeeper");
        
        // 注冊服務
        ServiceConfig<HelloService> service = new ServiceConfig<HelloService>();
        service.setRegistry(registryConfig);
        service.setInterface(HelloService.class);
        service.setRef(new HelloServiceImpl());
        service.export();
    }
}

// 服務消費者
public class Consumer {
    public static void main(String[] args) {
        // 引用遠端服務
        ApplicationConfig application = new ApplicationConfig();
        application.setName("consumer");
        
        ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
        reference.setApplication(application);
        reference.setInterface(HelloService.class);
        reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        reference.setParameters(Collections.singletonMap("timeout", "5000"));
        
        // 服務調用
        HelloService helloService = reference.get();
        String result = helloService.sayHello("world");
        System.out.println(result);
    }
}           

使用Dubbo進行服務調用和注冊中心的配置優化:

// 服務接口
public interface HelloService {
    String sayHello(String name);
}

// 服務提供者
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

// 服務注冊中心
public class Registry {
    public static void main(String[] args) {
        // 啟動注冊中心
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        registryConfig.setProtocol("zookeeper");
        
        // 注冊服務
        ServiceConfig<HelloService> service = new ServiceConfig<HelloService>();
        service.setRegistry(registryConfig);
        service.setInterface(HelloService.class);
        service.setRef(new HelloServiceImpl());
        service.export();
    }
}

// 服務消費者
public class Consumer {
    public static void main(String[] args) {
        // 引用遠端服務
        ApplicationConfig application = new ApplicationConfig();
        application.setName("consumer");
        
        ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
        reference.setApplication(application);
        reference.setInterface(HelloService.class);
        reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        reference.setConsumer(new ConsumerConfig().setCheck(false));
        
        // 服務調用
        HelloService helloService = reference.get();
        String result = helloService.sayHello("world");
        System.out.println(result);
    }
}           

使用Dubbo進行服務調用和注冊中心的監控:

// 服務接口
public interface HelloService {
    String sayHello(String name);
}

// 服務提供者
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

// 服務注冊中心
public class Registry {
    public static void main(String[] args) {
        // 啟動注冊中心
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        registryConfig.setProtocol("zookeeper");
        
        // 注冊服務
        ServiceConfig<HelloService> service = new ServiceConfig<HelloService>();
        service.setRegistry(registryConfig);
        service.setInterface(HelloService.class);
        service.setRef(new HelloServiceImpl());
        service.export();
    }
}

// 服務消費者
public class Consumer {
    public static void main(String[] args) {
        // 引用遠端服務
        ApplicationConfig application = new ApplicationConfig();
        application.setName("consumer");
        
        ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
        reference.setApplication(application);
        reference.setInterface(HelloService.class);
        reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        reference.setMonitor(new MonitorConfig("monitor://127.0.0.1:8080"));
        
        // 服務調用
        HelloService helloService = reference.get();
        String result = helloService.sayHello("world");
        System.out.println(result);
    }
}           

使用Dubbo進行服務調用和注冊中心的叢集容錯:

// 服務接口
public interface HelloService {
    String sayHello(String name);
}

// 服務提供者
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

// 服務注冊中心
public class Registry {
    public static void main(String[] args) {
        // 啟動注冊中心
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        registryConfig.setProtocol("zookeeper");
        
        // 注冊服務
        ServiceConfig<HelloService> service = new ServiceConfig<HelloService>();
        service.setRegistry(registryConfig);
        service.setInterface(HelloService.class);
        service.setRef(new HelloServiceImpl());
        service.export();
    }
}

// 服務消費者
public class Consumer {
    public static void main(String[] args) {
        // 引用遠端服務
        ApplicationConfig application = new ApplicationConfig();
        application.setName("consumer");
        
        ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
        reference.setApplication(application);
        reference.setInterface(HelloService.class);
        reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        reference.setCluster("failfast");
        
        // 服務調用
        HelloService helloService = reference.get();
        String result = helloService.sayHello("world");
        System.out.println(result);
    }
}
           

使用Dubbo進行服務調用和注冊中心的服務降級:

// 服務接口
public interface HelloService {
    String sayHello(String name);
}

// 服務提供者
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name) {
        throw new RuntimeException("Service is not available");
    }
}

// 服務注冊中心
public class Registry {
    public static void main(String[] args) {
        // 啟動注冊中心
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        registryConfig.setProtocol("zookeeper");
        
        // 注冊服務
        ServiceConfig<HelloService> service = new ServiceConfig<HelloService>();
        service.setRegistry(registryConfig);
        service.setInterface(HelloService.class);
        service.setRef(new HelloServiceImpl());
        service.export();
    }
}

// 服務消費者
public class Consumer {
    public static void main(String[] args) {
        // 引用遠端服務
        ApplicationConfig application = new ApplicationConfig();
        application.setName("consumer");
        
        ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
        reference.setApplication(application);
        reference.setInterface(HelloService.class);
        reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
        reference.setMock(true);
        
        // 服務調用
        HelloService helloService = reference.get();
        String result = helloService.sayHello("world");
        System.out.println(result);
    }
}           

以上是10個使用Dubbo在服務調用和注冊中心上的應用的詳細完整項目實戰案例,每個案例都包含了詳細的Java代碼實作,并進行了詳細的注釋。

繼續閱讀