这一篇依赖前面的SpringCloud的服务发现的服务,因此首先需要在Discovery Service服务发现那一篇中的创建Discovery Server服务
一、第一种方式通过@LoadBalanced注册LoadBalancing
1.创建ribbin-time-service服务
导入包,实际是通过Spring Initializr导入的,Eureka Discovery,Spring Web
spring-boot-starter-web spring-cloud-starter-netflix-eureka-client
2.配置ribbin-time-service启动类注解
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonTimeServiceApplication {
@Value("${server.port}")
private int port;
public static void main(String[] args) {
SpringApplication.run(RibbonTimeServiceApplication.class, args);
}
@GetMapping
public String getTime(){
return "The current time is "+ new Date().toString()
+ "(answered by service running on "+ port +")";
}
}
3.ribbin-time-service配置文件
spring.application.name=time-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
4.创建ribbin-time-app服务
导入包,实际是通过Spring Initializr导入的,Eureka Discovery,Spring Web,Ribbon
spring-boot-starter-web
spring-cloud-starter-netflix-eureka-client
spring-cloud-starter-netflix-ribbon
5.配置ribbin-time-app启动类注解
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonTimeAppApplication {
@Autowired
private RestTemplate restTemplate;
public static void main(String[] args) {
SpringApplication.run(RibbonTimeAppApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
@GetMapping
public String getTime(){
return restTemplate.getForEntity("http://time-service",String.class).getBody();
}
}
6.ribbin-time-app配置文件默认空配置
7.ribbin-time-service分别配置两个spring-boot服务,server.port分别为4444和5555,分别启动
ribbon-time-app启动后
通过http://localhost:8080/访问
//输出:
The current time is Mon Apr 13 22:53:15 CST 2020(answered by service running on 5555)
//或者
The current time is Mon Apr 13 22:53:15 CST 2020(answered by service running on 4444)
二、另一种方式通过@RibbonClient配置
1.创建ribbin-time-service服务
//配置启动类注解
@RestController
@SpringBootApplication
public class RibbonTimeServiceApplication {
@Value("${server.port}")
private int port;
public static void main(String[] args) {
SpringApplication.run(RibbonTimeServiceApplication.class, args);
}
@GetMapping
public String getTime(){
return "The current time is "+ new Date().toString()
+ "(answered by service running on "+ port +")";
}
}
2.ribbin-time-service服务配置文件
spring.application.name=time-service
3.创建ribbon-time-app服务
//配置启动类注解
@RestController
@RibbonClient(name = "time-service")
@SpringBootApplication
public class RibbonTimeAppApplication {
@Autowired
private RestTemplate restTemplate;
public static void main(String[] args) {
SpringApplication.run(RibbonTimeAppApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
@GetMapping
public String getTime(){
return restTemplate.getForEntity("http://time-service",String.class).getBody();
}
}
4.ribbon-time-app配置文件
time-service.ribbon.eureka.enabled=false
time-service.ribbon.listOfServers=http://localhost:4444,http://localhost:5555
5.ribbon-time-service分别配置两个spring-boot服务,server.port分别为4444和5555,分别启动
ribbon-time-app启动后
通过http://localhost:8080/访问
输出:
The current time is Mon Apr 13 22:53:15 CST 2020(answered by service running on 5555)
或者
The current time is Mon Apr 13 22:53:15 CST 2020(answered by service running on 4444)
三、自定义ribbon的负载规则IRule
1.RoundRibbonRule,滚动的负载,第一次a,第二次b,第三次a,以此循环
ResponseTimeWeightedRule根据每个节点的相应时间负载
RandomRule随机负载
ZoneAvoidanceRule以根据AWS区域和可用性发送流量
@Configuration
public class RibbonTimeConfig {
@Bean
public IRule ribbonRule(){
return new RandomRule();
}
}
2.自定义ribbon的负载规则IPing
DummyPing不检查活动性或者可用性,将流量发送到所有实例
PingUrl通过HTTP访问获取与其响应并检查结果
NIWSDiscoveryPing,Eureka发现服务的自动配置检测
@Configuration
public class RibbonTimeConfig {
@Bean
public PingUrl ribbonPing(){
PingUrl pingUrl = new PingUrl();
pingUrl.setExpectedContent("true");//这个内容需要与服务器接口返回的内容一致才认为服务是正常的
return pingUrl;
}
}
//启动类中配置
@RestController
@RibbonClient(name = "time-service",configuration = RibbonTimeConfig.class)
@SpringBootApplication
public class RibbonTimeAppApplication {
}
网友评论