美文网首页
Dubbo起步

Dubbo起步

作者: 诺之林 | 来源:发表于2020-04-29 09:58 被阅读0次

    目录

    定义接口

    mkdir common-api && cd common-api
    
    gradle init --type java-application
    
    vim common/api/SayHelloService.java
    
    package common.api;
    
    public interface SayHelloService {
        String sayHello(String name);
    }
    

    生产者

    生产者依赖

    spring init -b 2.1.3.RELEASE -dweb --build gradle provider-app && cd provider-app
    
    vim settings.gradle
    
    rootProject.name = 'provider-app'
    includeFlat 'common-api'
    
    vim build.gradle
    
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
        mavenCentral()
    }
    
    dependencies {
        compile project(':common-api')
        compile group: 'com.alibaba', name: 'dubbo', version: '2.6.8'
        compile group: 'io.netty', name: 'netty-all', version: '4.1.49.Final'
        compile group: 'redis.clients', name: 'jedis', version: '3.2.0'
        // 省略了未修改的配置
    }
    

    生产者实现

    vim com/example/providerapp/DemoApplication.java
    
    package com.example.providerapp;
    
    import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @EnableDubbo
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    
    vim com/example/providerapp/SayHelloServiceImpl.java
    
    package com.example.providerapp;
    
    import com.alibaba.dubbo.config.annotation.Service;
    import common.api.SayHelloService;
    
    @Service
    public class SayHelloServiceImpl implements SayHelloService {
    
        @Override
        public String sayHello(String name) {
            return "hello " + name;
        }
    }
    
    vim application.properties
    
    server.port=4000
    dubbo.application.name=provider-app
    dubbo.registry.address=redis://localhost:6379
    
    docker run --name redis-temp -p 6379:6379 -d redis
    
    ./gradlew bootrun
    

    消费者

    消费者依赖

    spring init -b 2.1.3.RELEASE -dweb --build gradle consumer-app && cd consumer-app
    
    vim settings.gradle
    
    rootProject.name = 'consumer-app'
    includeFlat 'common-api'
    
    vim build.gradle
    
    repositories {
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
        mavenCentral()
    }
    
    dependencies {
        compile project(':common-api')
        compile group: 'com.alibaba', name: 'dubbo', version: '2.6.8'
        compile group: 'io.netty', name: 'netty-all', version: '4.1.49.Final'
        compile group: 'redis.clients', name: 'jedis', version: '3.2.0'
        // 省略了未修改的配置
    }
    

    消费者实现

    vim com/example/providerapp/DemoApplication.java
    
    package com.example.providerapp;
    
    import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @EnableDubbo
    @SpringBootApplication
    public class DemoApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(DemoApplication.class, args);
        }
    
    }
    
    vim com/example/consumerapp/SayHelloController.java
    
    package com.example.consumerapp;
    
    import com.alibaba.dubbo.config.annotation.Reference;
    import common.api.SayHelloService;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.RestController;
    
    
    @RestController
    public class SayHelloController {
    
        @Reference
        private SayHelloService sayHelloService;
    
        @GetMapping("/sayHello")
        public String sayHello(@RequestParam String name) {
            return sayHelloService.sayHello(name);
        }
    }
    
    vim application.properties
    
    server.port=4001
    dubbo.application.name=consumer-app
    dubbo.registry.address=redis://localhost:6379
    
    ./gradlew bootrun
    
    curl 'localhost:4001/sayHello?name=xiaoming'
    # hello xiaoming
    

    初步印象

    • 生态非常的混乱 => com.alibaba vs org.apache.dubbo
    ✔ compile group: 'com.alibaba', name: 'dubbo-registry-nacos', version: '2.7.6'
    
    ✘ compile group: 'org.apache.dubbo', name: 'dubbo-registry-nacos', version: '2.7.6'
    
    • 版本兼容性太差 => 第三位版本不兼容 导致每次报错都不一样

    • 文档阅读体验差 => 一边阅读文档一边还要踩坑 Dubbo管理控制台跑不起来

    参考

    相关文章

      网友评论

          本文标题:Dubbo起步

          本文链接:https://www.haomeiwen.com/subject/cavmwhtx.html