由于dubbo起初是阿里开发,后来交给了apache维护,所以市场上就有两个版本的dubbo整合
1 Alibaba整合Dubbo
1.1 服务提供者
1.1.1 服务提供者接口
项目结构图
pom.xm
<groupId>cn.jzh</groupId>
<artifactId>dubbo-service</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
接口类
public interface DemoService {
public String test (String msg);
}
1.1.2 服务提供者实现类
1.1.2.1 项目结构图
1.1.2.2 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.jzh</groupId>
<artifactId>dubbo-service-alibaba-impl</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- 接口-->
<dependency>
<groupId>cn.jzh</groupId>
<artifactId>dubbo-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--SpringBoot框架web项目起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Dubbo集成SpringBoot框架起步依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- 访问zookeeper客户端 jar包 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
<!--排除依赖里的日志-->
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
1.1.2.3 服务实现类
import cn.jzh.service.DemoService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;
@Component
// @Service 这个注解使用的不是spring里面的,而是com.alibaba.dubbo.config.annotation.Service。
// timeout 配置超时时间 , interfaceClass 接口类 ,version 服务版本,如果配置了服务版本在消费端引用也必须一样
@Service(timeout = 1000,interfaceClass = DemoService.class)
public class DemoAlibabaServiceImpl implements DemoService {
@Override
public String test(String msg) {
return "dubbo接收信息"+msg;
}
}
1.1.2.4 配置文件
application.properties
server.port=8081
server.servlet.context-path=/
spring.application.name=spingboot-alibaba-provider
#设置dubbo的配置
dubbo.application.name=springboot-alibaba-provider
# 注册中心
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
1.1.2.5 启动类
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo // 启动dubbo功能
@EnableDubboConfiguration // 启动dubbo配置
@DubboComponentScan("cn.jzh.service.impl") // 扫描提供者实现类
public class AppAlibaba {
public static void main(String[] args) {
SpringApplication.run(AppAlibaba.class,args);
}
}
1.2 服务消费者
1.2.1 项目结构图示
提供者和消费者的pom.xml
一样,所以就不在此处写pom.xml
了
1.2.2 请求入口
import cn.jzh.service.DemoService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
// 在这里是使用@Reference去发现服务而不是@Autowired去注入Bean,@Reference里面可以配置version、timeout超时时间
@Reference
private DemoService demoService;
@RequestMapping("/test")
public String findAllStudent(){
return demoService.test("测试结果");
}
}
1.2.3 配置文件
application.properties
server.port=8082
server.servlet.context-path=/
spring.application.name=spingboot-alibaba-consumer
#设置dubbo的配置
dubbo.application.name=springboot-alibaba-consumer
# 注册中心
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
1.2.4 启动类
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
@EnableDubboConfiguration
public class AppAlibabaConsumer {
public static void main(String[] args) {
SpringApplication.run(AppAlibabaConsumer.class,args);
}
}
2 Apache整合Dubbo
2.1 服务提供者
2.1.1 服务提供者接口
项目结构图
pom.xm
<groupId>cn.jzh</groupId>
<artifactId>dubbo-service</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
接口类
public interface DemoService {
public String test (String msg);
}
2.1.2 服务提供者实现类
2.1.2.1 项目结构图
2.1.2.2 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
</parent>
<groupId>cn.jzh</groupId>
<artifactId>dubbo-service-apache-impl</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- 接口-->
<dependency>
<groupId>cn.jzh</groupId>
<artifactId>dubbo-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--SpringBoot框架web项目起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Dubbo集成SpringBoot框架起步依赖 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.1.2</version>
</dependency>
<!-- zookeeper依赖 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.13</version>
<exclusions>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
<version>4.2.0</version>
</dependency>
</dependencies>
</project>
2.1.2.3 服务实现类
在新版apache-dubbo
中dubbo
提供者注解@Service
提示为过时,推荐使用注解@DubboService
package cn.jzh.service.impl;
import cn.jzh.service.DemoService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;
@Component
@DubboService
public class DemoApacheServiceImpl implements DemoService {
@Override
public String test(String msg) {
return "dubbo接收信息"+msg;
}
}
2.1.2.4 配置文件
application.properties
server.port=8083
server.servlet.context-path=/
spring.application.name=spingboot-apache-provider
#设置dubbo的配置
dubbo.application.name=springboot-apache-provider-dubbo
# 注册中心
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.registry.protocol=zookeeper
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
2.1.2.5 启动类
package cn.jzh;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableDubbo
@SpringBootApplication
@DubboComponentScan("cn.jzh.service.impl")//用来扫描提供的接口实现位置
public class AppApache {
public static void main(String[] args) {
SpringApplication.run(AppApache.class,args);
}
}
2.2 服务消费者
2.2.1 项目结构图示
提供者和消费者的pom.xml
一样,所以就不在此处写pom.xml
了
2.2.2 请求入口
在新版apache-dubbo
中dubbo
提供者注解@Reference
提示为过时,推荐使用注解@DubboReference
package cn.jzh.contoller;
import cn.jzh.service.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoController {
@DubboReference
private DemoService demoService;
@RequestMapping("/test")
public String findAllStudent(){
return demoService.test("测试结果");
}
}
2.2.3 配置文件
application.properties
server.port=8084
server.servlet.context-path=/
spring.application.name=spingboot-apache-consumer
#设置dubbo的配置
dubbo.application.name=springboot-apache-consumer-dubbo
# 注册中心
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.registry.protocol=zookeeper
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
2.2.4 启动类
package cn.jzh;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class AppApacheConsumer {
public static void main(String[] args) {
SpringApplication.run(AppApacheConsumer.class,args);
}
}
原文地址:http://www.cnblogs.com/jingzh/p/16926509.html
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请务用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性