spring-cloud: eureka之:ribbon负载均衡自定义配置(二)

有默认配置的话基本上就是轮询接口,现在我们改用自定义配置,同时支持:轮询,随机接口读取

准备工作:

1.eureka服务

2.两个user服务: 项目名:spring-cloud-user接口:7900/7901

3.两个user服务:项目名:spring-cloud-user2接口:8800/8801

4.movie服务,读取/调用user信息接口

eureka启动文件加入@EnableEurekaServer注解

@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication { public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}

user服务:

spring-cloud-user配置文件:

#port
server.port=7901/7900
#data
spring.jpa.generate-ddl=false
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.datasource.platform=h2
spring.datasource.schema=classpath:schema.sql
spring.datasource.data=classpath:data.sql
spring.application.name=spring-cloud-user
#log
logging.level.root=INFO
logging.level.org.org.hibernate=INFO
logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.hibernate.type.descriptor.sql.BasicExtractor=TRACE
logging.level.com.muyang=debug
#eureka
eureka.client.serviceurl.defaultzone=http://localhost:8761/eureka
eureka.instance.preferIpAddress=true

spring-cloud-user启动文件加入@EnableEurekaClient注解

@EnableEurekaClient
@SpringBootApplication
public class BootUserApplication { public static void main(String[] args) {
SpringApplication.run(BootUserApplication.class, args);
}
}

spring-cloud-user的接口文件代码

@RestController
public class UserController { @Autowired
private UserRepository userRepository; @GetMapping("/simple/{id}")
public User findById(@PathVariable Long id)
{
return userRepository.findOne(id);
} }

spring-cloud-user2的配置同上,请注意相应的端口号的修改.

movie服务:

新建一个testConfiguration.java配置文件,跟启动文件目录同级

然后再新建一个@interface的ExcludeFromComponentScan注解文件,作为testConfiguration.java的componetscan注解

ExcludeFromComponentScan.java

public @interface ExcludeFromComponentScan {

}

TestConfiguration.java配置文件

引入componetScan,和随机接口读取

@Configuration
@ExcludeFromComponentScan
public class TestConfiguration { //@Autowired
//IClientConfig config; //随机
@Bean
public IRule ribbonIRule()
{
return new RandomRule();
} }

Application.java启动文件

再启动文件加入,@EnableEurekaClient注解,

同时设置spring-cloud-user接口为随机读取:@RibbonClient(name = "spring-cloud-user", configuration = TestConfiguration.class)

排除不合适的组件类型:excludeFilters:指定不适合组件扫描的类型

@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = ExcludeFromComponentScan.class) })@EnableEurekaClient

@EnableEurekaClient
@SpringBootApplication //随机spring-boot-user
@RibbonClient(name = "spring-boot-user", configuration = TestConfiguration.class)
@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = ExcludeFromComponentScan.class) }) //spring-boot-user2负载均衡
public class Application { @Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
} public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

  

movieController.java文档

以/test为例,spring-cloud-user为随机读取接口,spring-cloud-user2位轮询读取接口

@RestController
public class MovieController { @Autowired
private RestTemplate restTemplate; @Autowired
LoadBalancerClient loadBalancerClient; @GetMapping("/movie/{id}")
public User findById(@PathVariable Long id)
{
//http://localhost:7900/simple/
return restTemplate.getForObject("http://spring-boot-user/simple/" + id, User.class);
} @GetMapping("/test")
public String testUser()
{ //随机
ServiceInstance serviceInstance = this.loadBalancerClient.choose("spring-boot-user");
System.out.println("111:"+serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());
//负载均衡
ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("spring-boot-user2");
System.out.println("222:" + serviceInstance2.getServiceId() + ":" + serviceInstance2.getHost() + ":" + serviceInstance2.getPort()); return "1"; }
}

spring-cloud: eureka之:ribbon负载均衡自定义配置(二)的更多相关文章

  1. Spring Cloud微服务Ribbon负载均衡/Zuul网关使用

    客户端负载均衡,当服务节点出现问题时进行调节或是在正常情况下进行 服务调度.所谓的负载均衡,就是当服务提供的数量和调用方对服务进行 取舍的调节问题,在spring cloud中是通过Ribbon来解决 ...

  2. spring cloud: zuul(三): ribbon负载均衡配置

    zuul的routes配置下path/url组合不支持负载均衡 下面介绍zuul的routes配置下的path/serviceId负载均衡配置 spring-boot-user微服务开启了:7901, ...

  3. 关于spring cloud eureka整合ribbon实现客户端的负载均衡

    1. 实现eureka整合ribbon非常简单, 1.1.首先引入所需maven依赖 <dependency> <groupId>org.springframework.boo ...

  4. Spring Cloud 客服端负载均衡 Ribbon

    一.简介   Spring Cloud Ribbon 是一个基于Http和TCP的客服端负载均衡工具,它是基于Netflix Ribbon实现的.它不像服务注册中心.配置中心.API网关那样独立部署, ...

  5. 2.【Spring Cloud Alibaba】实现负载均衡-Ribbon

    负载均衡的两种方式 如何实现负载均衡 目前已经实现让A总能找到B,如何实现负载均衡 负载均衡的两种方式 服务器端负载均衡 客户端负载均衡 使用Ribbo实现负载均衡 Ribbon是什么 ==Netfl ...

  6. Spring Cloud ---- 服务消费与负载均衡(Rest + Ribbon )

    上一篇主要写了基于Eurake的服务的注册,主要就是创建注册中心,创建服务者,将服务者注册到注册中心,完成服务的暴露.这一篇主要写服务的消费与服务消费的负载均衡. 服务的调用方式有两种,Rest + ...

  7. 4.Spring Cloud初相识--------Feign负载均衡

    前言: 在上一节里,我们学习了ribbon的使用. 我们了解到ribbon是一个客户端负载均衡机制. 而我们今天要讲的Feign呢,也是一款客户端负载均衡机制. 或者这样说,Feign封装了ribbo ...

  8. Spring Cloud ---- 服务消费与负载均衡(feign)

    feign是一个声明式的伪客户端,只需要创建一个接口并且注解,它具有可插拔的特性.feign集合了Ribbon,再与Eurake结合实现服务的注册发现与负载均衡.结合Hystrix,具有熔断功能. 1 ...

  9. 【Spring Cloud学习之三】负载均衡

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 主流的负载均衡技术有nginx.LVS.HAproxy.F5,Spring Clou ...

随机推荐

  1. 常用jquery记录

    1.jquery easing jQuery Easing是一款比较老的jQuery插件,在很多网站都有应用,尤其是在一些页面滚动.幻灯片切换等场景应用比较多.它非常小巧,且有多种动画方案供选择,使用 ...

  2. linux常用命令:tr 命令

    tr 命令实现字符转换功能,其功能类似于 sed 命令,但是,tr 命令比 sed 命令简单.也就是说,tr 命令能实现的功能,sed 命令都可以实现.尽管如此,tr 命令依然是 Linux 系统下处 ...

  3. chrome谷歌浏览器用这种方式清除缓存比较方便了,必须是调试模式才行

     chrome谷歌浏览器用这种方式清除缓存比较方便了  PS:必须是调试模式才行,可以不是手机模式 ,有些低版本浏览器可能没有这个功能.   ----------------------------- ...

  4. nginx使用https功能

    nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/n ...

  5. Web前端学习笔记之安装和使用PhantomJS

    0x00 安装PhantomJS(linux环境安装) 将PhantomJS下载在/usr/local/src/packet/目录下(这个看个人喜好) 操作系统:CentOS 7 64-bit 1.下 ...

  6. 获取转UTF8的字符串

    /// <summary> /// 获取转UTF8的字符串 /// </summary> /// <param name="strWord">& ...

  7. Maximum GCD (stringstream)题解

    Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possiblepa ...

  8. Oracle 存储过程入门(一)

    一,基本入门介绍 公司系统需要用到oracle,可是还没在项目用过oracle,好吧,从基本学起.有问题的地方,欢迎指导啊. 看创建存储过程的基本定义.注意,带有[]的都是可选的,可有可无的.只是语法 ...

  9. Python time strptime()与time strftime()

    time strftime()接收时间元组,返回表示时间的字符串. time strptime()把时间字符串,解析成一个时间元组. import time t = time.strftime('%Y ...

  10. substring()的用法和注意事项

    作者原创:转载请注明出处 substring()方法的作用为截取字符串,其有两种用法: 分别如下: substring(int beginIndex);这个的作用为截取从beginindex位置处的元 ...