1. 新建maven项目motan-zookeper及module
2. 添加模块之间的依赖
2.1 motan-zookeper.pom
1 25 4.0.0 6 7com.motan.zookeper 8motan-zookeper 9pom 101.0-SNAPSHOT 1112 17 18motan-zookeper-api 13motan-zookeper-model 14motan-zookeper-service 15motan-zookeper-web 1619 23org.springframework.boot 20spring-boot-starter-parent 211.5.7.RELEASE 2224 30 3125 29org.projectlombok 26lombok 271.16.18 2832 49 5033 4834 47org.springframework.boot 35spring-boot-maven-plugin 3637 4338 4239 41repackage 4044 46true 45
2.2 motan-zookeper-api.pom
1 25 6 10motan-zookeper 7com.motan.zookeper 81.0-SNAPSHOT 94.0.0 11 12motan-zookeper-api 13 14
2.3 motan-zookeper-model.pom
1 25 6 10motan-zookeper 7com.motan.zookeper 81.0-SNAPSHOT 94.0.0 11 12motan-zookeper-model 13 1415 2016 19org.springframework.boot 17spring-boot-starter-data-jpa 18
2.4 motan-zookeper-service.pom
1 25 6 10motan-zookeper 7com.motan.zookeper 81.0-SNAPSHOT 94.0.0 11 12motan-zookeper-service 13 1415 16 8517 20 21org.springframework.boot 18spring-boot-starter 1922 25 26org.springframework.boot 23spring-boot-starter-actuator 2427 30 31org.springframework.boot 28spring-boot-starter-data-jpa 2932 35 36mysql 33mysql-connector-java 3437 41 42com.googlecode.log4jdbc 38log4jdbc 391.2 4043 47 48com.weibo 44motan-core 450.2.2 4649 53 54com.weibo 50motan-transport-netty 510.2.2 5255 59 60com.weibo 56motan-springsupport 570.2.1 5861 65com.weibo 62motan-registry-zookeeper 630.2.1 6466 70 71 72ma.glasnost.orika 67orika-core 681.5.1 6973 78com.motan.zookeper 74motan-zookeper-api 751.0-SNAPSHOT 76compile 7779 84com.motan.zookeper 80motan-zookeper-model 811.0-SNAPSHOT 82compile 83
2.5 motan-zookeper-web.pom
1 25 6 10motan-zookeper 7com.motan.zookeper 81.0-SNAPSHOT 94.0.0 11 12motan-zookeper-web 13 1415 16 6217 20 21org.springframework.boot 18spring-boot-starter-web 1922 25org.springframework.boot 23spring-boot-starter-actuator 2426 29 30org.springframework.boot 27spring-boot-starter-freemarker 2831 35 36com.weibo 32motan-core 330.2.2 3437 41 42com.weibo 38motan-transport-netty 390.2.2 4043 47 48com.weibo 44motan-springsupport 450.2.1 4649 53 54 55com.weibo 50motan-registry-zookeeper 510.2.1 5256 61com.motan.zookeper 57motan-zookeper-api 581.0-SNAPSHOT 59compile 60
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 @Bean12 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=update10 # sql 日志11 logging.level.jdbc=error12 logging.level.jdbc.sqltiming=info13 # motan配置14 motan.annotation.package=com.motan.zookeper15 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 @Bean12 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=70802 # motan配置3 motan.annotation.package=com.motan.zookeper4 motan.export.port=${random.int[5001,9999]}5 motan.zookeeper.host=127.0.0.1:2181
6. 启动应用
1) 启动本地的zookeper服务
2) 启动MotanZookeperServiceApp
3) 启动MotanZookeperWebApp