1.使用Maven导入依赖,这样就能简洁点,防止导入无关的依赖导致依赖库繁杂:
4.0.0
com.leyou.parent
leyou
1.0.0-SNAPSHOT
leyou-registry
leyou-gateway
leyou-item
leyou
Demo project for Spring Boot
pom
org.springframework.boot
spring-boot-starter-parent
2.0.6.RELEASE
UTF-8
UTF-8
1.8
Finchley.SR2
1.3.2
2.0.3
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
2.搭建Eureka注册中心:
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
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: 10000 #定时清理无效链接
package com.leyou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class LeyouRegistryApplication {
public static void main(String[] args){
SpringApplication.run(LeyouRegistryApplication.class);
}
}
至此,eureka注册中心搭建完毕。
3.搭建zuul网关:
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
server:
port: 10010
spring:
application:
name: leyou-gateway
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka #把zuul注入eurela
registry-fetch-interval-seconds: 5 #每隔5秒拉取服务
zuul:
prefix: /api
package com.leyou;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableDiscoveryClient
@EnableZuulProxy
public class LeyouGatewayApplication {
public static void main(String[] args){
SpringApplication.run(LeyouGatewayApplication.class);
}
}
leyou
com.leyou.parent
1.0.0-SNAPSHOT
4.0.0
com.leyou.item
leyou-item
1.0.0-SNAPSHOT
pom
leyou-item
com.leyou.item
1.0.0-SNAPSHOT
4.0.0
com.leyou.item
leyou-item-service
1.0.0-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-jdbc
org.mybatis.spring.boot
mybatis-spring-boot-starter
mysql
mysql-connector-java
tk.mybatis
mapper-spring-boot-starter
com.github.pagehelper
pagehelper-spring-boot-starter
com.leyou.item
leyou-item-interface
1.0.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-maven-plugin
6.配置leyou-item-service模块的application.yml文件:
server:
port: 8081
spring:
application:
name: item-service
datasource:
url: jdbc:mysql:///leyou
username: root
password: 123456
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka
instance:
lease-expiration-duration-in-seconds: 15 #过期时间15s
mybatis:
type-aliases-package: com.leyou.item.pojo #mybatis包扫描路径
org.springframework.boot
spring-boot-starter-actuator
8.配置zuul网关的application.yml:
server:
port: 10010
spring:
application:
name: leyou-gateway
eureka:
client:
service-url:
defaultZone: http://localhost:10086/eureka #把zuul注入eurela
registry-fetch-interval-seconds: 5 #每隔5秒拉取服务
zuul:
prefix: /api
routes:
item-service: /item/** #路由到商品的微服务
9.启动eureka、zuul、item-service模块: