dubbo的简单搭建

dubbo的简单搭建,第1张

dubbo的简单搭建

1.父工程pom中指定依赖以及版本



    4.0.0

    org.example
    demo-base
    pom
    1.0-SNAPSHOT
    
        service-api
        service-provider
        service-consumer
    


    
        2.7.5
    

    
        
            
                org.apache.dubbo
                dubbo
                ${dubbo.version}
            
            
                org.apache.dubbo
                dubbo-common
                ${dubbo.version}
            
            
                org.apache.dubbo
                dubbo-registry-zookeeper
                ${dubbo.version}
                
                    
                        org.apache.dubbo
                        dubbo-remoting-api
                    
                    
                        org.apache.dubbo
                        dubbo-common
                    
                
            
            
                org.apache.dubbo
                dubbo-registry-nacos
                ${dubbo.version}
            
            
                org.apache.dubbo
                dubbo-rpc-dubbo
                ${dubbo.version}
                
                    
                        org.apache.dubbo
                        dubbo-remoting-api
                    
                    
                        org.apache.dubbo
                        dubbo-common
                    
                
            
            
                org.apache.dubbo
                dubbo-remoting-netty4
                ${dubbo.version}
                
                    
                        org.apache.dubbo
                        dubbo-remoting-api
                    
                
            
            
                org.apache.dubbo
                dubbo-serialization-hessian2
                ${dubbo.version}
                
                    
                        org.apache.dubbo
                        dubbo-common
                    
                
            
        
    

    
        
        
            log4j
            log4j
            1.2.16
        
        
            org.slf4j
            slf4j-api
            1.7.5
        
        
            org.slf4j
            slf4j-log4j12
            1.7.5
        

        
        
            com.alibaba
            fastjson
            1.2.62
        
    



    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.3
                
                    1.8
                    1.8
                
            
        
    


2. 创建提供者模块

2.1 引入依赖



    
        demo-base
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    service-provider

    
        
            org.example
            service-api
            1.0-SNAPSHOT
        

        
        
            org.apache.dubbo
            dubbo
        

        
        
        
        
        
        


        
            org.apache.dubbo
            dubbo-registry-zookeeper
        
        
            org.apache.dubbo
            dubbo-rpc-dubbo
        
        
            org.apache.dubbo
            dubbo-remoting-netty4
        
        
            org.apache.dubbo
            dubbo-serialization-hessian2
        

    

2.2 编写dubbo的配置

#程序名称
dubbo.application.name=service-provider

#协议使用dubbo协议
dubbo.protocol.name=dubbo

#指定dubbo的通信端口(随意指定)
dubbo.protocol.port=20880

2.3 编写提供者的启动类,先写静态内部类作为dubbo的配置类,配置相关属性以及注册中心地址,然后再main方法中创建上下文对象,启动容器即可。

package com.lwb;

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 DubboMain {

    public static void main(String[] args) throws IOException {
        // 创建一个上下文对象
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfiguration.class);
        context.start();// 让容器启动
        System.in.read(); // 让程序停在这
    }


    // 写一个服务提供者的配置类
    @Configuration
    @EnableDubbo(scanbasePackages = "com.lwb.service.impl") // 类似组件扫描,也需要指定一个包,这里扫描的是实现类的包
    @PropertySource("classpath:/dubbo-provider.properties") // 加载配置文件
    static class ProviderConfiguration{
        // 加载注册中心的配置方法
        @Bean
        public RegistryConfig registryConfig(){
            // 创建一个注册中心的对象
            RegistryConfig registryConfig = new RegistryConfig();
            // 赋值注册中心地址  需要启动zookeeper
            registryConfig.setAddress("zookeeper://127.0.0.1:2181");
            return registryConfig;
        }
    }
}

---------------------------------------------------------------------------------------------------------------------------------

3.公共API模块只需要编写一个接口,用来调用即可。

 --------------------------------------------------------------------------------------------------------------------------------

4.消费者模块

4.1引入依赖,和提供者的依赖一样

4.2编写组件类

package com.lwb.bean;

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

@Component
public class ConsumerComponent {

    // 引入dubbo的service对象
    @Reference
    private HelloService helloService;

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


}

4.3编写配置文件

dubbo.application.name=service-consumer

#zookeeper地址
dubbo.registry.address=zookeeper://127.0.0.1:2181

4.4编写启动测试类

package com.lwb;

import com.lwb.bean.ConsumerComponent;
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 ConsumerMain {

    public static void main(String[] args) throws IOException {
        // 因为这是基于注解+配置类的方式进行dubbo参数的配置
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        // 获取消费者组件
        ConsumerComponent service = context.getBean(ConsumerComponent.class);
        while (true){
            System.in.read();
            String world = service.sayHello("world");
            System.out.println("result===="+world);
        }
    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan(basePackages = "com.lwb.bean") // spring的组件扫描
    @EnableDubbo //因为要引入dubbo组件,所以需要该注解
    static class ConsumerConfiguration{

    }

}

运行效果:

说明:在控制台按几次键,就会打印几次结果。 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5687889.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存