父工程依赖
<parent>
<groupId> org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
</parent>
<!--2.管理 SpringCloud的jar包,父项目只管理,不引用-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
Erueka服务端
依赖
<!--Eureka服务依赖,包含了Eureka客户端包、Spring-start-web包-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
Erueka服务端启动类以及yml
启动类添加@EnableEurekaServer //开启Eureka注册中心
//配置yml
server:
port: 8761 #Eureka服务端口号
eureka: # Eureka配置
instance:
hostname: localhost # Eureka服务名称
client: # Eureka客户端配置
registerWithEureka: false #该服务不注册到注册中心
fetchRegistry: false # 该服务不拉取注册表
serviceUrl: # 注册中心地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Erueka客户端
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Erueka客户端启动类以及yml
@EnableEurekaClient //表示此服务是Eureka客户端 开启Eureka客户端功能,不加此注解默认也开启客户端功能
//配置yml
server:
port: 10010
eureka:
client:
serviceUrl: # Eureka客户端配置 指向注册中心地址
defaultZone: http://localhost:8761/eureka/
instance: #配置实例名称
instance-id: user-server:10010
prefer-ip-address: true # 开启IP注册
spring:
application: # 配置服务名称
name: user-server
OpenFeign的使用
导入依赖
<!--Openfeign 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
启动类开启OpenFeign
/*
* 此注解会去扫描当前类同级以及以下所有包内类上加了@FeignClient注解的类
* 通过动态代理去创建一个实现类,实现类中的实现方法中会拿到公共路径+方法路径+请求方式+请求方式+服务名称
* 去帮你通过ribbon发送http请求调用
* */
@EnableFeignClients //开启openfeign
在feign包下创建一个feign接口
1.接口上加注解 @FeignClient(value ="服务名称")
2.找到要调用的方法,复制到feign接口中
3.然后通过controller调用
Hystrix的使用
yml中开启熔断支持
feign:
hystrix:
enabled: true #开启熔断支持
在注解 @FeignClient上添加属性 fallbackFactory = 跟托底类名一致.class
不要忘记在托底类上添加@Component注解把它交给Spring去管理
创建托底类然后实现FallbackFactory接口并指定泛型为Feign客户端接口 重写方法
下图托底类示例
Zuul
1.导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
2.启动类添加注解
@EnableZuulProxy //开启Zuul
zuul:
prefix: /pf #统一访问前缀
ignoredServices: "*" #禁用掉使用浏览器通过服务名的方式访问服务
routes:
user-server: /user/**
pay-server: /pay/**
order-server: /order/**
整合Mybatis必要依赖
<!-- SpringBoot-热部署包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- spring-boot整合mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
Mybatis的yml配置
数据源
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/pethome?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
mybatis:
#别名
type-aliases-package: com.pf.org.domain
#mapper 包扫描
mapper-locations: classpath:com/pf/*/mapper.xml
# 下划线转驼峰
configuration:
map-underscore-to-camel-case: true