一 . 概述
二 . 环境准备
2.1 zookeeper安装与启动
注意:zookeeper版本与spring-boot依赖包版本存在冲突问题, 本教程只适用于zookeeper-3.5.5
步骤一:下载后解压文件,执行命令cd apache-zookeeper-3.5.5-bin/conf
,cp zoo_sample.cfg zoo.cfg
,复制一份zoo_sample.cfg命名为zoo.cfg
-
步骤二:启动zookeeper
# 进入bin目录
cd /Users/a10.11.5/Downloads/apache-zookeeper-3.5.5-bin/bin
# 启动
./zkServer.sh start
步骤三:查看是否启动成功
ps -ef | grep zookeeper
三 . 整合spring-boot
3.1 工程结构介绍
说明:统一由
dubbo-manager-service-api
提供提供公共接口,dubbo-manager-consumer-6002
和dubbo-manager-provider-6001
公共调用
3.2 dubbo-manager-service-api
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">
<parent>
<artifactId>dubbo-manager</artifactId>
<groupId>com.qf</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-manager-service-api</artifactId>
<name>dubbo-manager-service-api</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
</project>
User
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
UserSerivce
public interface UserSerivce {
public List<User> getAll(String name);
}
3.3 dubbo-manager-provider-6001(提供方)
依赖包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo与spring boot整合的包 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!-- 注册中心是zookeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<!-- dubbo底层的通信方式 -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.24.Final</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>com.qf</groupId>
<artifactId>dubbo-manager-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
YML配置
server:
port: 6001
dubbo:
application:
name: dubbo-provider
protocol:
port: 20880
serialization: fastjson
registry:
address: zookeeper://localhost:2181
scan:
base-packages: com.qf.service
logback.xml
说明:日志处理
<?xml version="1.0" encoding="UTF-8"?>
<!-- scan: 当配置文件被修改后, 将会被重新载入。
scanPeriod: 置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。
debug: 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 -->
<configuration scan="true" scanPeriod="60 seconds" debug="false">
<!-- 输出到控制台, 线上屏蔽 -->
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!-- 配置日志输出到控制台的格式 -->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} -- %-4relative %-5level %logger{32} %thread -- %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
</appender>
<!-- 将日志记录到文件当中 -->
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 基于时间和大小的的滚动策略 -->
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<!--日志文件输出的文件名, 必须包含%i, 从0开始-->
<FileNamePattern>D:/logs/logback.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<!--日志文件保留天数-->
<MaxHistory>30</MaxHistory>
<!-- 最大20KB 超过最大值,会重新建一个文件-->
<maxFileSize>20MB</maxFileSize>
<!-- 所有的日志加起来最大的大小 -->
<totalSizeCap>400MB</totalSizeCap>
</rollingPolicy>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %thread -- %msg%n</pattern>
<charset>utf-8</charset>
</encoder>
</appender>
<!-- root节点是必选节点,用来指定最基础的日志输出级别,只有一个level属性。 -->
<root level="warn">
<!-- 标识这个appender将会添加到这个loger。 -->
<appender-ref ref="stdout"/>
<appender-ref ref="file"/>
</root>
</configuration>
UserServiceImpl API接口实现
/**
* 对外暴露的服务,需要指定是针对哪个接口暴露的服务
*/
@Component
@Service(interfaceClass=UserSerivce.class)
public class UserServiceImpl implements UserSerivce{
private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
@Override
public List<User> getAll(String name) {
logger.info("用户名" + name);
User user = new User();
user.setName("张三");
user.setAge(20);
return Arrays.asList(user);
}
}
3.4 dubbo-manager-consumer-6002(消费方)
依赖包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- dubbo与spring boot整合的包 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<!-- 注册中心是zookeeper -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
<!-- dubbo底层的通信方式 -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.24.Final</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<dependency>
<groupId>com.qf</groupId>
<artifactId>dubbo-manager-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
YML配置
server:
port: 6002
dubbo:
application:
name: dubbo-consumer
registry:
address: zookeeper://localhost:2181
protocol:
port: 20880
serialization: fastjson
UserController
@RestController
@RequestMapping("/user")
public class UserController {
@Reference
private UserSerivce userSerivce;
@RequestMapping
public Object getAll(String name) {
return userSerivce.getAll(name);
}
}
3.5 测试
在浏览器输入http://localhost:6002/user
成功!!!
网友评论