idea搭建dubbo项目(服务提供者+服务消费者)
下一个项目要用到dubbo,自己之前没有用过这个框架,所以提前学习搭建一下,在这些个博客是为了以后学习改进之用。
啥也不说,先启动zookeerer,不会的或者没有安装的先看看别的教程哦。。。
dubbo官方文档:http://dubbo.apache.org/en-us/
看看官方的dubbo架构:
项目结构:
api存放一些共工模块的接口,pojo等等,可以建一个maven项目,或者都建为spingboot项目,我这里都是统一建了springboot项目。
右击项目名,module
在api里面写一个接口:
public interface TestService {
String testDubbo();
}
再来写一个服务提供者,创建项目跟上面一样,也是创建一个springboot项目:
导入相关依赖(要将api模块添加进去):
<dependency>
<groupId>com.study</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<!-- 整合dubbo -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- zookeeper客户端 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.7</version>
</dependency>
yml文件:
dubbo:
application:
name: dubbo-provider
registry:
address: 127.0.0.1:2181
protocol: zookeeper
check: false
protocol:
name: dubbo
port: 20888
monitor:
protocol: register
consumer:
check: false
timeout: 3000
scan: com.study.api.service.impl #扫描的包名
server:
port: 8091
实现api里面的接口:
注意这里面的service注解不是spring里面那个service注解了,
而是import com.alibaba.dubbo.config.annotation.Service;
@Service
public class TestServiceImpl implements TestService {
@Override
public String testDubbo() {
return "hello world";
}
}
启动类(添加了新注解):
@EnableDubbo
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
接下来我们创建一个消费者,跟创建服务提供者一样:
pom文件:
<!-- dubbo依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.study</groupId>
<artifactId>api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
yml:
注意:我这边端口号加了" “,这边我感觉是springboot某个版本的一个坑在这,如果不添加” ",则一直报错。。。
dubbo:
application:
name: dubbo-consumer
registry:
address: 127.0.0.1:2181
protocol: zookeeper
check: false
protocol:
name: dubbo
port: "20888"
monitor:
protocol: register
consumer:
check: false
timeout: 3000
server:
port: 8092
public interface ProductService {
String test();
}
注意注解哦
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.study.api.service.TestService;
import com.study.consumer.service.ProductService;
@Service
public class ProductServiceImpl implements ProductService {
@Reference
private TestService testService;
@Override
public String test() {
return testService.testDubbo();
}
}
import com.study.consumer.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Autowired
private ProductService productService;
@RequestMapping(value = "/test",method = RequestMethod.GET)
public String test01(){
return productService.test();
}
}
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
启动服务提供者和消费者:
访问: