Spring Boot 集成 Dubbo 微服务
dubbo地址:https://dubbo.apache.org/zh/docs/v3.0/concepts/service-discovery/
dubbo架构:
1、搭建zookeeper 服务器
window版本:zookeeper-3.4.13.tar.gz
修改:.\zookeeper-3.4.13\conf\zoo.cfg
添加如下信息:
#数据路径
dataDir=E:\BigData\zookeeper-3.4.13\data
#日志路径:
dataLogDir=E:\BigData\zookeeper-3.4.13\logs
启动zk
双击:zookeeper-3.4.13\bin\zkServer.cmd
启动成功!
1.1、引入maven dubbo pom包
<!--引入dubbo环境-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
代码目录:
2、通过注解方式
2.1 配置provider(生产者)
2.1.1 配置provider 的 yaml
dubbo:
application: #应用配置,用于配置当前应用信息,不管该应用是提供者还是消费者。
name: dubbo-provider
registry: #注册中心配置,用于配置连接注册中心相关信息。
address: 127.0.0.1:2181
protocol: zookeeper
check: false
protocol: #协议配置,用于配置提供服务的协议信息,协议由提供方指定,消费方被动接受。
name: dubbo
port: 30003
monitor:
protocol: register
consumer:
check: false
timeout: 3000
## 发布到dubbo的接口所在包位置,可以不配置
# scan: com.mtl.productprovier.qo
#service: # 手动注入方式
# interface: com.mtl.productprovier.qo
# ref: productQo
server:
port: 8081
2.1.2 编写接口 ProductQo
public interface ProductQo {
public List<String> getProductList();
}
2.1.3 接口实现 ProductQoImpl
/*注意使用dubbo的Service注解*/
import com.alibaba.dubbo.config.annotation.Service;
@Service
public class ProductQoImpl implements ProductQo {
@Override
public List<String> getProductList() {
return Arrays.asList("张三", "李四");
}
}
2.1.4 启动类加上注解 @EnableDubbo 启用dubbo服务
@SpringBootApplication
@EnableDubbo
public class ProductProvierApplication{
public static void main(String[] args) {
SpringApplication.run(ProductProvierApplication.class, args);
}
}
2.2 配置consumer(消费者)
2.2.1 配置consumer 的yaml
dubbo:
application:
name: dubbo-consumer
registry:
address: 127.0.0.1:2181
protocol: zookeeper
check: false
monitor:
protocol: register
consumer:
check: false
timeout: 3000
server:
port: 8082
2.2.2 创建controller包 并添加 OrderController 文件
ProductQo:需要引入 provider jar包
<dependency>
<groupId>com.mtl</groupId>
<artifactId>product-provier</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
@Reference:消费端调用dubbo服务
@RestController
@RequestMapping("/order")
public class OrderController {
@Reference
private ProductQo productQo;
@GetMapping("/list")
public List<String> list() {
return productQo.getProductList();
}
}
2.2.3 启动类加上注解 @EnableDubbo 启用dubbo服务
@SpringBootApplication
@EnableDubbo
public class OrderConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(OrderConsumerApplication.class, args);
}
}
浏览器访问:http://localhost:8082/order/list
访问成功!
3、通过xml方式
目录:
3.1 provider 服务端
3.1.1 resources文件夹 添加 dubbo-provider.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 必须加上:提供方应用服务端的名称 -->
<dubbo:application name="dubbo-provider"/>
<!-- 服务端需要把dubbo服务注册到zookeeper上进行广播 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 服务端声明需要对外开放提供服务的接口 -->
<dubbo:service interface="com.mtl.demo.qo.ProductQo" protocol="dubbo" ref="productQo"/>
<!--<!– 提供方应用信息,用于计算依赖关系 –>-->
<!--<dubbo:application name="dubbo-provider"/>-->
<!--<dubbo:registry id="register" address="${dubbo.registry.address}"/>-->
<!--<dubbo:registry id="local" address="zookeeper://127.0.0.1:2181"/>-->
<!--<dubbo:protocol name="${dubbo.protocol.name}" port="${dubbo.protocol.port}"/>-->
</beans>
3.1.2 添加接口 ProductQo
package com.mtl.demo.qo;
import java.util.List;
public interface ProductQo {
public List<String> getProductList();
}
3.1.2 接口实现 ProductQoImpl
@Service:要用 spring 下的注解
import org.springframework.stereotype.Service;
@Service("productQo")
public class ProductQoImpl implements ProductQo {
@Override
public List<String> getProductList() {
return Arrays.asList("你是谁?", "来自哪里?");
}
}
3.1.2 接口实现 ProductQoImpl
@ImportResource:启动类添加 dubbo-provider.xml 配置文件
@SpringBootApplication
@ImportResource("dubbo-provider.xml")
@EnableDubbo
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
3.2 consumer 消费端
3.2.1 resources文件夹 添加 dubbo-consumer.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- 添加 DUBBO SCHEMA -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 应用名 -->
<dubbo:application name="dubbo-consumer"/>
<!-- 连接到哪个注册中心(连接到本机的2181端口zookeeper) -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<!-- 消费方用什么协议获取服务(用dubbo协议在20880端口暴露服务) -->
<dubbo:protocol port="28080"/>
<!-- 提供哪些接口给消费者调用 -->
<dubbo:reference id="productQo" interface="com.mtl.demo.qo.ProductQo" />
</beans>
3.2.2 创建controller包 并添加 OrderController 文件
ProductQo:需要引入 provider jar包
<dependency>
<groupId>com.mtl</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
@Reference:消费端调用dubbo服务
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
ProductQo productQo;
@GetMapping("/list")
public List<String> list() {
return productQo.getProductList();
}
}
3.2.3 启动类加上注解 @EnableDubbo 启用dubbo服务
@ImportResource:启动类添加 dubbo-consumer.xml 配置文件
@SpringBootApplication
@ImportResource("dubbo-consumer.xml")
@EnableDubbo
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
启动服务
调用成功~