目录
一:SpringBoot集成Dubbo
Apache Dubbo Spring Boot 项目可以轻松使用 Dubbo 作为 RPC 框架创建Spring Boot应用程序。更重要的是,它还提供:
Apache Dubbo是一个高性能、轻量级、基于java的RPC框架。 Dubbo 提供了三个关键功能,包括基于接口的远程调用、容错和负载均衡以及自动服务注册和发现。
发布版本
<dependencies>
<!-- Dubbo Spring Boot Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
</dependencies>
1. 创建公共项目
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>com.zl</groupId>
<artifactId>dubbo-maven-1</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
model类
package com.zl.model;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 7924975682459971235L;
private Integer id;
private String name;
private Integer age;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
服务接口
package com.zl.service;
import com.zl.model.Student;
public interface StudentService {
Student queryStudent(Integer id);
}
2. 创建提供者项目provider
第一步:创建SpringBoot项目,引入核心依赖
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 https://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.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zl</groupId>
<artifactId>dubbo-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>dubbo-provider</name>
<description>dubbo-provider</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--加入公共项目的gav-->
<dependency>
<groupId>com.zl</groupId>
<artifactId>dubbo-maven-1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.8</version>
</dependency>
<!--zookeeper依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.8</version>
<type>pom</type>
<exclusions>
<!-- 排除log4j依赖 -->
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
第二步:实现Service公共项目的接口,暴露服务
package com.zl.service.impl;
import com.zl.model.Student;
import com.zl.service.StudentService;
import org.apache.dubbo.config.annotation.DubboService;
// 暴露服务
@DubboService(interfaceClass = StudentService.class,version = "1.0"
,timeout = 5000)
public class StudentServiceImpl implements StudentService {
@Override
public Student queryStudent(Integer id) {
Student student = new Student();
if (1001 == id){
student.setId(1001);
student.setName("Jack");
student.setAge(18);
}else if(1002 == id){
student.setId(1002);
student.setName("Rose");
student.setAge(22);
}
return student;
}
}
第三步:application.properties进行dubbo属性配置
#配置服务器名称
spring.application.name=studentService-provider
#配置扫描的包
dubbo.scan.base-packages=com.zl.service
#配置dubbo协议
#dubbo.protocol.name=dubbo
#dubbo.protocol.port=20881
#注册中心
dubbo.registry.address=zookeeper://localhost:2181
第四步:启动类
package com.zl;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo // 启动Dubbo
public class DubboProviderApplication {
public static void main(String[] args) {
SpringApplication.run(DubboProviderApplication.class, args);
}
}
3. 创建消费者consumer项目
第一步:创建SpringBoot项目,引入核心依赖,和提供者相同;增加一个web依赖,方便测试
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
第二步:controller调用远程服务
package com.zl.controller;
import com.zl.model.Student;
import com.zl.service.StudentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DubboController {
// 这里的interfaceClass属性省略也行
@DubboReference(interfaceClass = StudentService.class,version = "1.0")
private StudentService studentService;
@GetMapping("/query")
public String queryStudent(){
Student student = studentService.queryStudent(1001);
return "调用远程接口,获取对象:"+student;
}
}
第三步:application.properties进行dubbo属性配置
#指定服务名称
spring.application.name=consumer-application
#指定注册中心
dubbo.registry.address=zookeeper://localhost:2181
第四步:启动类
package com.zl;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableDubbo
public class DubboConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(DubboConsumerApplication.class, args);
}
}
4. 注册中心Zookeeper的安装
解压下载好的Zookeeper压缩包:
C:\dev\Zookeeper\apache-zookeeper-3.5.5-bin\conf\zoo.cfg进行配置
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
#dataDir=/tmp/zookeeper
#修改存放临时生成的数据的目录
dataDir=C:/dev/Zookeeper/apache-zookeeper-3.5.5-bin/data
# the port at which the clients will connect
#端口号
clientPort=2181
#需要启动另外一个服务,默认端口是8080,防止冲突修改一下
admin.serverPort=8888
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
执行zkServer.cmd程序,启动Zookeeper
启动提供者和消费者项目的启动类
进行访问
图书推荐:《Python 自动化办公应用大全》
关键点
内容简介
当当网链接:《Python自动化办公应用大全(ChatGPT版)
京东的链接:京东安全