整合nacos
先整合spring cloud alibaba
<dependencyManagement> <dependencies> <!-- 整合spring cloud--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 整合spring cloud alibaba--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>0.9.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
导入nacos
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
不需写注解,配置好配置文件
spring cloud: nacos: discovery: server-addr: 127.0.0.1:8848 cluster-name: BJ #集群名称 application: name: content-center #服务名称
整合ribbon
依赖已经在nacos中一起导入了
在启动类中restTemplate,加上注解
@LoadBalanced//为restTemplate整合Ribbon
配置负载均衡规则
在包外面定义ribbon所使用的规则
在包内运用该规则
@RibbonClient(name = "user-center",configuration = RibbonConfiguration.class)//单独配置
@RibbonClients(defaultConfiguration = RibbonConfiguration.class)//全局配置
建议在配置文件中直接定义规则
#指定该服务用到RandomRule规则 #user-center: # ribbon: # NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
饥饿加载:减少第一次访问的时间
#指定该服务开启饥饿加载 ribbon: eager-load: clients: user-center # 后面可用逗号分隔为那些服务进行饥饿加载,解决首次加载过慢:user-center,user-center2,user-center3..... enabled: true
以下规则直接在包外配置文件RibbonConfiguration中配置调用
nacos搭配ribbon调用权重大的服务
package com.itmuch.usercenter.configuretion; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.*; import com.netflix.ribbon.proxy.annotation.Var; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; import org.springframework.cloud.alibaba.nacos.ribbon.NacosServer; @Slf4j public class NacosWeightedRule extends AbstractLoadBalancerRule { @Autowired private NacosDiscoveryProperties nacosDiscoveryProperties; @Override public void initWithNiwsConfig(IClientConfig iClientConfig) { //读取配置文件并初始化NacosWeightedRule //暂时用不到,robbon里面的规则这个也都用不到 } @Override public Server choose(Object o) { try { //ribbon的入口,需要用到getname,所以用BaseLoadBalancer强转一下 BaseLoadBalancer loadBalancer = (BaseLoadBalancer) this.getLoadBalancer(); // log.info("lb:{}",loadBalancer); String name = loadBalancer.getName(); //实现负载均衡算法 NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();//拿到服务发现的相关API //nacos client自动通过基于权重的负载均衡算法,给我们一个实例 Instance instance = namingService.selectOneHealthyInstance(name); log.info("选择的实例是:port:{},instance:{}",instance.getPort(),instance); return new NacosServer(instance); } catch (NacosException e) { return null; } } } //public class NacosWeightedRule implements IRule { // @Override // public Server choose(Object o) { // return null; // } // // @Override // public void setLoadBalancer(ILoadBalancer iLoadBalancer) { // // } // // @Override // public ILoadBalancer getLoadBalancer() { // return null; // }
nacos调用不同集群下的服务
package com.itmuch.usercenter.configuretion; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.*; import com.netflix.ribbon.proxy.annotation.Var; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; import org.springframework.cloud.alibaba.nacos.ribbon.NacosServer; @Slf4j public class NacosWeightedRule extends AbstractLoadBalancerRule { @Autowired private NacosDiscoveryProperties nacosDiscoveryProperties; @Override public void initWithNiwsConfig(IClientConfig iClientConfig) { //读取配置文件并初始化NacosWeightedRule //暂时用不到,robbon里面的规则这个也都用不到 } @Override public Server choose(Object o) { try { //ribbon的入口,需要用到getname,所以用BaseLoadBalancer强转一下 BaseLoadBalancer loadBalancer = (BaseLoadBalancer) this.getLoadBalancer(); // log.info("lb:{}",loadBalancer); String name = loadBalancer.getName(); //实现负载均衡算法 NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();//拿到服务发现的相关API //nacos client自动通过基于权重的负载均衡算法,给我们一个实例 Instance instance = namingService.selectOneHealthyInstance(name); log.info("选择的实例是:port:{},instance:{}",instance.getPort(),instance); return new NacosServer(instance); } catch (NacosException e) { return null; } } } //public class NacosWeightedRule implements IRule { // @Override // public Server choose(Object o) { // return null; // } // // @Override // public void setLoadBalancer(ILoadBalancer iLoadBalancer) { // // } // // @Override // public ILoadBalancer getLoadBalancer() { // return null; // }