什么是SpringCloud:
在SpringBoot的基础上构建的微服务架构。
1.引入组件启动器
2.覆盖默认配置
3.在引导类上添加相应的注解
eureka组件
注册中心:服务的注册与发现
服务端:
1.引入服务端的启动器 eureka-server
2.添加配置:
spring.application.name服务名
eureka.client.service-url.defaultZone :http://localhost:xxx/eureka
eureka.server.eviction-interval-timer-in-ms:剔除无效连接时间
eureka.server.enable-self-preservation:关闭自我保护
3.@EnableEurekaServer 开启eureka服务功能
客户端:
1.引入启动器:eureka-client
2.添加配置:
spring.application.name 客户端名
eureka.client.service-url.defaultZone :http://localhost:xxx/eureka
eureka.instance.lease-renewal-interval-in-seconds:心跳时间
eureka.instance.lease-expiration-duration-in-seconds:过期时间
eureka.client.register-with-eureka:true/false(默认是true,是否把自己注册给eureka.)
eureka.client.fetch-registy:true/false(默认是true,是否拉取服务列表)
eureka.client.registy-interval-seconds:拉去服务的间隔时间
3.@EnableDiscoveryClient:启用Eureka客户端
ribbon组件
注册中心
覆盖默认配置
server:
port: 10087
spring:
application:
name: xxxx-registry
eureka:
client:
service-url:
defaultZone: http://localhost:10087/eureka/
service:
enable-self-preservation: false #关闭自我保护模式
eviction-interval-timer-in-ms: 10000 #每隔10秒钟,进行一次服务列表的清理
启动类相关注解
@SpringBootApplication
@EnableEurekaServer
网关注册
覆盖默认配置
server:
port: 10010
spring:
application:
name: xxx-gateway
eureka:
client:
service-url:
defaultZone: http://localhost:10087/eureka
registry-fetch-interval-seconds: 5
zuul:
prefix: /api
routes:
item-service: /item/** #路由到商品微服务
启动类相关注解
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
相关业务处理的其中一个模块
覆盖默认配置
server:
port: 8081
spring:
application:
name: item-service
datasource:
url: jdbc:mysql:///haha
username: root
password: root
eureka:
client:
service-url:
defaultZone: http://localhost:10087/eureka
instance:
lease-renewal-interval-in-seconds: 5 #心跳时间
lease-expiration-duration-in-seconds: 15 #过期时间
mybatis:
type-aliases-package: com.xxx.item.pojo #pojo包存放于接口模块 ,pom引入接口模块依赖
启动类相关注解
@SpringBootApplication
@EnableDiscoveryClient