首先确保你的环境中有 Docker,输入下列命令进行 Zookeeper 安装。
docker run -p 2181:2181 -d --name=zookeeper zookeeper
首先确保你的环境有 Git 和 Maven,然后依次输入下列命令。
git clone https://github.com/apache/dubbo-admin.git
cd dubbo-admin
mvn clean package -Dmaven.test.skip=true
cd dubbo-admin-distribution/target
java -jar dubbo-admin-0.2.0-SNAPSHOT.jar
成功运行之后访问 127.0.0.1:8080,出现以下界面说明管理控制台搭建成功。
打开 IDEA 创建一个基础的 Gradle Module Project,创建服务接口类。
package com.example.api.service;
public interface HelloWorldService {
public String sayHelloTo(String developer);
}
将接口工程项目打包成 Jar 包,一会儿提供给服务提供者和消费者使用。
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'org.apache.dubbo:dubbo-spring-boot-starter:2.7.7'
implementation 'org.apache.curator:curator-framework:4.3.0'
implementation 'org.apache.curator:curator-recipes:4.3.0'
implementation 'com.example:api:1.0.0'
}
package com.example.provider.service.impl;
import com.example.api.service.HelloWorldService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService
public class HelloWorldServiceImpl implements HelloWorldService {
@Override
public String sayHelloTo(String developer) {
return "Hello World " + developer;
}
}
package com.example.provider;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo(scanBasePackages = "com.example.provider.service.impl")
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
spring.application.name=com.example.provider
server.port=8082
dubbo.application.name=com.example.provider
dubbo.registry.address=zookeeper://127.0.0.1:2181
package com.example.consumer.controller;
import com.example.api.service.HelloWorldService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@DubboReference
private HelloWorldService helloWorldService;
@GetMapping("/hello")
public String sayHello(String developer) {
return helloWorldService.sayHelloTo(developer);
}
}
spring.application.name=com.example.consumer
server.port=8081
dubbo.application.name=com.example.consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
到这里我们就完成了一个最基础的 Dubbo Demo Project!点击获取示例代码