SpringCloud客户端的负载均衡Ribbon
一.创建Eureka服务器
springcloud-eureka-server-ribbon(创建的就是普通的Eureka-Server)
1.添加依赖(创建项目时,勾选SpringWeb,Eureka Server)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.配置application.properties文件
#端口号
server.port=9100
#服务名
eureka.instance.hostname=localhost
#是否将自己注册到Eureka Server
eureka.client.register-with-eureka=false
#是否从Eureka Server获取注册服务信息
eureka.client.fetch-registry=false
#指定注册中心位置
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
3.在启动类添加注解(@EnableEurekaServer)
@SpringBootApplication
@EnableEurekaServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
二.创建两个服务提供者
springcloud-eureka-client-provider8081
springcloud-eureka-client-provider8082
1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eurekaclient</artifactId>
</dependency>
2.配置application.properties文件
#端口号
server.port=8081
#服务名
spring.application.name=springcloud-eureka-client-provider
#注册中心地址
eureka.client.service-url.defaultZone=http://localhost:9100/eureka
===================
#端口号
server.port=8082
#服务名
spring.application.name=springcloud-eureka-client-provider
#注册中心地址
eureka.client.service-url.defaultZone=http://localhost:9200/eureka
3.创建控制器
@RestController
public class TestController {
@RequestMapping("/test")
public String test(){
return "集群服务提供者8081";
}
}
==========================================
@RestController
public class TestController {
@RequestMapping("/test")
public String test(){
return "集群服务提供者8082";
}
}
4.启动类中添加注解(@EnableEurekaClient)
@SpringBootApplication
@EnableEurekaClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
三.创建服务调用者
springcloud-eureka-client-consumer
1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eurekaclient</artifactId>
</dependency>
2.配置application.properties文件
#端口号
server.port=8083
#服务名
spring.application.name=springcloud-eureka-client-provider
#注册中心地址
eureka.client.service-url.defaultZone=http://localhost:9100/eureka
3.编写config 更改ribbon默认轮询策略
@Configuration
public class RestTempConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
@Bean
public IRule iRule(){
return new RandomRule();
}
}
4.编写控制器
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/test")
public String test(){
ResponseEntity<String> result = restTemplate.getForEntity("http://04-SPRINGCLOUD-EUREKA-CLIENT-PROVIDER/test", String.class);
String body = result.getBody();
return "ribbon负载均衡-服务消费者:"+body;
}
}
5.给启动类添加注解(@EnableEurekaClient)
6.访问地址 http://localhost:8083/test 在地址后面敲回车 体验负载均衡