Ribbon,fegin负载

 

只需要使用@EnableEurekaServer注解就可以让应用变为Eureka服务器,这是因为spring boot封装了Eureka Server,让你可以嵌入到应用中直接使用。至于真正的EurekaServer是Netflix公司的开源项目,也是可以单独下载使用的
用Ribbon或者Feign做服务消费者 声明式REST客户端: Feign
zuul

 

RestTemplate Feign
Feign是一个声明式的REST客户端,它的目的就是让REST调用更加简单。
Feign可以与Eureka和Ribbon组合使用以支持负载均衡

1、zuul作为整个应用的流量入口,接收所有的请求,如app、网页等,并且将不同的请求转发至不同的处理微服务模块,其作用可视为nginx。
2、feign则是将当前微服务的部分服务接口暴露出来,并且主要用于各个微服务之间的服务调用

# feign和ribbon结合,指定策略。feign默认的是轮询的策略,这里的配置可以自定义
MICROSERVICE-ORDER:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class EurekaTest {
    public static void main(String[] args) {
        SpringApplication.run(EurekaTest.class, args);
    }
    @Bean
    @LoadBalanced//默认为轮询负载策略
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}
如果有多个服务者,客户端没有加//@LoadBalanced负载策略,远程调用会失败

@Configuration
public class TestConfiguration {
    @Bean
    public IRule ribbonRule() {
        return new RandomRule(); //设置负载均衡的规则为随机
       // return new RoundRobinRule(); //默认的轮询策略
    }
}
@RibbonClient(name = "hello-service", configuration = TestConfiguration.class)//不止hello-service,所有远程调用都按此策略 
public class EurekaTest {
    public static void main(String[] args) {
        SpringApplication.run(EurekaTest.class, args);
    }
    @Bean
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

改进版
public @interface ExcludeFromComponentScan {
}

@Configuration
@ExcludeFromComponentScan
public class TestConfiguration {
......

//只有hello-service,远程调用才用TestConfiguration中重写的策略,其他服务的远程调用策略不受影响
@RibbonClient(name = "hello-service", configuration = TestConfiguration.class)
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type= FilterType.ANNOTATION,value= ExcludeFromComponentScan.class)})//在入口类中排除此注解不扫描
public class EurekaTest {
......
https://www.cnblogs.com/duanxz/p/6123502.html

feign和ribbon结合,指定策略。feign默认的是轮询的策略,这里的配置可以自定义
MICROSERVICE-ORDER:
 ribbon:
   NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
  

 

posted @ 2019-05-19 18:53  苍天一穹  阅读(232)  评论(0)    收藏  举报