简搭一个springcloud+zookeeper+dubbo脚手架
写了这么久的代码,除了crud就是crud,到头来连项目都不会搭,这里捡起来一下,仿照现有项目样式,自己写一个,锻炼一下自己的能力(基本看着各种文档搭的,知其然不知其所以然)。
1.项目准备
1.1项目介绍
简介:一个分布式的贸易项目,先把后端基础编码环境搭起来,数据库,前端还有其他后续再说。
1.2项目创建
先建几个maven项目。
base-parent:管理其他模块项目,该项目配置的dependency所有的jar包都会被该项目下的所有模块继承
app-gateway:网关项目,主要接收前台强求,连接后端接口
master-data-api:主数据中心api
master-data-service:主数据中心service层
2.创建项目
2.1base-parent
先建base-parent项目,就是一个maven项目。
一路next完事。
其实听同事说,现在的不需要再单独建parent项目了,各模块单独在一个父模块下面就行,继续问下去他也不是很清楚,我就还是建了,后续了解了再改吧。
base-parent pom文件
主要导入boot和cloud的包
<?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.example</groupId>
<artifactId>base-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<!--spring boot dependencies-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud dependencies-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
2.2app-gateway
同样是建一个maven项目,想简单些就直接右击parent项目
可以看到parent就是base-parent,pom文件的配置会自己给你写上去。
然后是gateway的pom配置,因为这个项目需要用到zookeeper和dubbo,所以需要引入相关依赖。
<?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>com.example</groupId>
<artifactId>base-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>app-gateway</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--zookeeper依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
gateway application.properties文件配置
#设置内嵌tomcat端口号
server.port=8082
server.servlet.context-path=/
#dubbo配置
spring.application.name=app-gateway
#指定注册中心,使用的是zookeeper
spring.dubbo.registry=zookeeper://127.0.0.1:2181
因为gateway是消费端的,所以先把对应的配置写好
doubbo-data-consumer.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="data-consumer"/>
<!-- 使用zookeeper做为注册中心 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" check="false"/>
<!-- 用dubbo协议在20880端口暴露服务,默认:20880 -->
<dubbo:protocol name="dubbo" port="20881" dispather="all"/>
<!-- 缺省配置 -->
<dubbo:consumer timeout="1800000" retries="0"/>
<!-- ref中的值要跟服务实现类中的@Server的值一致 -->
<!-- 这是样例接口,需要主数据中心有对应接口的配置-->
<dubbo:reference interface="com.example.data.api.service.demo.DemoQueryApi" id="demoQueryApi" check="false"/>
</beans>
然后还有最重要的application
package com.example;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication(scanBasePackages = {"com.example"})
@ImportResource({"classpath:doubbo-data-consumer.xml"})
@EnableDubboConfiguration//开启dubo的配置
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
}
2.3master-data-api
同样是建一个maven项目,想简单些就直接右击parent项目
可以看到parent就是base-parent,pom文件的配置会自己给你写上去。
然后是gateway的pom配置,因为这个项目需要用到zookeeper和dubbo,所以需要引入相关依赖。
<?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>com.example</groupId>
<artifactId>base-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>app-gateway</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--zookeeper依赖-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
api pom默认,打包方式改成jar就行
<parent>
<artifactId>base-parent</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>master-data-api</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging><!--打包成jar-->
2.4master-data-service
同样建一个继承base-parent的项目
master-data-service是服务提供方,所以pom里也需要加入zk,db的依赖。
<parent>
<artifactId>base-parent</artifactId>
<groupId>com.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>master-data-service</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging><!--打包成war-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Dubbo 依赖 -->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
master-data-service application.properties
#设置内嵌tomcat端口号
server.port=8081
#设置上下文
server.servlet.context-path=/
#设置dubbo的配置
spring.application.name=master-data-service
#设置当前工程是一个服务提供者
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://127.0.0.1:2181
doubbo-data-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="data-provider"/>
<!-- 使用zookeeper做为注册中心 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" check="false"/>
<!-- 用dubbo协议在20880端口暴露服务,默认:20880 -->
<dubbo:protocol name="dubbo" port="20880" dispather="all"/>
<!-- 缺省配置 -->
<dubbo:provider timeout="30000" threadpool="fixed" threads="100" accepts="1000" id="payload" payload="11557050"/>
<!-- ref中的值要跟服务实现类中的@Server的值一致 -->
<dubbo:service interface="com.example.data.api.service.demo.DemoQueryApi" ref="demoQueryApi"></dubbo:service>
<bean id="demoServiceQueryImpl" class="com.example.data.api.impl.DemoServiceQueryImpl"/>
</beans>
application:
package com.example.data;
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication(scanBasePackages = {"com.example"})
@ImportResource({"classpath:doubbo-data-provider.xml"})
@EnableDubboConfiguration//开启dubo的配置
public class MasterDataServiceApplication {
public static void main(String[] args) {
SpringApplication.run(MasterDataServiceApplication.class, args);
}
}
然后接下来还有最后一步,安装zookeeper
http://www.apache.org/dyn/closer.cgi/zookeeper
下载zookeeper,目录随意,能找到就行。
在zookeeper-3.4.6\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
# the port at which the clients will connect
clientPort=2181
# 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
zookeeper-3.4.6\bin下面
每次启动项目提前打开它。
项目启动先起service,再起gateway。
这个脚手架只是初步搭建,还很不完善,但是勉强能用,仅供参考,照抄等死。