• springboot集成dubbo


    一、安装zookeeper

    docker pull zookeeper

      

    二、dubbo-provider

      2.1 POM

    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.cnblogs.pengsn</groupId>
        <artifactId>dubboServer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.4.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/io.dubbo.springboot/spring-boot-starter-dubbo -->
            <dependency>
                <groupId>io.dubbo.springboot</groupId>
                <artifactId>spring-boot-starter-dubbo</artifactId>
                <version>1.0.0</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.10</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>4.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    
    </project>

      2.2 配置

    server.port=9999
    spring.dubbo.application.name=dubbo_provider
    spring.dubbo.registry.protocol=zookeeper
    spring.dubbo.registry.address=zookeeper://172.19.82.243:32777
    spring.dubbo.protocol.name=dubbo
    spring.dubbo.protocol.port=20880
    spring.dubbo.server=true
    spring.dubbo.scan.base-packages=com.cnblogs.pengsn.dubbo.provider.service

           2.3 接口实现

    package com.cnblogs.pengsn.dubbo.provider.service.impl;
    
    import java.util.UUID;
    
    import org.springframework.stereotype.Component;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import com.cnblogs.pengsn.dubbo.provider.pojo.UserInfo;
    import com.cnblogs.pengsn.dubbo.provider.service.ITestService;
    
    @Component
    @Service(version = "1.0" , interfaceClass = ITestService.class)
    public class TestServiceImpl implements ITestService{
    
        public UserInfo queryUserInfo(UserInfo userInfo) {
            System.err.println(userInfo.toString());
            UserInfo ui = new UserInfo();
            ui.setUserId(UUID.randomUUID().toString());
            ui.setUserName("pengsn");
            return ui;
        }
    
    }
    package com.cnblogs.pengsn.dubbo.provider.service;
    
    import com.cnblogs.pengsn.dubbo.provider.pojo.UserInfo;
    
    public interface ITestService {
    
        public UserInfo queryUserInfo( UserInfo userInfo );
        
    }
    package com.cnblogs.pengsn.dubbo.provider.pojo;
    
    import java.io.Serializable;
    
    public class UserInfo implements Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = 2391763259872013964L;
    
        private String userName;
        
        private String userId;
    
        public String getUserName() {
            return userName;
        }
    
        public void setUserName(String userName) {
            this.userName = userName;
        }
    
        public String getUserId() {
            return userId;
        }
    
        public void setUserId(String userId) {
            this.userId = userId;
        }
        
        @Override
        public String toString() {
            return "userName: " + userName + "; userId: " + userId;
        }
        
    
    }

           2.4 服务启动  

    package com.cnblogs.pengsn;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
    
    @SpringBootApplication
    @EnableDubbo
    public class DubboServerApplication {
    
        public static void main(String[]args) {
            SpringApplication.run(DubboServerApplication.class, args);
        }
        
    }

    三、dubbo-consumer

      3.1 POM

    <project xmlns="http://maven.apache.org/POM/4.0.0"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.cnblogs.pengsn</groupId>
        <artifactId>comsumer</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.4.RELEASE</version>
            <relativePath /> <!-- lookup parent from repository -->
        </parent> 
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- https://mvnrepository.com/artifact/io.dubbo.springboot/spring-boot-starter-dubbo -->
            <dependency>
                <groupId>io.dubbo.springboot</groupId>
                <artifactId>spring-boot-starter-dubbo</artifactId>
                <version>1.0.0</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
            <dependency>
                <groupId>com.101tec</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.10</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>4.0.1</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
        
    </project>

      3.2 配置

    server.port=9998
    spring.dubbo.application.name=dubbo_consumer
    spring.dubbo.registry.protocol=zookeeper
    spring.dubbo.registry.address=zookeeper://172.19.82.243:32777
    spring.dubbo.protocol.name=dubbo
    spring.dubbo.protocol.port=12345
    spring.dubbo.scan=org.spring.springboot.dubbo

           3.3 接口调用

    package com.cnblogs.pengsn.business.impl;
    
    import org.springframework.stereotype.Component;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import com.cnblogs.pengsn.business.IConsumerService;
    import com.cnblogs.pengsn.dubbo.provider.pojo.UserInfo;
    import com.cnblogs.pengsn.dubbo.provider.service.ITestService;
    
    @Component
    public class ConsumerServiceImpl implements IConsumerService {
    
        @Reference(version = "2.0")
        private ITestService testService;
        
        public void test() {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserId("good");
            userInfo.setUserName("userName");
            UserInfo queryUserInfo = testService.queryUserInfo(userInfo);
            System.err.println(queryUserInfo);
        }
    
    }
    package com.cnblogs.pengsn.business;
    
    public interface IConsumerService {
        
        public void test();
    
    }

           3.4 服务启动  

    package com.cnblogs.pengsn;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.ConfigurableApplicationContext;
    
    import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
    import com.cnblogs.pengsn.business.IConsumerService;
    
    @SpringBootApplication
    @EnableDubbo
    public class DubboClientApplication {
    
        public static void main(String[]args) {
            ConfigurableApplicationContext context = SpringApplication.run(DubboClientApplication.class, args);
            IConsumerService bean = context.getBean(IConsumerService.class);
            bean.test();
        }
        
    }

    四、安装dubbo-admin查看

       

       

       

    如果觉得文章对您有用,请点下推荐。您的支持将鼓励我继续创作!

  • 相关阅读:
    《老男孩》
    java中四种阶乘的计算
    DataOutputStream的乱码问题
    java中飞clone方法
    类反射的简单例子
    PHP: 如何连接oracle数据库进行数据读取
    .NET : 如何读取图片中的元数据信息
    重新审视REST
    PHP 的历史
    Image File Format Specifications {转载}
  • 原文地址:https://www.cnblogs.com/pengsn/p/14680713.html
Copyright © 2020-2023  润新知