SpringBoot
- 使用Spring Initializr创建工程,勾选Spring Web
- 导入依赖
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.20version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<scope>testscope>
dependency>
- application.properties配置文件改为application.yml
server:
port: 8080
Mybatis-Plus
- 导入依赖
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.2version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.1.1version>
dependency>
- 编写配置文件application.yml
mybatis:
mapper-locations=classpath: mappers/*xml
type-aliases-package: com.rainbow.entity
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id type: ASSIGN_ID
- 配置mapper、service、serviceImpl
- mapper:创建接口继承BaseMapper;该接口用@Mapper注解
- service:创建接口继承IService
- serviceImpl:创建类实现service接口;继承ServiceImpl;用@Service注解该类
Mysql
- 导入依赖
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.16version>
dependency>
- 配置文件application.yml
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/DatabaseName
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
Redis
- 需要在服务器上启动Redis服务
- 在Java中引入依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-pool2artifactId>
dependency>
- 配置文件application.yml中配置redis
spring:
redis:
host: 192.168.133.128
port: 6379
password: root
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 100ms
- 注入RedisTemplate
@Autowired
private RedisTemplate redisTemplate
Nacos
- 服务器启动Nacos
- java导入依赖
- 父工程引入Alibaba的SpringCloud依赖
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.2.6.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
- 在配置文件application.yml中配置nacos
spring:
cloud:
nacos:
server-addr: localhost:8848
Feign
- 引入依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
- 在服务消费者的启动类上添加注解
@EnableFeignClients
开启Feign的功能
- 在服务消费者的包下定义接口,加上服务提供者的接口
- 使用该接口实现远程调用
网关Gateway
- 引入依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-gatewayartifactId>
dependency>
- 编写基本配置
server:
port: 10010
spring:
application:
name: gateway
cloud:
nacos:
server-addr: localhost:8848
gateway:
routes:
- id: user-service
uri: lb://userservice
predicates:
- Path=/user/**
RabbitMQ
- 服务器启动RabbitMQ
- 引入依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-amqpartifactId>
dependency>
- 配置
spring:
rabbitmq:
host: 192.168.133.128
port: 5672
virtual-host: /
username: root
password: root
- 注入
RabbitTemplate
@Autowired
private RabbitTemplate rabbitTemplate
- 服务提供者
rabbitTemplate.convertAndSend
- 服务提供者
@RabbitListener
Elasticsearch
- 导入依赖
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
dependency>
- 初始化代码
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.133.128:9200")
));