3、spring cloud服務註冊中心eureka---基於feign的負載均衡(第二章)
阿新 • • 發佈:2018-11-13
基於feign的負載均衡
spring-cloud-producer-one修改,將其中的controller改動如下:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return return "test1:hello"+name+"this is first message!";;
}
}
spring-cloud-producer-two修改,將其中的controller改動如下:
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return return "test2:hello"+name+"this is first message!";;
}
}
spring-cloud-producer-one 、spring-cloud-producer-two 修改,將其中的remote改動 都修改為 @FeignClient(name=“spring-cloud-producer”)
@FeignClient(name="spring-cloud-producer")
public interface HelloRemote {
@RequestMapping(value = "/hello")
String hello(@RequestParam(value = "name") String name);
}
在配置檔案中改動埠:
spring-cloud-producer-one 、spring-cloud-producer-two 修改,將其中的application.properties 都改動為
spring.application.name=spring-cloud-producer
在eureka就會發現兩個服務提供者,
然後在瀏覽器再次輸入:http://localhost:9091/hello/llx 進行測試:
第一次返回結果:test1:hello llx this is first message!
第二次返回結果:test2:hello llx this is first message!
不斷的進行測試下去會發現兩種結果交替出現,說明兩個服務中心自動提供了服務均衡負載的功能。如果我們將服務提供者的數量在提高為N個,測試結果一樣,請求會自動輪詢到每個服務端來處理。