哪個(gè)網(wǎng)站的域名便宜網(wǎng)站優(yōu)化推廣seo公司
其實(shí)在項(xiàng)目中直接使用ribbon時(shí)不多,大多是使用feign的,其實(shí)feign底層也是通過(guò)ribbon構(gòu)建的,主要記憶一下計(jì)算規(guī)則,ribbon的源碼還是很不錯(cuò)的,還是值得學(xué)習(xí)的。
1、添加pom
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
2、啟動(dòng)類注解
我沒(méi)有在啟動(dòng)類添加注解,直接另外一個(gè)配置類@Configuration添加
3、添加配置
# 默認(rèn)是輪訓(xùn),都有隨機(jī)、加權(quán)響應(yīng)時(shí)間、重試,看具體業(yè)務(wù)和服務(wù)器搭配
#eureka-client.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule
4、參照源碼規(guī)則,自己編寫規(guī)則
這里是通過(guò)hashcode在hash閉環(huán)定義的,當(dāng)每臺(tái)服務(wù)器hashcode在hash閉環(huán)的對(duì)應(yīng)位置后,每次請(qǐng)求直接定位到hash中順時(shí)針尋找最近的服務(wù)器
package com.xl.ribbon.consumer.rules;import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.mvc.condition.RequestConditionHolder;import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;@NoArgsConstructor
public class MyRule extends AbstractLoadBalancerRule implements IRule {@Overridepublic void initWithNiwsConfig(IClientConfig iClientConfig) {}@Overridepublic Server choose(Object key) {HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();String uri = request.getServletPath() + "?" + request.getQueryString();return route(uri.hashCode(), getLoadBalancer().getAllServers());}public Server route(int hashId, List<Server> servers) {if (CollectionUtils.isEmpty(servers)) {return null;}TreeMap<Long, Server> serverMap = new TreeMap<>();servers.forEach(server -> {// 虛化若干個(gè)服務(wù)節(jié)點(diǎn),到環(huán)上for (int i = 0; i < 8; i++) {long hash = hash(server.getId() + i);serverMap.put(hash, server);}});long hash = hash(String.valueOf(hashId));SortedMap<Long, Server> last = serverMap.tailMap(hash);// 當(dāng)request URL的hash值大于任意一個(gè)服務(wù)器對(duì)應(yīng)的hashKey,// 取serverMap中的第一個(gè)節(jié)點(diǎn)if (last.isEmpty()) {Server value = serverMap.firstEntry().getValue();last.put(hash, value);}return last.get(last.firstKey());}public long hash(String key) {MessageDigest md5;try {md5 = MessageDigest.getInstance("MD5");} catch (NoSuchAlgorithmException e) {throw new RuntimeException(e);}byte[] keyBytes = null;try {keyBytes = key.getBytes("UTF-8");} catch (UnsupportedEncodingException e) {throw new RuntimeException(e);}md5.update(keyBytes);byte[] digest = md5.digest();long hash = (digest[2] & 0xFF << 16) | (digest[1] & 0xFF) << 8 | (digest[0] & 0xFF);return hash & 0xffffffffL;}
}
5、規(guī)則配置
其實(shí)直接配置在application.properties也可以,這里我是在一個(gè)配置文件
package com.xl.ribbon.consumer;import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.xl.ribbon.consumer.rules.MyRule;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** Created by lig on 2024/10/12.*/
@Configuration
//@RibbonClient(name = "eureka-client", configuration = com.netflix.loadbalancer.RandomRule.class)
@RibbonClient(name = "eureka-client", configuration = MyRule.class)
public class RibbonConfiguration {
//
// @Bean
// public IRule defaultLBStrategy() {
// return new RandomRule();
// }}
6、接口
package com.xl.ribbon.consumer;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;/*** Created by lig on 2024/10/12.*/
@RestController
public class RibbonController {@Autowiredprivate RestTemplate restTemplate;@GetMapping("/sayHi")public String sayHi() {return restTemplate.getForObject("http://eureka-client/sayHi", String.class);}}
總結(jié)
其實(shí)在實(shí)際項(xiàng)目中ribbon編寫符合自己業(yè)務(wù)的規(guī)則還是挺復(fù)雜的,用途還是挺廣的.