项目的基本结构及功能如下图所示,这里使用maven构建项目,然后手动导入依赖,不使用Spring Initializr快速创建是为了让项目没有那没多花里胡哨的依赖,自己添加更容易管理
下面是各个模块的pom.xml和application.yml(因为是初始工程,所以有些模块没有引入依赖和规定一些配置,因此就不一一列出)
leyou (父工程)
pom.xml
4.0.0
com.leyou.parent
leyou
1.0.0-SNAPSHOT
leyou-registry
leyou-registry
leyou-gateway
leyou-item
leyou-common
pom
leyou
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
2.0.6.RELEASE
UTF-8
UTF-8
1.8
Finchley.SR2
1.3.2
2.0.2
1.1.9
5.1.32
1.2.3
1.0.0-SNAPSHOT
1.26.1-RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis.starter.version}
tk.mybatis
mapper-spring-boot-starter
${mapper.starter.version}
com.github.pagehelper
pagehelper-spring-boot-starter
${pageHelper.starter.version}
mysql
mysql-connector-java
${mysql.version}
com.github.tobato
fastdfs-client
${fastDFS.client.version}
org.springframework.boot
spring-boot-maven-plugin
leyou-registry
pom.xml
leyou
com.leyou.parent
1.0.0-SNAPSHOT
4.0.0
com.leyou.registry
leyou-registry
1.0.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
application.yml
server:
port: 10086 #访问端口
spring:
application:
name: leyou-registry #项目名
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
register-with-eureka: false #关闭 将自己注册到服务中心
fetch-registry: false # 关闭自动拉取服务
server:
enable-self-preservation: false # 关闭自我保护
eviction-interval-timer-in-ms: 5000 # 每隔5秒进行一次服务列表清理
leyou-item-service
pom.xml
leyou-item
com.leyou.item
1.0.0-SNAPSHOT
4.0.0
com.leyou.item.service
leyou-item-service
1.0.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.mybatis.spring.boot
mybatis-spring-boot-starter
tk.mybatis
mapper-spring-boot-starter
com.github.pagehelper
pagehelper-spring-boot-starter
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
com.leyou.item.interface
leyou-item-interface
1.0.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-actuator
application.yml
server:
port: 8081
spring:
application:
name: leyou-item-service
datasource:
url: jdbc:mysql://localhost:3306/leyou
username: root
password: 0611
hikari:
max-lifetime: 28830000 # 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQL wait_timeout参数(show variables like '%timeout%';)
maximum-pool-size: 9 # 连接池中允许的最大连接数。缺省值:10;推荐的公式:((core_count * 2) + effective_spindle_count)
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
instance:
lease-renewal-interval-in-seconds: 5 # 5秒钟发送一次心跳
lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期
leyou-gateway
pom.xml
leyou
com.leyou.parent
1.0.0-SNAPSHOT
4.0.0
com.leyou.gateway
leyou-gateway
1.0.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-zuul
org.springframework.boot
spring-boot-starter-actuator
application.yml
server:
port: 10010
spring:
application:
name: leyou-gateway
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
registry-fetch-interval-seconds: 5 # 拉取服务的间隔时间
zuul:
prefix: /api # 注意:前面有个斜杠
routes:
leyou-item-service: /item/**
测试
运行
访问:localhost:10086 (服务注册中心)
鼠标放在链接上面,浏览器底部会弹出一个地址,因为我们导入了这个依赖,这就是actuator提供的接口
org.springframework.boot
spring-boot-starter-actuator
我们点击访问:
因为我们没有添加信息,所以是一个空的json,但是可以肯定的是:我们能够访问到item-service了。
接下来我们通过路由访问试试,根据路由规则,我们需要访问的地址是:
http://localhost:10010/api/item/actuator/info