小鸡炸

导航

SpringCloud学习笔记3

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

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

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网关配置

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

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>
		<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相同则带负载均衡
zuul.routes.a.path= /aaa/**
zuul.routes.a.service-id= server001
#上面可简写zuul.routes.servere001.path=/aaa/**	第一个字不能是zuul
#如果是zuul.routes.servere001= 等同于 zuul.routes.servere001.path=/servere001/**

4、启动类添加注解

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

posted on 2022-01-19 11:27  小鸡炸  阅读(33)  评论(0编辑  收藏  举报