ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringCloud学习笔记2

2022-01-19 11:31:39  阅读:168  来源: 互联网

标签:zuul String SpringCloud 笔记 class 学习 ratelimit spring public


一、Ribbon实现客户端的负载均衡【只支持2.3版本即以下、过渡性知识点】

Ribbon:一个服务集群多个、Ribbon来决定选择调用哪一个(负载均衡)、使用了Feign就不用使用Ribbon了

1、创建项目选择以下组件:SpringBoot DevTools、Eureka Discovery Client、Ribbon、Spring Web

2、添加配置信息

# 设置服务端口
server.port=8881
#  设置服务名 可以相同、相同则为后面的负载均衡准备
spring.application.name=hello-service
#  设置注册中心地址
eureka.client.service-url.defaultZone=http://localhost:7777/eureka

3、在启动类配置RestTemplate

//@EnableDiscoveryClient向服务中心注册,并且注册了一个叫restTemplate的bean。
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceRibbonApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceRibbonApplication.class, args);
    }
    
    //@LoadBalanced注册表明,这个restRemplate是需要做负载均衡的。
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

4、Controller使用RestTemplate调用其他服务实现负载均衡

@RestController
@RequestMapping("/classes")
public class ClassesController {
	//restTemplate对象自带负载均衡的调用客户端对象
    @Autowired RestTemplate restTemplate;
	
	@RequestMapping("/index001")
	public String index001() {
		return "client002-classes-index001"+restTemplate.getForObject("http://service001/student/index001",String.class);
	}
}

二、Hystrix断路器(熔断)

1、Hystrix介绍:有3个服务、当Feign在三个中选一个时、假如选得2号、但2号这时候恰好宕机了、此时就属于断路

是什么?hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与hystrix本身的功能不谋而合,因此Netflix团队将该框架命名为Hystrix,并使用了对应的卡通形象做作为logo。

为什么要使用?在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。

2、添加依赖jar包

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-hystrix</artifactId>
	<version>1.4.0.RELEASE</version>
</dependency>

3、Controller配置断熔

@RestController
@RequestMapping("/hystrix")
public class HystrixController {
	//restTemplate对象自带负载均衡的调用客户端对象、或者直接使用Feign
    @Autowired RestTemplate restTemplate;

    @RequestMapping("/test")
    //当请求的地址出现错误、执行error方法、如果test执行超过2秒或抛出异常皆会被断路
    @HystrixCommand(fallbackMethod = "error")
    public String test(String name) throws Exception{
    	if (1==1)
    		throw new Exception("error");
    	 return restTemplate.postForObject("http://server001/student/method002",null,String.class);
    }
    //异常也会被断路
    public String error(String name,Throwable e) {
    	return e.getMessage()+name;
    }
}

4、启动类添加断熔注解

@SpringBootApplication
@EnableDiscoveryClient
//允许断路器
@EnableCircuitBreaker
public class RibbonApplication {
    public static void main(String[] args) {
        SpringApplication.run(RibbonApplication.class, args);
    }
}

三、Zuul网关配置、新版本请使用gateway

1、作用:反向代理+负载均衡+限流

限流需要单独添加jar包


2、添加依赖jar包

	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<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-zuul</artifactId>
		</dependency>
<!--限流的jar包、需要手动导入-->
	<dependency>
		<groupId>com.marcosbarbero.cloud</groupId>
		<artifactId>spring-cloud-zuul-ratelimit</artifactId>
		<version>2.0.0.RELEASE</version>
	</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>

3、配置网关

#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:7777/eureka/
#服务端口
server.port=8888
#我在注册中心的网关名称
spring.application.name=zuul001


# 如:server001有个服务:http://localhost:8001/student/method001
# 配置下面网关后用户访问 http://localhost:8888/zuul/student/method001、和上面的路径一摸一样
# 网关配置 zuul.routes.任意名[亦可位服务名].path:、如果service-id相同则带负载均衡1
zuul.routes.abc.path= /abc/**
zuul.routes.abc.service-id= CLIENT002
# 上面可简写zuul.routes.servere001.path=/aaa/**	第一个字不能是zuul
# 如果是zuul.routes.servere001= 等同于 zuul.routes.clie.path=/servere001/**

## 开启限流、针对单个服务
zuul.ratelimit.enabled=true
# 60秒内请求超过3次、服务端就抛出异常、60s后可以恢复正常请求
zuul.ratelimit.policies.abc.limit=3
zuul.ratelimit.policies.abc.refresh-interval=60
## 针对某个IP进行限流、不影响其他IP
## zuul.ratelimit.policies.abc.type=ip地址


# 开启全局限流
# zuul.ratelimit.enabled=true
# zuul.ratelimit.default-policy.limit=3
# zuul.ratelimit.default-policy.refresh-interval=60
# zuul.ratelimit.default-policy.type=origin

4、启动类添加注解

@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class SpringcloudzuulApplication {
	public static void main(String[] args) {
		SpringApplication.run(SpringcloudzuulApplication.class, args);
	}
}

标签:zuul,String,SpringCloud,笔记,class,学习,ratelimit,spring,public
来源: https://www.cnblogs.com/xiaojizha/p/15821567.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有