0 前言
搭建一个简单的server,和一个服务提供者support,然后分别创建一个服务消费者:feign Consumer,一个服务消费者ribbon Consumer。
分别测试Hystrix效果。
1、简单的Server+Support
1.1 代码截图
1.2 父pom
详见《Spring Cloud配置中心(一)》-“3 父 pom文件,关键部分”。
1.3 server的pom
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
1.4 support 的 pom
<dependencies>
<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>
</dependencies>
1.5 EurekaServerApplication
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
1.6 EurekaServiceApplication
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServiceApplication.class,args);
}
}
1.7 server的application.yml
server:
port: 8700
eureka:
server:
enable-self-preservation: true # 自我保护模式(缺省为打开),正式环境不要这么做
eviction-interval-timer-in-ms: 1000 # 续期时间,即扫描失效服务的间隔时间(缺省为60*1000ms)
client:
service-url:
defaultZone: "http://${eureka.instance.hostname}:${server.port}/eureka/"
register-with-eureka: false # 自身不再向Eureka注册
fetch-registry: false # 启动时,禁用client注册:不禁用会报错
instance:
hostname: localhost
spring:
application:
name: eureka-server
1.8 support的application.yml
server:
port: 8701
eureka:
client:
service-url:
defaultZone: "http://${eureka.instance.hostname}:8700/eureka/"
instance:
hostname: localhost
spring:
application:
name: eureka-support # 服务实例名,注册及服务消费时均需使用该名称
1.9 support的controller
@RestController
public class Controller {
@RequestMapping("support/hello")
public String hello(String name) {
System.out.println("8701-hello:入参,name=" + name);
return "8701-hello:入参,name=" + name;
}
}
1.10 运行测试
先启动server,后启动support
http://localhost:8700/
- 浏览器截图: