SpringCloud实现负载均衡和熔断器

SpringCloud 是当前最流行的分布式框架之一 而负载均衡也是分布式的核心模块 虽然我没有做过分布式项目 但是学习新东西还是有必要的 就简单的做了一个负载均衡和熔断器

首先需要的是注册中心

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class BootCenter {

	public static void main(String[] args) {

		SpringApplication.run(BootCenter.class, args);
	}
}

以及注册中心的配置 这里我就简单的在application.配置了一下

server.port=8888
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.server.enable-self-preservation=false
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/

消费方–狗的配置

server.port=2333
spring.application.name=boot-orange
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/

feign.hystrix.enabled=true//需要手动开启熔断功能
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000//超时5秒熔断服务

消费方需要去实现一个接口

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dog.config.FallBack;

//name需要服务方的名单,,fallbackFactory指向一个实现熔断失败的接口  由我们自定义·实现
@FeignClient(name = "boot-apple", fallbackFactory = FallBack.class)
public interface DogImp {

	//注意 这个接口是调用服务方的接口  必须保证一致  所有服务方的接口必须和该接口一致  方法名同样必须一致
	@RequestMapping("/info/getData")
	public String getData();
}

熔断实现的接口 由于原服务方已经无法提供服务 所以需要我们自定义实现并返回接口对象

import org.springframework.stereotype.Component;
import com.dog.service.DogImp;
import feign.hystrix.FallbackFactory;

@Component
public class FallBack implements FallbackFactory<DogImp> {

	@Override
	public DogImp create(Throwable cause) {

		return new DogImp() {

			@Override
			public String getData() {
				return "服务繁忙";
			}
		};
	}
}

接下来是提供服务的服务方–鸭
上面需要服务方的名单就是这个: spring.application.name=boot-apple
所以必须保持一致

server.port=2335
spring.application.name=boot-apple
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
feign.hystrix.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000//熔断时间设置为5秒

服务方的实现就比较简单了

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.duck.entity.User;

@Controller
@RequestMapping("/info")
public class DuckController {

	@RequestMapping("/getData")
	@ResponseBody
	public String getData(@RequestBody User u) {
		
		// try {
		// Thread.sleep(5000);
		// } catch (Exception e) {
		// }
		System.out.println("调用duck服务");
		return "duck";
	}
}

服务方–猫 同理 这个也必须和上面的服务方名单保持一致 spring.application.name=boot-apple

server.port=2334
spring.application.name=boot-apple
eureka.client.serviceUrl.defaultZone=http://localhost:8888/eureka/
feign.hystrix.enabled=true
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000

mport org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/info")
public class CatController {

	@RequestMapping("/getData")
	@ResponseBody
	public String getData() {

		// try {
		// Thread.sleep(1000);
		// } catch (Exception e) {
		//
		// }
		System.out.println("调用cat服务");
		return "cat";
	}

}

调用服务

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.dog.service.DogImp;

@Controller
public class DogController {

	@Autowired
	private DogImp dogImp;

	@RequestMapping("/dog")
	@ResponseBody
	public String dog() {

		return dogImp.getData();
	}

}

结果发现结果是一次 调用cat服务,一次 服务繁忙 。 说明鸭提供的服务被熔断了 结果发现是鸭方提供的接口多了一个注解

在这里插入图片描述在这里插入图片描述
在这里插入图片描述
重新开启一次 可以发现这几次都实现了轮训的访问服务方
在这里插入图片描述在这里插入图片描述
实现熔断接口 就要让服务器响应时间超过熔断器设定的时间 把鸭方服务器sleep 5秒
可以发现 鸭方服务熔断之后就不在访问鸭方服务 而是每一个请求都去访问猫服务
熔断实现
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx是一种高性能的开源Web服务器和反向代理服务器,具有快速、轻量级、可扩展性强等特点。nginx的负载均衡功能是通过将请求分配到多个服务器节点上来实现的。nginx可以对请求进行多种方式的负载均衡,包括:轮询、IP Hash、least_conn、fair等等。nginx负载均衡的优点在于其高性能和稳定性,能够有效地提高应用程序的吞吐量和响应速度。 Spring Cloud是一套基于Spring Boot的分布式系统开发工具,具有服务注册与发现、服务调用、负载均衡熔断器、配置管理等功能。Spring Cloud的负载均衡功能是通过Ribbon实现的,在服务调用时通过Ribbon发起请求,Ribbon会根据预定义的负载均衡算法将请求分配到不同的服务器节点上。Spring Cloud支持的负载均衡算法包括:ZoneAvoidanceRule、RandomRule、RoundRobinRule、WeightedResponseTimeRule等。Spring Cloud的负载均衡功能具有简单易用、与Spring Boot集成度高等优点,同时也能有效地提高应用程序的负载能力和可用性。 总的来说,nginx和Spring Cloud都是具有负载均衡功能的工具,选择哪种工具取决于应用场景和需求。如果侧重于高性能和稳定性,可以选择用nginx进行负载均衡;如果侧重于简单易用和集成度高,可以选择使用Spring Cloud进行负载均衡。当然,两者也可以结合使用,在需要高性能和负载均衡时选择nginx,在需要简单易用和服务注册与发现时选择Spring Cloud,以达到更好的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值