目標(biāo)
基于 eureka惹谐,創(chuàng)建一個后端service碾篡,使用不同端口運行3個實例舰绘,創(chuàng)建一個client service蹂喻,通過 ribbon 調(diào)用后端service,驗證負(fù)載均衡效果捂寿。
創(chuàng)建 eureka server
artifactId:eureka-server口四,spring boot: 1.5.16,依賴:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
</dependencies>
啟動類添加注解 @EnableEurekaServer
秦陋,配置文件:
spring.application.name=eurekaserver
server.port=8082
eureka.instance.hostname=localhost
eureka.client.serviceUrl.defaultZone=http://localhost:8082/eureka/
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
啟動蔓彩,查看頁面 http://localhost:8082,看是否正常顯示驳概。
創(chuàng)建后端服務(wù)
artifactId:backend-service赤嚼,依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
啟動類添加 @EnableEurekaClient
,配置文件:
spring.application.name=server
server.port = 9090
eureka.client.serviceUrl.defaultZone= http://localhost:8082/eureka/
eureka.client.healthcheck.enabled= true
eureka.instance.leaseRenewalIntervalInSeconds= 1
eureka.instance.leaseExpirationDurationInSeconds= 2
測試接口:
@RestController
public class MyRestController {
@Autowired
Environment environment;
@GetMapping("/")
public String health() {
return "I am Ok";
}
@GetMapping("/backend")
public String backend() {
System.out.println("Inside MyRestController::backend...");
String serverPort = environment.getProperty("local.server.port");
System.out.println("Port : " + serverPort);
return "Hello form Backend!!! " + " Host : localhost " + " :: Port : " + serverPort;
}
}
啟動顺又,訪問接口探膊,看是否正常。
創(chuàng)建 ribbon client 服務(wù)
artifactId:ribbon-client待榔,依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
</dependencies>
配置文件:
spring.application.name=client
server.port=8888
eureka.client.serviceUrl.defaultZone= http://localhost:8082/eureka/
eureka.client.healthcheck.enabled= true
eureka.instance.leaseRenewalIntervalInSeconds= 1
eureka.instance.leaseExpirationDurationInSeconds= 2
server.ribbon.eureka.enabled=true
server.ribbon.ServerListRefreshInterval=1000
ribbon 配置類:
public class RibbonConfiguration {
@Autowired
IClientConfig config;
@Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
}
@Bean
public IRule ribbonRule(IClientConfig config) {
return new AvailabilityFilteringRule();
}
}
啟動類:
@SpringBootApplication
@EnableDiscoveryClient
@RibbonClient(name = "server", configuration = RibbonConfiguration.class)
public class RibbonClientApplication {
@Bean
@LoadBalanced
public RestTemplate proviceRestTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(RibbonClientApplication.class, args);
}
}
測試類:
@RestController
public class HelloController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hi")
public String hi() {
return restTemplate.getForObject("http://server/backend", String.class);
}
}
運行測試
啟動 eureka server逞壁,然后啟動3個后端服務(wù):
java -jar -Dserver.port=9090 target/backend-service-0.0.1-SNAPSHOT.jar
java -jar -Dserver.port=9091 target/backend-service-0.0.1-SNAPSHOT.jar
java -jar -Dserver.port=9092 target/backend-service-0.0.1-SNAPSHOT.jar
啟動 ribbon client,訪問測試接口:
http://localhost:8888/hi
多次刷新锐锣,就可以看到顯示不同的端口號腌闯,說明負(fù)載均衡生效。