spring cloud之ribbon/openFeign

 

 

 

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
eureka该jar中已集成ribbon,无需在引用,若非引用也无碍
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

 

 

代码如下
@Configuration
public class ApplicationContextConfig {

@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}

 

 



修改ribbon负载均衡策略(默认轮询)--配置不能被被@ComponentScan扫描(@SpringBootApplication中包含@ComponentScan
1随机

代码如下

@Configuration
public class MySelfRule {

@Bean
public IRule myRule(){
//TODO 定义为随机
return new RandomRule();
}

}

 手写轮询算法

 

 

 代码如下

public interface LoadBalancer {
ServiceInstance instances(List<ServiceInstance> serviceInstances);
}

 

 

@Component
public class MyLB implements LoadBalancer {

private AtomicInteger atomicInteger = new AtomicInteger(0);

public final int getAndIncrement() {
int current;
int next;
do {
current = this.atomicInteger.get();
next = current >= 2147483647 ? 0 : current + 1;
} while (! this.atomicInteger.compareAndSet(current,next));
System.out.println("*****************第几次访问次数**next:"+next);
return next;
}

@Override
public ServiceInstance instances(List<ServiceInstance> serviceInstances) {

int index = getAndIncrement() % serviceInstances.size();

return serviceInstances.get(index);
}
}

测试---手写轮询算法

@Resource
private LoadBalancer loadBalancer;

@Resource
private DiscoveryClient discoveryClient;
@GetMapping("/consumer/lb/{id}")
public CommonResult<Payment> getPaymentLB(@PathVariable("id") long id){
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVECE");
if (instances == null || instances.size() <= 0){
return null;
}
ServiceInstance serviceInstance = loadBalancer.instances(instances);
URI uri = serviceInstance.getUri();
return restTemplate.getForObject(uri+"/payment/getPaymentById/"+id,CommonResult.class);

}
测试结果
{"code":200,"message":"成功","data":null}

openfeign使用

openfeign = rubbon+restTemplate (
openfeign 的使用更符合我们的编码习惯

使用
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

服务端接口 B

 

 

 客户端A

 

 

 

 

 

 

在启动类上加@EnableFeignClients
调用 http://localhost/order-feign/getPaymentById/1
结果 {"code":200,"message":"success","data":{"code":200,"message":"成功","data":{"id":1,"serial":"22222"}}

openfeign超时控制(默认1s)
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000

openfeign日志增强

 

 logging:

  level:
com.atgugu.springcloud.service.PaymentFeignService: debug

查看控制台日志

--->GET http://CLOUD-PAYMENT-SERVECE/payment/getPaymentById/1 HTTP/1.1
---> END HTTP (0-byte body)
<--- HTTP/1.1 200 (5038ms)
connection: keep-alive
content-type: application/json
date: Thu, 04 Jun 2020 09:14:48 GMT
keep-alive: timeout=60
transfer-encoding: chunked






 

 


 

 

 


 

 







posted on 2020-06-04 11:01  寂寞一沙洲  阅读(616)  评论(0编辑  收藏  举报

导航