4.0.0
com.itmuch.cloud
microservice-consumer-movie-ribbon-customizing-properties
0.0.1-SNAPSHOT
jar
org.springframework.boot
spring-boot-starter-parent
1.4.3.RELEASE
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.cloud
spring-cloud-dependencies
Camden.SR4
pom
import
org.springframework.boot
spring-boot-maven-plugin
server:
port: 8010
spring:
application:
name: microservice-consumer-movie
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
#这里选用的是负载均衡的[随机策略]
microservice-provider-user:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
关键处:
#这里选用的是负载均衡的[随机策略]
microservice-provider-user:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
说明:还可以更换不同的策略;
package com.itmuch.cloud.study.user.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Integer age;
private BigDecimal balance;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return this.age;
}
public void setAge(Integer age) {
this.age = age;
}
public BigDecimal getBalance() {
return this.balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
}
package com.itmuch.cloud.study;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* 用配置文件来完成自定义
*/
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerMovieApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerMovieApplication.class, args);
}
}
package com.itmuch.cloud.study.user.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import com.itmuch.cloud.study.user.entity.User;
@RestController
public class MovieController {
private static final Logger LOGGER = LoggerFactory.getLogger(MovieController.class);
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBalancerClient;
@GetMapping("/test")
public String test() {
ServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-provider-user");
System.out.println("--------随机:"+serviceInstance.getHost()+":"+serviceInstance.getPort()+":"+serviceInstance.getServiceId());
ServiceInstance serviceInstance2 = this.loadBalancerClient.choose("microservice-provider-user2");
System.out.println("--------轮询:"+serviceInstance2.getHost()+":"+serviceInstance2.getPort()+":"+serviceInstance2.getServiceId());
return "1";
}
@GetMapping("/user/{id}")
public User findById(@PathVariable Long id) {
return this.restTemplate.getForObject("http://microservice-provider-user/" + id, User.class);
}
@GetMapping("/log-user-instance")
public void logUserInstance() {
ServiceInstance serviceInstance = this.loadBalancerClient.choose("microservice-provider-user");
// 打印当前选择的是哪个节点
MovieController.LOGGER.info("{}:{}:{}", serviceInstance.getServiceId(), serviceInstance.getHost(), serviceInstance.getPort());
}
}
microservice-provider-user:8000
microservice-provider-user:8001(改端口)
microservice-provider-user2:8002(改服务名和端口)
microservice-provider-user2:8003(改服务名和端口)
控制台输出:
--------轮询:192.168.1.102:8002:microservice-provider-user2
--------随机:192.168.1.102:8000:microservice-provider-user
--------轮询:192.168.1.102:8003:microservice-provider-user2
--------随机:192.168.1.102:8000:microservice-provider-user
--------轮询:192.168.1.102:8002:microservice-provider-user2
--------随机:192.168.1.102:8000:microservice-provider-user
--------轮询:192.168.1.102:8003:microservice-provider-user2
--------随机:192.168.1.102:8000:microservice-provider-user
--------轮询:192.168.1.102:8002:microservice-provider-user2
--------随机:192.168.1.102:8001:microservice-provider-user
--------轮询:192.168.1.102:8003:microservice-provider-user2
--------随机:192.168.1.102:8001:microservice-provider-user
--------轮询:192.168.1.102:8002:microservice-provider-user2
--------随机:192.168.1.102:8001:microservice-provider-user
--------轮询:192.168.1.102:8003:microservice-provider-user2
--------随机:192.168.1.102:8001:microservice-provider-user
--------轮询:192.168.1.102:8002:microservice-provider-user2
......
#这里选用的是负载均衡的[随机策略]
microservice-provider-user:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
==============================
QQ群:143522604
欢迎和大家一起学习、交流、提升!
==============================