《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

package cn.zxuqian.controllers;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class ProductController {

private static Logger log = LoggerFactory.getLogger(ProductController.class);

@RequestMapping(“/products”)

public String productList() {

log.info(“Access to /products endpoint”);

return “外套,夹克,毛衣,T恤”;

}

}

为web配置Ribbon


首先在pom.xml中添加Ribbon的依赖:

org.springframework.cloud

spring-cloud-starter-netflix-ribbon

然后修改Application类,添加如下代码:

@EnableCircuitBreaker

@EnableDiscoveryClient

@RibbonClient(name = “product-service”)

@SpringBootApplication

public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);

}

@Bean

@LoadBalanced

public RestTemplate rest(RestTemplateBuilder builder) {

return builder.build();

}

}

这里用到了@RibbonClient(name = "product-service")注解,用来标记此项目为Ribbon负载均衡的客户端,它需要选择产品服务集群中其中的一台来访问所需要的服务,这里的name属性对应于productService项目中配置的spring.application.name属性。

@LoadBalanced注解标明了RestTemplate会被配置为自动使用Ribbon的LoadBalancerClient来选择服务的uri并发送请求。

在我们在ProductService类中添加如下代码:

@Service

public class ProductService {

private final RestTemplate restTemplate;

@Autowired

private DiscoveryClient discoveryClient;

public ProductService(RestTemplate restTemplate) {

this.restTemplate = restTemplate;

}

@HystrixCommand(fallbackMethod = “backupProductList”)

public String productList() {

List instances = this.discoveryClient.getInstances(“product-service”);

if(instances != null && instances.size() > 0) {

return this.restTemplate.getForObject(instances.get(0).getUri() + “/products”, String.class);

}

return “”;

}

public String backupProductList() {

return “夹克,毛衣”;

}

public String productListLoadBalanced() {

return this.restTemplate.getForObject(“http://product-service/products”, String.class);

}

}

这里新添加了一个productListLoadBalanced方法,跟之前的productList方法访问的是同一服务,只不过是用Ribbon Client去做了负载均衡,这里的uri的host变成了product-service即要访问的服务的名字,跟@RibbonClient中配置的name属性保持一致。最后在我们的ProductController中添加下面的代码:

@RestController

public class ProductController {

@Autowired

private ProductService productService;

@RequestMapping(“/products”)

public String productList() {

return productService.productList();

}

@RequestMapping(“/productslb”)

public String productListLoadBalanced() {

return productService.productListLoadBalanced();

}

}

来创建一个专门处理/productslb请求的方法,调用productServie提供负载均衡的方法。

到这里我们的代码就完成了,代码看似简单,其实是所有的配置都使用了默认值。Ribbon提供了编程式和配置式两种方式来配置Ribbon Client。现简单介绍下,后续深入Ribbon时再和大家一起看看如何修改它的配置。Ribbon提供如下配置(左边是接口,右边是默认实现):

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl

  • IRule ribbonRule: ZoneAvoidanceRule

  • IPing ribbonPing: DummyPing

  • ServerList<Server> ribbonServerList: ConfigurationBasedServerList

  • ServerListFilter<Server> ribbonServerListFilter: ZonePreferenceServerListFilter

  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

  • ServerListUpdater ribbonServerListUpdater: PollingServerListUpdater

因为我们这个项目用了Eureka,所以有些配置项和默认实现有所不同,如Eureka使用DiscoveryEnabledNIWSServerList取代ribbonServerList来获取在Eureka上注册的服务的列表。下边有一个简单的Congiguration类,来自Spring官网:

public class SayHelloConfiguration {

@Autowired

IClientConfig ribbonClientConfig;

@Bean

public IPing ribbonPing(IClientConfig config) {

return new PingUrl();

}

@Bean

public IRule ribbonRule(IClientConfig config) {

return new AvailabilityFilteringRule();

}

}

写在最后

还有一份JAVA核心知识点整理(PDF):JVM,JAVA集合,JAVA多线程并发,JAVA基础,Spring原理,微服务,Netty与RPC,网络,日志,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算…

image

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
理,微服务,Netty与RPC**,网络,日志,Zookeeper,Kafka,RabbitMQ,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存,Hadoop,Spark,Storm,YARN,机器学习,云计算…

[外链图片转存中…(img-W2yszh4g-1714660641682)]

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

Logo

欢迎加入西安开发者社区!我们致力于为西安地区的开发者提供学习、合作和成长的机会。参与我们的活动,与专家分享最新技术趋势,解决挑战,探索创新。加入我们,共同打造技术社区!

更多推荐