Dubbo简易搭建

引入Dubbo依赖(版本自定义即可)

<dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-zookeeper</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-registry-nacos</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-rpc-dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-remoting-netty4</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-serialization-hessian2</artifactId>
        </dependency>
        <!-- 该依赖为接口依赖,自定义即可 -->
        <dependency>
            <groupId>king-parent</groupId>
            <artifactId>dubbo-filter-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

Provider

  1. 添加Config类:

    @Configuration
    @EnableDubbo(scanBasePackages = "com.king.service")
    @PropertySource("classpath:/dubbo-provider.properties")
    public class ProviderConfig {
        @Bean
        public RegistryConfig registryConfig() {
            RegistryConfig registryConfig = new RegistryConfig();
            registryConfig.setAddress("zookeeper://127.0.0.1:2181");
            return registryConfig;
        }
    }
    
  2. 添加dubbo基本配置

    dubbo.application.name=service-provider
    dubbo.protocol.name=dubbo
    dubbo.protocol.port=20880
    
  3. 添加接口实现类

    @Service
    public class HelloServiceImpl implements HelloService {
        @Override
        public String sayHello(String name) {
            return "hello~" + name;
        }
    }
    
  4. 添加main函数类

    public class ProviderMain {
        public static void main(String[] args) throws IOException {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfig.class);
            context.start();
            System.in.read();
        }
    }
    

Consumer

  1. 添加consumer配置文件

    其中Qos为dubbo自带的检查dubbo服务信息和服务健康的工具,可以设置为关闭或修改端口,我下面操作是修改了默认22222的端口。以免出现和提供者端口冲突情况

    dubbo.application.name=service-consumer
    dubbo.registry.address=zookeeper://127.0.0.1:2181
    #关闭Qos
    dubbo.application.qosEnable=true
    dubbo.application.qosPort=33333
    dubbo.application.qosAcceptForeignIp=false
    
  2. 添加消费端的Config类

    @Configuration
    @ComponentScan("com.king.call")
    @PropertySource("classpath:/dubbo-consumer.properties")
    @EnableDubbo
    public class ConsumerConfig {
    }
    
  3. 添加消费端调用提供端类

    @Component
    public class ConsumerCall {
    
        @Reference
        HelloService helloService;
        public String sayHello(String name) {
            return helloService.sayHello(name);
        }
    }
    
  4. 添加consumer端的Main函数

    public class ConsumerMain {
        public static void main(String[] args) throws IOException {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfig.class);
            ConsumerCall bean = context.getBean(ConsumerCall.class);
            while (true){
                System.in.read();
                String result = bean.sayHello(" consumer ");
                System.out.println(result);
            }
        }
    }
    

Dubbo简易环境搭建就这么结束了~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值