• Dubbo简易搭建


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

    <dependencies>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubboartifactId>
            dependency>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-registry-zookeeperartifactId>
            dependency>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-registry-nacosartifactId>
            dependency>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-rpc-dubboartifactId>
            dependency>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-remoting-netty4artifactId>
            dependency>
            <dependency>
                <groupId>org.apache.dubbogroupId>
                <artifactId>dubbo-serialization-hessian2artifactId>
            dependency>
            
            <dependency>
                <groupId>king-parentgroupId>
                <artifactId>dubbo-filter-apiartifactId>
                <version>1.0-SNAPSHOTversion>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    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;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
    2. 添加dubbo基本配置

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

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

      public class ProviderMain {
          public static void main(String[] args) throws IOException {
              AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ProviderConfig.class);
              context.start();
              System.in.read();
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

    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
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    2. 添加消费端的Config类

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

      @Component
      public class ConsumerCall {
      
          @Reference
          HelloService helloService;
          public String sayHello(String name) {
              return helloService.sayHello(name);
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    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);
              }
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11

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

  • 相关阅读:
    SS命令使用介绍
    第十章 路由器的基本配置
    鸿鹄工程项目管理系统em Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统
    java毕业设计校园商铺mybatis+源码+调试部署+系统+数据库+lw
    5、超链接标签
    springboot前后端时间类型传输
    深入了解前端开发规范的区别《Commonjs、AMD、CMD、ES6模块化》
    最新暴力破解漏洞技术详解
    Vulnhub系列靶机-The Planets Earth
    ubuntu 20.04 docker 安装 mysql
  • 原文地址:https://blog.csdn.net/Decembetion/article/details/126448249