2022-03-31 spring cloud alibaba+zookeeper+dubbo 简单案例配置
前言
原本是spring boot的项目,需求原因转spring cloud alibaba + zookeeper + dubbo技术(官方github地址-zh)。
参考官方案例进行调整,并记录一下,类似小案例。
相关代码在github上,供以后参考。
pom文件
父模块 pom文件
进行版本控制:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
<spring-cloud-alibaba.version>2.1.2.RELEASE</spring-cloud-alibaba.version>
<cqdata.version>1.32-SNAPSHOT</cqdata.version>
<minio.version>7.0.2</minio.version>
<hutool.version>5.7.21</hutool.version>
<swagger.version>2.6.1</swagger.version>
<response.version>2.01-SNAPSHOT</response.version>
</properties>
<dependencyManagement>
<dependencies>
<!--
spring-cloud-dependencies spring-cloud-alibaba-dependencies
spring-cloud-starter-alibaba-nacos-discovery spring-cloud-starter-dubbo
之间关系?
-->
<!-- Spring Cloud dependencies -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud Alibaba dependencies -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 内部响应包-->
<dependency>
<groupId>com.xxx</groupId>
<artifactId>response</artifactId>
<version>${response.version}</version>
</dependency>
<!--swagger2 ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<!--内部气象数据工具包-->
<dependency>
<groupId>com.xxxx</groupId>
<artifactId>data</artifactId>
<version>${cqdata.version}</version>
</dependency>
<!--minio客户端-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
<version>${minio.version}</version>
</dependency>
<!--hutool工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
公用依赖:
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
consumer模块 pom文件
依赖:
<dependencies>
<!-- 发现服务-->
<dependency>
<groupId>com.xxxx.xxxx</groupId>
<artifactId>webservice_api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!--swagger2 ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!-- Dubbo Spring Cloud Starter -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!-- Spring Cloud Zookeeper Service Discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.12</version>
</dependency>
<!--响应包-->
<dependency>
<groupId>com.xxxx</groupId>
<artifactId>response</artifactId>
</dependency>
<!--hutool工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
</dependencies>
插件:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--生命周期中测试插件 跳过测试-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
provider模块 pom文件
依赖:
<dependencies>
<!-- Sample API -->
<dependency>
<groupId>com.cqkj.zjdl</groupId>
<artifactId>webservice_api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
<!-- Dubbo Spring Cloud Starter -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<!-- Spring Cloud Nacos Service Discovery -->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>-->
<!-- </dependency>-->
<!-- Spring Cloud Zookeeper Service Discovery -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.12</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!--响应包-->
<dependency>
<groupId>com.cqkj</groupId>
<artifactId>response</artifactId>
</dependency>
<!--气象数据工具包-->
<dependency>
<groupId>com.cqkj</groupId>
<artifactId>data</artifactId>
</dependency>
<!--minio客户端-->
<dependency>
<groupId>io.minio</groupId>
<artifactId>minio</artifactId>
</dependency>
<!--hutool工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
</dependencies>
构建插件:
<build>
<plugins>
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--生命周期中测试插件 跳过测试-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
api模块 pom文件
依赖(没有实际用处):
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
</dependencies>
构建插件:
<build>
<plugins>
<!-- 只编译,不会导入相关依赖-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
yaml配置文件
spring cloud alibaba + zk + dubbo的配置
consumer 配置
# dubbo配置
dubbo:
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://172.10.11.180:2181
cloud:
subscribed-services: spring-cloud-alibaba-dubbo-provider # 发现服务
# zookeeper配置
spring:
application:
name: spring-cloud-alibaba-dubbo-consumer
cloud:
zookeeper:
discovery:
register: false
connect-string: 172.10.11.180:2181
provider 配置
# dubbo配置
dubbo:
scan:
base-packages: com.cqkj.zjdl.webservice.service.impl
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://172.10.11.180:2181 # 暴露服务
# zookeeper配置
spring:
application:
name: spring-cloud-alibaba-dubbo-provider
main:
# Spring Boot 2.1 需要设定
allow-bean-definition-overriding: true
cloud:
zookeeper:
discovery:
register: true
root: provider # 根节点(前缀)
connect-string: 172.10.11.180:2181