1.创建公共接口模块api
package com.example.dubboapi.service;
public interface IndexService {
String echo();
}
2.创建服务提供者provider
2.1.导入依赖
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.1</version>
<type>pom</type>
</dependency>
<!-- dubbo-api 自己创建的公共接口 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2.2.编写配置文件
server:
port: 9901
dubbo:
application:
id: dubbo-provider
name: dubbo-provider
registry:
address: zookeeper://127.0.0.1:2181
2.3.编写测试类
package com.example.dubboprovider.service;
import com.example.dubboapi.service.IndexService;
import org.apache.dubbo.config.annotation.Service;
@Service
public class IndexServiceImpl implements IndexService {
@Override
public String echo() {
return "This is IndexServiceImpl echo()";
}
}
2.4.编写启动类
@SpringBootApplication
@EnableDubbo
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
3.创建服务消费者consumer
3.1.导入依赖
<dependencies>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.1</version>
<type>pom</type>
</dependency>
<!-- dubbo-api 自己创建的公共接口 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
3.2.编写配置文件
server:
port: 9000
dubbo:
application:
name: dubbo-consumer
registry:
address: zookeeper://127.0.0.1:2181
3.3.编写测试类
package com.example.dubboconsumer.controller;
import com.example.dubboapi.service.IndexService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@Reference
private IndexService service;
@RequestMapping("/")
public String index() {
return service.echo();
}
}
4.开始测试
启动zookeeper(如果你未完成zookeeper的安装,可以回看笔者此前的文章《Windows下安装Zookeeper》);
启动服务提供者模块dubbo-provider;
启动服务消费者模块dubbo-consumer;
访问消费者模块中对应的接口:http://localhost:9000/
文章转载自云丶言,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。







