一、Client代碼示例
說明:此處使用的SpringBoot版本為2.1.13.RELEASE朋其,SpringCloud版本為Greenwich.SR5
為了演示負載均衡就需要有多個client,此處我們只需要將之前的client復(fù)制一份久又,稍作修改即可
1勤讽、maven依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、application.yml
client
server:
port: 8001
spring:
application:
name: client
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: client-8001
client:
service-url:
defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
#info信息
info:
app:
name: client-8001
company:
name: www.xxx.com
build:
artifactId: ${project.artifactId}
version: ${project.version}
client2
server:
port: 8002
spring:
application:
name: client
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: client-8002
client:
service-url:
defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
#info信息
info:
app:
name: client-8002
company:
name: www.xxx.com
build:
artifactId: ${project.artifactId}
version: ${project.version}
3膜眠、啟動類
啟動類是一樣的岩臣,此處代碼就只貼了一份
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class Client8001Application {
public static void main(String[] args) {
SpringApplication.run(Client8001Application.class,args);
}
}
4、其他java類
Controller類
package org.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name){
return "hello,"+name;
}
}
package example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name){
return "hello2,"+name;
}
}
兩個服務(wù)的Controller類實際上應(yīng)該是一樣的宵膨,此處為了演示負載均衡架谎,稍作修改一個為“return "hello,"+name;”,另一個為“return "hello2,"+name;”
二辟躏、代碼示例
說明:此處使用的SpringBoot版本為2.1.13.RELEASE谷扣,SpringCloud版本為Greenwich.SR5
SpringCloud實現(xiàn)負載均衡比較簡單 ,只需要在RestTemplate類上加上一個注解@LoadBalanced,并修改下RestTemplate調(diào)用方式捎琐,原始使用的是url直接調(diào)用会涎,現(xiàn)在改為通過服務(wù)調(diào)用
1、maven依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2瑞凑、application.yml
server:
port: 9002
spring:
application:
name: ribbon
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: ribbon-9002
client:
service-url:
defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
#info信息
info:
app:
name: ribbon-9002
company:
name: www.xxx.com
build:
artifactId: ${project.artifactId}
version: ${project.version}
3末秃、啟動類
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
public class Ribbon9002Application {
public static void main(String[] args) {
SpringApplication.run(Ribbon9002Application.class,args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
4、其他java類
Controller
調(diào)用地址格式:http://{服務(wù)}/hello/name
package org.example.controller;
import org.springframework.beans.factory.annotation.Autowired;
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;
@RestController
public class HelloController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name){
return restTemplate.getForObject("http://client/hello/"+name,String.class);
}
}
2籽御、測試驗證
先后啟動server练慕、client、consumer,訪問http://localhost:7001/
多次訪問http://localhost:9002/hello/zs
交替出現(xiàn)
SpringCloud默認使用輪詢策略進行負載均衡技掏,如果想修改策略铃将,只需要增加IRue注入到Spring容器
@Bean
IRule iRule(){
return new RandomRule();//IRule實現(xiàn)類,此處為隨機
}
再次多次訪問http://localhost:9002/hello/zs哑梳,不再試交替出現(xiàn)“hello,zs”和“hello2,zs”劲阎,而是隨機出現(xiàn)
我們也可以自己定義負載均衡算法,大家可以參照:https://blog.csdn.net/www1056481167/article/details/81151064
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
參考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
還有尚硅谷周陽老師的視頻