目录:
Spring整合Dubbo
1.1 依赖
1.2 Provider
1.3 Consumer
Springboot整合Dubbo
2.1 公共API工程
2.2 dubbo提供者
2.3 dubbo消费者
Spring整合Dubbo
依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
Provider
配置文件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"
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="provider" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 用dubbo协议在29014端口暴露服务 -->
<dubbo:protocol name="dubbo" port="29014" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.itmayiedu.service.UserService"
ref="orderService" />
<!-- 具体的实现bean -->
<bean id="orderService" class="com.itmayiedu.service.impl.UserServiceImpl" />
</beans>
Service
public class UserServiceImpl implements UserService {
public String getList(Integer id) {
System.out.println("客户端有人来消费了....");
if (id==1) {
return "我";
}
if (id==2) {
return "扎克伯格";
}
if (id==3) {
return "马化腾";
}
return "没有找到";
}
}
启动
public class TestMember {
public static void main(String[] args) throws IOException {
// 发布服务
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("provider.xml");
app.start();// 加载
System.out.println("服务发布成功...");
System.in.read(); // 让程序阻塞
}
}
Consumer
配置文件
<?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"
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="consumer" />
<!-- 使用multicast广播注册中心暴露发现服务地址 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="userService" interface="com.itmayiedu.service.UserService" />
</beans>
启动
ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("consumer.xml");
UserService userService = (UserService) app.getBean("userService");
String name = userService.getList(1);
System.out.println("name:" + name);
Springboot整合Dubbo
公共API工程
放入一个接口即可
public interface DubboApi {
String hello();
}
dubbo提供者
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- dubbo的依赖 -->
<!-- https://mvnrepository.com/artifact/com.alibaba.spring.boot/dubbo-spring-boot-starter -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!-- 公共api -->
<dependency>
<groupId>com.jason</groupId>
<artifactId>DubboApi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
配置文件
server:
port: 8081
spring:
dubbo:
application:
name: dubbo-provider
id: dubbo-provider # 这里id和name都不能少,不然在这个版本下会报错
registry:
protocol: zookeeper
address: localhost:2181
protocol:
name: dubbo
port: 2${server.port}
server:
true
monitor:
protocol: registry
Service层
特别注意,这里的service是dubbo包里面的!!!
import org.springframework.stereotype.Component;
import com.alibaba.dubbo.config.annotation.Service;
import com.jason.api.DubboApi;
@Service //特别注意,这里的service是dubbo包里面的!!!
@Component
public class TestService implements DubboApi{
@Override
public String hello() {
// TODO Auto-generated method stub
return "hello";
}
}
Controller层
@RestController
public class TestController{
@Autowired
private TestService testService;
@RequestMapping("/hello")
public String hello() {
// TODO Auto-generated method stub
return testService.hello();
}
}
启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
@SpringBootApplication
@EnableDubboConfiguration
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
dubbo消费者
依赖
和提供者一样
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- dubbo的依赖 -->
<!-- https://mvnrepository.com/artifact/com.alibaba.spring.boot/dubbo-spring-boot-starter -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!-- 公共api -->
<dependency>
<groupId>com.jason</groupId>
<artifactId>DubboApi</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
配置文件
server:
port: 8082
spring:
dubbo:
application:
name: dubbo-consumer
id: dubbo-consumer
registry:
protocol: zookeeper
address: localhost:2181
protocol:
name: dubbo
port: 2${server.port}
server:
true
monitor:
protocol: registry
调用接口
@RestController
public class TestController{
@Reference(check=false)
private DubboApi dubboApi;
@RequestMapping("/hello")
public String hello() {
return dubboApi.hello();
}
}
启动类
@SpringBootApplication
@EnableDubboConfiguration
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}