Motan-Zookeper分布式项目搭建
Posted jcjssl
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Motan-Zookeper分布式项目搭建相关的知识,希望对你有一定的参考价值。
1. 新建maven项目motan-zookeper及module
2. 添加模块之间的依赖
2.1 motan-zookeper.pom
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.motan.zookeper</groupId> 8 <artifactId>motan-zookeper</artifactId> 9 <packaging>pom</packaging> 10 <version>1.0-SNAPSHOT</version> 11 <modules> 12 <module>motan-zookeper-api</module> 13 <module>motan-zookeper-model</module> 14 <module>motan-zookeper-service</module> 15 <module>motan-zookeper-web</module> 16 </modules> 17 18 <parent> 19 <groupId>org.springframework.boot</groupId> 20 <artifactId>spring-boot-starter-parent</artifactId> 21 <version>1.5.7.RELEASE</version> 22 </parent> 23 <dependencies> 24 <dependency> 25 <groupId>org.projectlombok</groupId> 26 <artifactId>lombok</artifactId> 27 <version>1.16.18</version> 28 </dependency> 29 </dependencies> 30 31 <build> 32 <plugins> 33 <plugin> 34 <groupId>org.springframework.boot</groupId> 35 <artifactId>spring-boot-maven-plugin</artifactId> 36 <executions> 37 <execution> 38 <goals> 39 <goal>repackage</goal> 40 </goals> 41 </execution> 42 </executions> 43 <configuration> 44 <executable>true</executable> 45 </configuration> 46 </plugin> 47 </plugins> 48 </build> 49 50 </project>
2.2 motan-zookeper-api.pom
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>motan-zookeper</artifactId> 7 <groupId>com.motan.zookeper</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>motan-zookeper-api</artifactId> 13 14 </project>
2.3 motan-zookeper-model.pom
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>motan-zookeper</artifactId> 7 <groupId>com.motan.zookeper</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>motan-zookeper-model</artifactId> 13 14 <dependencies> 15 <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-data-jpa</artifactId> 18 </dependency> 19 </dependencies> 20 </project>
2.4 motan-zookeper-service.pom
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>motan-zookeper</artifactId> 7 <groupId>com.motan.zookeper</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>motan-zookeper-service</artifactId> 13 14 <dependencies> 15 <!--springboot自动配置 不需要web依赖--> 16 <dependency> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter</artifactId> 19 </dependency> 20 <!--性能api--> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-actuator</artifactId> 24 </dependency> 25 <!--springdata jpa自动配置--> 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-data-jpa</artifactId> 29 </dependency> 30 <!--mysql驱动--> 31 <dependency> 32 <groupId>mysql</groupId> 33 <artifactId>mysql-connector-java</artifactId> 34 </dependency> 35 <!--sql日志增强--> 36 <dependency> 37 <groupId>com.googlecode.log4jdbc</groupId> 38 <artifactId>log4jdbc</artifactId> 39 <version>1.2</version> 40 </dependency> 41 <!--motan核心依赖--> 42 <dependency> 43 <groupId>com.weibo</groupId> 44 <artifactId>motan-core</artifactId> 45 <version>0.2.2</version> 46 </dependency> 47 <!--motan传输层 netty实现--> 48 <dependency> 49 <groupId>com.weibo</groupId> 50 <artifactId>motan-transport-netty</artifactId> 51 <version>0.2.2</version> 52 </dependency> 53 <!--motan与springboot整合--> 54 <dependency> 55 <groupId>com.weibo</groupId> 56 <artifactId>motan-springsupport</artifactId> 57 <version>0.2.1</version> 58 </dependency> 59 <!--motan使用zookeeper作为注册中心--> 60 <dependency> 61 <groupId>com.weibo</groupId> 62 <artifactId>motan-registry-zookeeper</artifactId> 63 <version>0.2.1</version> 64 </dependency> 65 <dependency> 66 <groupId>ma.glasnost.orika</groupId> 67 <artifactId>orika-core</artifactId> 68 <version>1.5.1</version> 69 </dependency> 70 71 <!--依赖关系--> 72 <dependency> 73 <groupId>com.motan.zookeper</groupId> 74 <artifactId>motan-zookeper-api</artifactId> 75 <version>1.0-SNAPSHOT</version> 76 <scope>compile</scope> 77 </dependency> 78 <dependency> 79 <groupId>com.motan.zookeper</groupId> 80 <artifactId>motan-zookeper-model</artifactId> 81 <version>1.0-SNAPSHOT</version> 82 <scope>compile</scope> 83 </dependency> 84 </dependencies> 85 </project>
2.5 motan-zookeper-web.pom
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>motan-zookeper</artifactId> 7 <groupId>com.motan.zookeper</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>motan-zookeper-web</artifactId> 13 14 <dependencies> 15 <!--springboot自动配置+web mvc自动配置--> 16 <dependency> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter-web</artifactId> 19 </dependency> 20 <!--性能api--> 21 <dependency> 22 <groupId>org.springframework.boot</groupId> 23 <artifactId>spring-boot-starter-actuator</artifactId> 24 </dependency> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter-freemarker</artifactId> 28 </dependency> 29 <!--motan核心依赖--> 30 <dependency> 31 <groupId>com.weibo</groupId> 32 <artifactId>motan-core</artifactId> 33 <version>0.2.2</version> 34 </dependency> 35 <!--motan传输层 netty实现--> 36 <dependency> 37 <groupId>com.weibo</groupId> 38 <artifactId>motan-transport-netty</artifactId> 39 <version>0.2.2</version> 40 </dependency> 41 <!--motan与springboot整合--> 42 <dependency> 43 <groupId>com.weibo</groupId> 44 <artifactId>motan-springsupport</artifactId> 45 <version>0.2.1</version> 46 </dependency> 47 <!--motan使用zookeeper作为注册中心--> 48 <dependency> 49 <groupId>com.weibo</groupId> 50 <artifactId>motan-registry-zookeeper</artifactId> 51 <version>0.2.1</version> 52 </dependency> 53 54 <!--依赖关系--> 55 <dependency> 56 <groupId>com.motan.zookeper</groupId> 57 <artifactId>motan-zookeper-api</artifactId> 58 <version>1.0-SNAPSHOT</version> 59 <scope>compile</scope> 60 </dependency> 61 </dependencies> 62 </project>
3. 模块依赖关系图
4. 层说明
- motan-zookeper-api ——业务逻辑接口
- motan-zookeper-model ——实体类
- motan-zookeper-service ——数据访问层,业务逻辑层的实现
- motan-zookeper-web ——表现层
5. 各模块的结构
5.1 motan-zookeper-api
5.2 motan-zookeper-model
5.3 motan-zookeper-service
5.3.1. JpaConfig.java
1 package com.motan.zookeper.config; 2 3 import org.springframework.boot.autoconfigure.domain.EntityScan; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 6 7 @Configuration 8 @EntityScan(basePackages = {"com.motan.zookeper.model"}) 9 @EnableJpaRepositories(basePackages = {"com.motan.zookeper.repository"}) 10 public class JpaConfig { 11 }
5.3.2. MotanConfig.java
1 package com.motan.zookeper.config; 2 3 import com.weibo.api.motan.config.springsupport.*; 4 import org.springframework.beans.factory.annotation.Value; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 8 @Configuration 9 public class MotanConfig { 10 11 @Bean 12 public AnnotationBean motanAnnotationBean(@Value("${motan.annotation.package}") String packageName) { 13 AnnotationBean motanAnnotationBean = new AnnotationBean(); 14 motanAnnotationBean.setPackage(packageName); 15 return motanAnnotationBean; 16 } 17 18 @Bean(name = "motan") 19 public ProtocolConfigBean protocolConfig() { 20 ProtocolConfigBean config = new ProtocolConfigBean(); 21 config.setDefault(true); 22 config.setSerialization("hessian2"); 23 config.setName("motan"); 24 config.setMaxContentLength(1548576); 25 config.setRequestTimeout(50000000); 26 27 return config; 28 } 29 30 @Bean(name = "registry") 31 public RegistryConfigBean registryConfigSit(@Value("${motan.zookeeper.host}") String zookeeperAddress) { 32 RegistryConfigBean config = new RegistryConfigBean(); 33 config.setDefault(true); 34 config.setRegProtocol("zookeeper"); 35 config.setAddress(zookeeperAddress); 36 return config; 37 } 38 39 /** 40 * 服务端配置 41 */ 42 @Bean(name = "motanServerBasicConfig") 43 public BasicServiceConfigBean baseServiceConfig(@Value("${motan.export.port}") String port) { 44 BasicServiceConfigBean config = new BasicServiceConfigBean(); 45 config.setDefault(true); 46 config.setExport("motan:" + port); 47 config.setGroup("sinomall"); 48 config.setAccessLog(false); 49 config.setShareChannel(true); 50 config.setRequestTimeout(9000000); 51 config.setRegistry("registry"); 52 return config; 53 } 54 55 /** 56 * 客户端配置 57 */ 58 @Bean(name = "motanClientBasicConfig") 59 public BasicRefererConfigBean baseRefererConfig() { 60 BasicRefererConfigBean config = new BasicRefererConfigBean(); 61 config.setProtocol("motan"); 62 config.setGroup("sinomall"); 63 config.setAccessLog(false); 64 config.setRegistry("registry"); 65 config.setCheck(false); 66 config.setRetries(0); 67 config.setThrowException(true); 68 config.setRequestTimeout(9000); 69 config.setDefault(true); 70 return config; 71 } 72 }
5.3.3. MotanZookeperServiceApp.java
1 package com.motan.zookeper; 2 3 import com.weibo.api.motan.common.MotanConstants; 4 import com.weibo.api.motan.util.MotanSwitcherUtil; 5 import org.springframework.boot.SpringApplication; 6 import org.springframework.boot.autoconfigure.SpringBootApplication; 7 8 @SpringBootApplication 9 public class MotanZookeperServiceApp { 10 public static void main(String[] args) { 11 SpringApplication.run(MotanZookeperServiceApp.class,args); 12 //服务提供方需要开启心跳检测 13 MotanSwitcherUtil.setSwitcherValue(MotanConstants.REGISTRY_HEARTBEAT_SWITCHER, true); 14 System.out.println("服务端启动"); 15 } 16 }
5.3.4. application.properties
1 # 数据源配置 2 spring.datasource.url=jdbc:log4jdbc:mysql://127.0.0.1:3306/motan-zookeper?useUnicode=true&characterEncoding=utf-8 3 spring.datasource.username=root 4 spring.datasource.password=root 5 spring.datasource.driver-class-name=net.sf.log4jdbc.DriverSpy 6 # jpa配置 7 spring.jpa.database=mysql 8 spring.jpa.show-sql=true 9 spring.jpa.hibernate.ddl-auto=update 10 # sql 日志 11 logging.level.jdbc=error 12 logging.level.jdbc.sqltiming=info 13 # motan配置 14 motan.annotation.package=com.motan.zookeper 15 motan.export.port=${random.int[5001,9999]} 16 motan.zookeeper.host=127.0.0.1:2181
5.4. motan-zookeper-web
5.4.1. MotanConfig.java
1 package com.motan.zookeper.config; 2 3 import com.weibo.api.motan.config.springsupport.*; 4 import org.springframework.beans.factory.annotation.Value; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 8 @Configuration 9 public class MotanConfig { 10 11 @Bean 12 public AnnotationBean motanAnnotationBean(@Value("${motan.annotation.package}") String packageName) { 13 AnnotationBean motanAnnotationBean = new AnnotationBean(); 14 motanAnnotationBean.setPackage(packageName); 15 return motanAnnotationBean; 16 } 17 18 @Bean(name = "motan") 19 public ProtocolConfigBean protocolConfig() { 20 ProtocolConfigBean config = new ProtocolConfigBean(); 21 config.setDefault(true); 22 config.setSerialization("hessian2"); 23 config.setName("motan"); 24 config.setMaxContentLength(1548576); 25 config.setRequestTimeout(50000000); 26 27 return config; 28 } 29 30 @Bean(name = "registry") 31 public RegistryConfigBean registryConfigSit(@Value("${motan.zookeeper.host}") String zookeeperAddress) { 32 RegistryConfigBean config = new RegistryConfigBean(); 33 config.setDefault(true); 34 config.setRegProtocol("zookeeper"); 35 config.setAddress(zookeeperAddress); 36 return config; 37 } 38 39 /** 40 * 服务端配置 41 */ 42 @Bean(name = "motanServerBasicConfig") 43 public BasicServiceConfigBean baseServiceConfig(@Value("${motan.export.port}") String port) { 44 BasicServiceConfigBean config = new BasicServiceConfigBean(); 45 config.setDefault(true); 46 config.setExport("motan:" + port); 47 config.setGroup("sinomall"); 48 config.setAccessLog(false); 49 config.setShareChannel(true); 50 config.setRequestTimeout(9000000); 51 config.setRegistry("registry"); 52 return config; 53 } 54 55 /** 56 * 客户端配置 57 */ 58 @Bean(name = "motanClientBasicConfig") 59 public BasicRefererConfigBean baseRefererConfig() { 60 BasicRefererConfigBean config = new BasicRefererConfigBean(); 61 config.setProtocol("motan"); 62 config.setGroup("sinomall"); 63 config.setAccessLog(false); 64 config.setRegistry("registry"); 65 config.setCheck(false); 66 config.setRetries(0); 67 config.setThrowException(true); 68 config.setRequestTimeout(9000); 69 config.setDefault(true); 70 return config; 71 } 72 }
5.4.2. MotanZookeperWebApp
1 package com.motan.zookeper; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 @SpringBootApplication 7 public class MotanZookeperWebApp { 8 public static void main(String[] args) { 9 SpringApplication.run(MotanZookeperWebApp.class,args); 10 System.out.println("客户端启动"); 11 } 12 }
5.4.3. application.properties
1 server.port=7080 2 # motan配置 3 motan.annotation.package=com.motan.zookeper 4 motan.export.port=${random.int[5001,9999]} 5 motan.zookeeper.host=127.0.0.1:2181
6. 启动应用
1) 启动本地的zookeper服务
2) 启动MotanZookeperServiceApp
3) 启动MotanZookeperWebApp
以上是关于Motan-Zookeper分布式项目搭建的主要内容,如果未能解决你的问题,请参考以下文章