Dubbo起步

2020-04-29  本文已影响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

初步印象

✔ 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'

参考

上一篇下一篇

猜你喜欢

热点阅读