SpringBoot由Pivotal小组开发和开源,Boot译为引导,SpringBoot的核心还是Spring,其意义是简化框架搭建(SpringBoot集成了大量开发库,例如spring-webmvc、jackson-json、validation-api和tomcat等),使码者废弃繁琐的模板化配置(比如:SSH和SSM架构候配置web.xml、加载spring和spring mvc、配置DataSource连接、配置spring事物,配置日志),更专注于业务和应用开发。
模块 | 说明 |
---|---|
sb-api | entity、dto、vo、enum、constants存储层 |
sb-orm | 数据模型映射持久化层-父级 |
sb-orm-test | 数据模型映射持久化层-业务粒度 |
sb-service | 微服务层-父级 |
sb-service-test | 微服务层-业务粒度 |
sb-support | 基础工具及组件层 |
sb-web | web层 |
文件名 | 模块 | 说明 |
---|---|---|
pom.xml | sb-service-test | 微服务提供者maven包依赖配置 |
TestServerApplication.java | sb-service-test | 微服务启动类 |
TestServiceImpl.java | sb-service-test | 微服务接口实现类及关键注解 |
application.properties | sb-service-test | 微服务核心文件,配置数据库+redis+线程池+MQ资源+指定日志文件及级别等等,可根据环境区分 |
pom.xm | sb-web | 服务消费者maven包依赖配置 |
StartWebApplication.java | sb-web | web服务启动类 |
TestController.java | sb-web | web服务及关键注解 |
application.yml | sb-web | web服务核心文件,配置系统+端口+服务注册地址,可根据环境区分,yml和properties文件SpringBoot都支持,只是yml是树形结构格式 |
<1>:微服务pom.xml文件(说明:已深度精简依赖)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo-spring-boot}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.starter.version}</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<2>:TestServerApplication.java文件
@MapperScan("com.wangze.sb.orm.test") 指定要扫描的Mapper类的包的路径
@SpringBootApplication(scanBasePackages = {"com.wangze.sb.test"})类包扫描路径
它包含三个重要注解:@ComponentScan,@EnableAutoConfiguration,@SpringBootConfiguration
Spring框架有四大注解:@Service,@Repository,@Component,@Controller
@ComponentScan作用是自动扫描启动类的包下或者子包下被注解标识的类,识别并加入到IOC容器
@EnableAutoConfiguratio作用是替代Spring框架中的@Configuration注解
@EnableAutoConfiguration作用时候启动自动配置
<3>:TestServiceImpl.java文件
注解名 | 作用解释 |
---|---|
@org.springframework.stereotype.Service | 属于Spring框架, 意指service层,用于标注业务层组件,还需结合xml配置dubbo:service interface等使用 |
@com.alibaba.dubbo.config.annotation.Service | 属于dubbo, 用来声明provider的注解,即声明该类是服务的提供者 |
@Controller | 属于Spring框架, 用于标注控制层组件(如struts中的action) |
@Repository | 属于Spring框架, 用于标注数据访问组件(即DAO组件),注意:如果系统配置自动扫描mapper接口,可省略 |
@Component | 属于Spring框架, 泛指组件 |
@Resource | 属于javaEE,非Spring框架,@Resource(“xxx”)定义bean名称,同时支持byType和byName两种方式进行装配依赖对象 |
@Autowired | 属于Spring框架, @Autowired(“xxx”)定义bean名称,支持按照类型(byType)装配依赖对象,可结合@Qualifier(“xxx”) 支持按名称装备 |
<4>:application.properties文件
logging.config=classpath:log4j2-dev.xml
## Dubbo \u670D\u52A1\u63D0\u4F9B\u8005\u914D\u7F6E
spring.dubbo.application.id=sb.default
spring.dubbo.application.name=sb.default
spring.dubbo.registries.sb.address=127.0.0.1:2181
spring.dubbo.registries.sb.id=sb
spring.dubbo.registries.sb.group=sb
spring.dubbo.registries.sb.protocol=zookeeper
spring.dubbo.registries.sb.file=/sch.cache
spring.dubbo.consumer.check=false
spring.dubbo.protocol.name=dubbo
spring.dubbo.protocol.port=-1
## Datasource
spring.datasource.name=cons
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/pms?zeroDateTimeBehavior=convertToNull
spring.datasource.username=root
spring.datasource.password=789123Bb
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.filters=log4j2,wall
spring.datasource.maxActive=20
spring.datasource.initialSize=1
spring.datasource.maxWait=60000
spring.datasource.minIdle=1
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=select 'x'
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxOpenPreparedStatements=20
#\u5F00\u542F\u9A7C\u5CF0\u8F6C\u6362
mybatis.configuration.mapUnderscoreToCamelCase=true
logging.level.com.wangze.orm=debug
<5>: web层pom.xml文件配置(说明:已深度精简依赖)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<6>: StartWebApplication.java配置
注解名 | 作用解释 |
---|---|
@EnableDubboConfiguration | 注意web层必须开启Dubbo龚配置 |
注解名 | 作用解释 |
---|---|
@RestController | @RestController注解相当于@ResponseBody+@Controller合在一起的作用 |
@Reference | 属于dubbo框架,标注消费者身份并开始 引用服务 |
<8>: application.yml配置
security:
basic:
enabled: false
user:
name: admin
password: admin
role: ADMIN
server:
context-path: /sb-web
port: 8888
spring:
profiles:
active:
- dev
# - test
# - sit
# - prod
logging:
config: classpath:log4j2.xml
区分环境时可以指定注册中心zookeeper地址,例如 application-dev.yml
spring:
dubbo:
application:
id: sb.default
name: sb.default
consumer:
check: false
timeout: 30000
protocol:
name: dubbo
port: -1
registries:
sb:
address: 127.0.0.1:2181
group: sb
id: sb
protocol: zookeeper
<1>: 首先启动注册中心zookeeper服务。
<2>: 再启动微服务提供者TestServerApplication.java服务。
<3>: 最后启动web消费者StartWebApplication.java服务。
<4>: 访问系统
<5>: 使用postman,jemter,idea restful,firefox HttpRequester等工具发送post请求看成果。
<1>: logback包冲突,原因是springboot自带的log4j与logback包冲突。
at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:161)
解决办法:
[1]微服务层:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.starter.version}</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
[2]web层:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
</exclusions>
</dependency>
<2>: slf4j-log4j12包冲突,原因是springboot自带的log4j与slf4j-log4j12包冲突。
java.lang.IllegalStateException: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError
解决办法:
[1]web层:
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<3>: spring包冲突,原因是springboot及duboo中自带的spring包与apache spring包冲突
java.lang.IllegalStateException: No persistence exception translators found in bean factory. Cannot perform exception translation.
解决办法:
[1]微服务层:
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${dubbo-spring-boot}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
[2]web层:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
[2]web层:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
</exclusions>
</dependency>
<4>: lease check registry access list (whitelist/blacklist)
Forbid consumer 10.26.190.89 access service com.wangze.sb.test.dubbo.TestService from registry 127.0.0.1:2181 use dubbo version 2.5.3, Please check registry access list (whitelist/blacklist)