一、添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
pom.xml文件中存在spring-cloud-starter-netflix-eureka-server依赖时无需再引入Ribbon依赖,Eureka中已经包含了Ribbon依赖;
二、properties文件
server.port=8889
spring.datasource.url=jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=sx96411
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#jpa
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
#eureka
spring.application.name=microservice-consumer-movie
eureka.client.serviceUrl.defaultZone=http://suxing:suxing123@localhost:8000/eureka/
eureka.instance.prefer-ip-address=true
#Ribbon
#ribbon.eureka.enabled=false
#ribbon.eager-load.enabled=true
#ribbon.eager-load.clients=client1
三、Controller
package com.vainglory.stySpringCloud.controller;
import java.math.BigDecimal;
import java.net.URI;
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.http.ResponseEntity;
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.vainglory.stySpringCloud.entity.User;
import ch.qos.logback.classic.Logger;
@RestController
public class MovieController {
private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(MovieController.class);
@Autowired
private RestTemplate restTemplate;
@Autowired
private LoadBalancerClient loadBanlancerClient;
//返回User类型
@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.loadBanlancerClient.choose("microservice-provider-user");
//打印当前选择的是哪个节点
MovieController.LOGGER.info("{}:{}:{}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
}
}
在控制器中使用以下代码注入Ribbon Client负载均衡客户端
@Autowired
private LoadBalancerClient loadBanlancerClient;
四、Entity Bean
package com.vainglory.stySpringCloud.entity;
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Integer age;
private BigDecimal balance;
//省略getter和setter
}
五、启动类
package com.vainglory.stySpringCloud;
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;
@SpringBootApplication
@EnableDiscoveryClient
public class MicroserviceConsumerMovieApplication {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerMovieApplication.class, args);
}
}
@LoadBalanced注解用于开启负载均衡,RestTemplate类会使用Ribbon的自动化配置;
总结:使用Eureka和Ribbon实现服务提供者的高可用以及服务消费者的负载均衡,同时解决硬编码问题。