Spring boot and Spring clound microservice project construction process
Article Directory
- Preface: This blog post is used to record the construction process of Spring boot and Spring clound projects, including simple database query, reverse engineering, and common pom dependencies. Completely configure the Spring clound project microservice project.
Note: Please install the module after importing pom dependencies.
1. Create the parent parent to provide the basic jar package
- Create maven moudule
- Specify the parent type to be the pom type, and add related dependency
parent dependency one (1)
2. Create common to provide public jar packages, entity classes, tools, etc. for the project
-
Create maven type module
-
Add parent class dependency reference and project need jar package
common dependency one (2) -
Add the reverse engineering configuration file generatorConfig.xml generatorConfig.xml under resources
Two (1)
-
Create a mappers directory under resources to store mapper.xml.
-
Run reverse engineering as shown in the figure to get mapper, pojo, mappers
-
Add the required tools, such as
Three, create a server to provide spring cloud services
-
Create spring boot module
-
Add pom dependency and introduce the parent package
server dependency one (3) -
Run the main class to add @EnableEurekaServer annotation
-
Configure application.properties service
#端口
server.port=8000
#application.name
spring.application.name=ttl_server
#通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.
eureka.instance.hostname=localhost
#registerWithEureka表示是否注册自身到eureka服务器
eureka.client.register-with-eureka=false
#fetchRegistry表示是否从eureka服务器获取注册信息。
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
Fourth, create api to write project source code
-
Create a spring boot module (the steps are the same as the server creation)
-
Add pom and introduce common as a jar package into
api dependency one (4) -
Run the main class to add client annotations
@EnableEurekaClient
@EnableFeignClients
@ComponentScan("com.zhiyou")
@MapperScan("com.zhiyou.mapper")
- Configure application.properties client
#设置客户端
server.port=8001
spring.application.name=ttl_api
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/
#连接数据库
spring.datasource.url=jdbc:mysql://120.27.244.176:3306/hospital?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=HBQ521521cf*
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#设置mapper.xml文件扫描位置
mybatis.mapper-locations=classpath:/mappers/*.xml
#给实体类包起别名
mybatis.type-aliases-package=com.zhiyou.pojo
Five, test program operation
- Write a test program, query the contents of the table
- controller
- serviceImpl
- Start the server first, then start the api.
- Browser input "http://localhost:8001/found.action" test