01-service-ribbon客户端负载均衡

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在这里插入图片描述

发布了95 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_26264237/article/details/103499778