01-service-ribbon客户端负载均衡
01-eureka-server
01-service-ribbon
pom.xml
eureka-client:eureka客户端
hystrix:接口熔断
ribbon:客户端负载均衡
<parent>
<groupId>com.sky</groupId>
<artifactId>chapter1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>chapter2-service-ribbon</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
application.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@RestController
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
@Autowired
HelloService helloService;
@GetMapping(value = "/hi")
public String hi(@RequestParam String name) {
return helloService.hiService(name);
}
}
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "hiError")
public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name=" + name, String.class);
}
public String hiError(String name) {
return "hi," + name + ",sorry,error!";
}
}
测试1
先启动chapter1-eureka-server
再启动chapter1-service-hi(端口8762)修改端口后再启动一次(8763)
再启动service-ribbon(8764)
访问:http://localhost:8761/
看看eureka的服务注册情况
访问:http://ip:8764/hi?name=hello
交替访问8762和8763端口
测试2
在测试1的基础上停掉8762端口和8763端口
访问:http://ip:8764/hi?name=hello