01. 基于注解的Dubbo Demo

1. 创建一个空的maven项目

4.0.0com.dxhdemo-basepom1.0-SNAPSHOT2.7.5org.apache.dubbodubbo${dubbo.version}org.apache.dubbodubbo-common${dubbo.version}org.apache.dubbodubbo-registry-zookeeper${dubbo.version}org.apache.dubbodubbo-registry-nacos${dubbo.version}org.apache.dubbodubbo-rpc-dubbo${dubbo.version}org.apache.dubbodubbo-remoting-netty4${dubbo.version}org.apache.dubbodubbo-serialization-hessian2${dubbo.version}log4jlog4j1.2.16org.slf4jslf4j-api1.7.5org.slf4jslf4j-log4j121.7.5com.alibabafastjson1.2.62org.apache.maven.pluginsmaven-compiler-plugin1111

2. 创建module —— service-api

新建接口:

package com.dxh.service;

public interface HelloService {
    String sayHello(String name);
}

3. 创建module —— service-provider

创建服务提供者模块

3.1 引入相关依赖

pom中引入 service-api

pom中引入dubbo

com.dxhservice-api1.0-SNAPSHOTorg.apache.dubbodubboorg.apache.dubbodubbo-registry-zookeeperorg.apache.dubbodubbo-rpc-dubboorg.apache.dubbodubbo-remoting-netty4org.apache.dubbodubbo-serialization-hessian2

3.2 实现service-api中的接口

编写实现类实现 HelloService,注意@Service注解使用dubbo的注解

package com.dxh.service.impl;

import com.dxh.service.HelloService;
import org.apache.dubbo.config.annotation.Service;

//这里的注解使用dubbo的@Service的注解
@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String sayHello(String name) {
        return "Hello: "+ name;
    }
}

3.3 新建dubbo配置文件

resource中新建配置文件——dubbo-provider.properties

dubbo.application.name=service-provider
dubbo.protocol.name=dubbo
# 端口随便写
dubbo.protocol.port=20880

3.4 编写配置类以及启动类

我们使用静态内部类的方法编写配置类

package com.dxh;

import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;

public class DubboPureMain {
    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
        context.start();
        // 避免直接关闭
        System.in.read();
    }

    @Configuration
    @EnableDubbo(scanBasePackages = "com.dxh.service.impl")
    @PropertySource("classpath:/dubbo-provider.properties")
    static class ProviderConfiguration{
        /**
         * 可以在配置文件中填写 dubbo.registry.address=zookeeper://39.100.230.98:2181
         * 这样的话 这个bean就可以不写了
         */
        @Bean
        public RegistryConfig registryConfig(){
            RegistryConfig registryConfig = new RegistryConfig();
            registryConfig.setAddress("zookeeper://39.100.230.98:2181");
            return registryConfig;
        }
    }
}

4. 创建module —— service-consumer

创建服务消费者模块

4.1 引入相关依赖

pom中引入 service-api

pom中引入dubbo

com.dxhservice-api1.0-SNAPSHOTorg.apache.dubbodubboorg.apache.dubbodubbo-registry-zookeeperorg.apache.dubbodubbo-rpc-dubboorg.apache.dubbodubbo-remoting-netty4org.apache.dubbodubbo-serialization-hessian2

4.2 创建消费者组件

注意@Reference使用dubbo的注解

package com.dxh.bean;

import com.dxh.service.HelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Component;

@Component
public class ConsumerComponet {
    //注意使用dubbo的注解
    @Reference
    private HelloService helloService;

    public String sayHello(String name){
        return helloService.sayHello(name);
    }
}

4.3 新建dubbo配置文件

resource中新建配置文件——dubbo-consumer.properties

dubbo.application.name=service-consumer
dubbo.registry.address=zookeeper://39.100.230.98:2181

4.4 编写配置类以及启动类

package com.dxh;

import com.dxh.bean.ConsumerComponet;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;

public class AnnotationConsumerMain {
    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "com.dxh.bean")
    @EnableDubbo
    static class ConsumerConfiguration{

    }

    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        //获取消费者组件
        ConsumerComponet service = context.getBean(ConsumerComponet.class);
        while (true){
            System.in.read();
            String hello = service.sayHello("world");
            System.out.println("result : "+hello);
        }
    }
}

5. 启动测试效果

  • 启动服务提供者
  • 启动服务消费者

【Dubbo】01. 基于注解的Dubbo Demo_Dubbo