搭建三个工程eureka-server,hello-service,hystrix-service。
eureka-server搭建,pom.xml文件如下:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.8.RELEASE
com.hangzhou.springcloud
eureka-server
0.0.1-SNAPSHOT
eureka-server
Demo project for Spring Boot
1.8
Hoxton.SR6
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
application.properties文件如下:
# 指定运行端口号
server.port=1111
# 指定服务名称
spring.applcation.name=eureka-server
# 指定本机地址
eureka.instance.hostname=localhost
# 指定是否将服务注册到注册中心(注册中心不需要开启)
eureka.client.register-with-eureka=false
# 指定是否从注册中心获取服务(注册中心不需要开启)
eureka.client.fetch-registry=false
程序启动类,编写如下:
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
自此注册中心搭建完毕。
hello-service搭建开始,pom.xml文件如下:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.8.RELEASE
com.hangzhou.springcloud
hello-service
0.0.1-SNAPSHOT
hello-service
Demo project for Spring Boot
1.8
Hoxton.SR6
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
application.properties文件如下:
# 指定运行端口号
server.port=1112
# 指定服务名称
spring.application.name=hello-service
# 指定本机地址
eureka.instance.hostname=localhost
# 指定是否将服务注册到注册中心(注册中心不需要开启)
eureka.client.register-with-eureka=true
# 指定是否从注册中心获取服务(注册中心不需要开启)
eureka.client.fetch-registry=true
# 配置注册中心地址
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
程序启动类,如下所示:
@EnableDiscoveryClient
@SpringBootApplication
public class HelloServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HelloServiceApplication.class, args);
}
}
controller类中,加入睡眠方便实现服务降级效果。
@RequestMapping("/test")
@RestController
public class HelloController {
@GetMapping(value="/hello")
public String hello() throws InterruptedException {
System.out.println("进入helloword");
new Thread().sleep(5000);
return "hello word";
}
}
自此服务提供者搭建完毕,下面我们来搭建hystrix.service,pom.xml文件如下:
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.8.RELEASE
com.hangzhou.springcloud
hystrix-service
0.0.1-SNAPSHOT
hystrix-service
Demo project for Spring Boot
1.8
Hoxton.SR6
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.cloud
spring-cloud-starter-netflix-hystrix
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
application.properties文件如下:
# 指定运行端口号
server.port=1116
# 服务名称
spring.application.name=hystrix-service
eureka.client.fetch-registry=true
eureka.client.register-with-eureka=true
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
程序启动类代码如下:
@EnableCircuitBreaker
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class HystrixServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixServiceApplication.class, args);
}
}
feign客户端调用工具如下:
//此value值与服务提供者的spring.application.name的值是一致的
@FeignClient(value = "hello-service")
public interface HelloService {
//服务提供者的全路径访问(不包括http://localhost:1112)
@GetMapping(value="/test/hello")
public String hello();
}
controller类如下所示:
@RestController
@RequestMapping("/hystrix")
public class HystrixController {
@Autowired
private HelloService helloService;
@RequestMapping("/hello")
@HystrixCommand(fallbackMethod = "testHello")
public String hello() {
return helloService.hello();
}
public String testHello() {
return "服务调用不通过,服务降级";
}
}
搭建完毕,依次启动项目eureka-server,hello-servic,hystrix-service目录,游览器输入http://localhost:1116/hystrix/hello,效果如下图所示: