微服务注册中心Nacos以及负载均衡实战
一、前言
提示:本文讲到的代码部分来自上文
上文链接地址
二、微服务注册中心Nacos
1、介绍
服务注册:服务提供者provider,启动的时候向注册中心上报自己的网络信息
服务发现:服务消费者consumer,启动的时候向注册中心上报自己的网络信息,拉取provider的相关网络信息
核心:服务管理,是有个服务注册表,心跳机制动态维护,服务实例在启动时注册到服务注册表,并在关闭时注销。
2、下载地址
3、Linux/Mac安装Nacos
解压安装包
进入bin目录
启动 sh startup.sh -m standalone
关闭 sh shutdown.sh
访问 localhost:8848/nacos
默认账号密码 nacos/nacos
三、项目集成Nacos实现服务直接的调用
1、添加依赖
<!--添加nacos客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2、配置Nacos地址
server:
port: 8000
spring:
application:
name: spring-other1 #服务的名称
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
3、启动类增加注解
@EnableDiscoveryClient //注入到注册中心
4、服务之间的调用(可通过随机数实现负载均衡)
@RestController
@RequestMapping("other2")
public class OtherController2 {
//通过restTemplate远程调用
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
/**
* 使用Nacos
* @param userName
* @return
*/
@RequestMapping("getusername2")
String getUserName2(@RequestParam("name") String userName) {
//获取服务列表,因为是集群所以是list
List<ServiceInstance> instanceList = discoveryClient.getInstances("spring-other1");
ServiceInstance serviceInstance = instanceList.get(0);
String userName2 = restTemplate.getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/other1/getusername?name="+userName,String.class);
return userName2 + "-该字段为other2的getUserName反回的数据";
}
}
5、测试集群
四、通过Ribbon实现负载均衡策略
1、在启动类RestTemplate上添加注解@LoadBalanced
1、首先从注册中心获取provider的列表
2、通过一定的策略选择其中一个节点
3、再返回给restTemplate调用
@SpringBootApplication
@EnableDiscoveryClient
public class SpringOther2Application {
public static void main(String[] args) {
SpringApplication.run(SpringOther2Application.class,args);
}
@Bean
@LoadBalanced//开启负载均衡Ribbon
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
2、被调用方controller代码
@RestController
@RequestMapping("other1")
public class OtherController1 {
//方便知道是那台机器
@RequestMapping("getusername2")
String getUserName2(@RequestParam("name") String userName, HttpServletRequest request) {
return userName+"-由"+request.getServerName()+":"+request.getServerPort()+"发起";
}
}
3、调用方controller代码
@RestController
@RequestMapping("other2")
public class OtherController2 {
//通过restTemplate远程调用
@Autowired
private RestTemplate restTemplate;
/**
* 使用Ribbon负载均衡
* @param userName
* @return
*/
@RequestMapping("getusername3")
String getUserName3(@RequestParam("name") String userName) {
//写入服务名即可
String userName2 = restTemplate.getForObject("http://spring-other1/other1/getusername2?name="+userName,String.class);
return userName2 + "-该字段为other2的getUserName反回的数据";
}
}
效果图
五、负载均衡策略调整
策略类 | 命名 | 描述 |
---|---|---|
RandomRule | 随机策略 | 随机选择server |
RoundRobinRule | 轮询策略 | 按照顺序选择server(默认) |
RetryRule | 重试策略 | 当选择server不成功,短期内尝试选择一个可用的server |
AvailabilityFilteringRule | 可用过滤策略 | 过滤掉一直失败并被标记为circuit tripped的server,过滤掉那些高并发链接的server(active connections超过配置的阈值) |
WeightedResponseTimeRule | 响应时间加权重策略 | 根据server的响应时间分配权重,以响应时间作为权重,响应时间越短的服务器被选中的概率越大,综合了各种因素,比如:网络,磁盘,io等,都直接影响响应时间 |
ZoneAvoidanceRule | 区域权重策略 | 综合判断server所在区域的性能,和server的可用性,轮询选择server |
1、启动配置文件中修改策略
在调用方的启动配置文件
spring-other1: #服务名称
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #负载均衡策略(该为随机)