作用:
提供者hystrix-provider-payment8005
1.新建hystrix-provider-payment8005
2.pom
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>cn.chen.demogroupId>
<artifactId>api-commonartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
3.yml
server:
port: 8005 # 端口号
spring:
# 应用名称
application:
name: hystrix-payment-service # 服务名称
# 数据源
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 数据库操作类型
driver-class-name: org.gjt.mm.mysql.Driver # 数据库驱动
#url: jdbc:mysql://192.168.221.129:3306/demo2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
url: jdbc:mysql://localhost:3306/demo2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
# 是否支持热部署
devtools:
restart:
enabled: true
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka # 单机版
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版
instance:
instance-id: payment8005
prefer-ip-address: true
# eureka客户端想服务端发动心跳的时间间隔,单位为秒(默认是30秒)。开发的时候可以设置小一些,以保证服务关闭后注册中心及时剔除服务
lease-renewal-interval-in-seconds: 1
# eureka服务端在收到最后一次心跳后等待时间上限,单位为秒(默认是90秒)。开发时候设置小一些
lease-expiration-duration-in-seconds: 2
mybatis:
mapperLocations: classpath:mapper/*.xml #mapper文件
type-aliases-package: cn.chen.cloud.entity #所有entity别名所在包
4.主启动
@SpringBootApplication
@EnableEurekaClient
public class HystrixPaymentApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixPaymentApplication.class,args);
}
}
5.业务类
1)service
@Service
public class PaymentService {
/**
* @Description 模拟访问成功情况
**/
public String IsOk(){
return "线程池:"+Thread.currentThread().getName()+",IsOk。";
}
/**
* @Description 模拟访问失败情况
**/
public String IsTimeOut(int timeNumber){
try {
TimeUnit.SECONDS.sleep(timeNumber);
}catch (Exception e) {
e.printStackTrace();
}
return "线程池:"+Thread.currentThread().getName()+",IsTimeOut,耗时(秒)"+timeNumber;
}
}
2)controller
@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {
@Resource
private PaymentService paymentService;
@GetMapping("/hystrix/ok")
public String ok(){
String result = paymentService.IsOk();
log.info("*******result:"+result);
return result;
}
@GetMapping("/hystrix/timeout")
public String timeOut(){
String result = paymentService.IsTimeOut(3);
log.info("*******result:"+result);
return result;
}
}
6.启动7001(单机版)
启动8005(Hystrix)
消费者hystrix-consumer-order80
1.新建hystrix-consumer-order80
2.pom依赖(参考consumer-order80)
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>cn.chen.demogroupId>
<artifactId>api-commonartifactId>
<version>${project.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<scope>runtimescope>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
3.yml配置(参考consumer-order80)
server:
port: 80
spring:
application:
name: hystrix-order-consumer
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka # 单机版
#defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
instance:
instance-id: order80
prefer-ip-address: true
payment:
#url: http://localhost:8001
server:
name: http://payment-service
4.主启动类
@SpringBootApplication
@EnableFeignClients
public class HystrixOrderApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixOrderApplication.class,args);
}
}
springcloud学习系列目录