通过仅使用Ribbon而不使用任何服务注册表是否可能实现负载平衡?

huangapple go评论98阅读模式
英文:

Is it possible to achieve Load balancing by only using Ribbon without any Service Registry?

问题

  1. ****ConsumerService****
  2. ======Consumer Controller======
  3. @RestController
  4. @RibbonClient(name="ProducerService", configuration=RibbonConfig.class)
  5. public class ConsumerController {
  6. @Autowired
  7. private RestTemplate restTemplate;
  8. @GetMapping("/hello")
  9. public String hello()
  10. {
  11. String url = "http://ProducerService/value";
  12. return restTemplate.getForObject(url, String.class);
  13. }
  14. }
  15. ======Consumer Application======
  16. import org.springframework.boot.SpringApplication;
  17. import org.springframework.boot.autoconfigure.SpringBootApplication;
  18. @SpringBootApplication
  19. public class ConsumerService2Application {
  20. public static void main(String[] args) {
  21. SpringApplication.run(ConsumerService2Application.class, args);
  22. }
  23. }
  24. ======Ribbon Configuration File======
  25. import com.netflix.client.config.IClientConfig;
  26. import com.netflix.loadbalancer.AvailabilityFilteringRule;
  27. import com.netflix.loadbalancer.IPing;
  28. import com.netflix.loadbalancer.IRule;
  29. import com.netflix.loadbalancer.PingUrl;
  30. public class RibbonConfig {
  31. @Autowired
  32. IClientConfig clientConfig;
  33. @Bean
  34. public IPing ping(IClientConfig clientConfig)
  35. {
  36. return new PingUrl();
  37. }
  38. @Bean
  39. public IRule ribbonRule(IClientConfig config) {
  40. return new AvailabilityFilteringRule();
  41. }
  42. }
  43. ======Configuration File======
  44. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  45. import org.springframework.context.annotation.Bean;
  46. import org.springframework.context.annotation.Configuration;
  47. import org.springframework.web.client.RestTemplate;
  48. @Configuration
  49. public class config {
  50. @LoadBalanced
  51. @Bean
  52. public RestTemplate restTemplate() {
  53. return new RestTemplate();
  54. }
  55. }
  56. ======Consumer Yaml File======
  57. server:
  58. port: 8080
  59. ProducerService:
  60. ribbon:
  61. eureka:
  62. enabled: false
  63. listOfServers: localhost:8081, localhost:8082, localhost:8083
  64. ServerListRefreshInterval: 15000
  65. ****ProducerService****
  66. ======Producer Controller======
  67. import org.springframework.beans.factory.annotation.Value;
  68. import org.springframework.web.bind.annotation.GetMapping;
  69. import org.springframework.web.bind.annotation.RestController;
  70. @RestController
  71. public class HelloController {
  72. @Value("${server.port}")
  73. private int port;
  74. @GetMapping("/value")
  75. public String hello() {
  76. return "Hello application running on port: " + port;
  77. }
  78. }
  79. ======Producer Application======
  80. import org.springframework.boot.SpringApplication;
  81. import org.springframework.boot.autoconfigure.SpringBootApplication;
  82. @SpringBootApplication
  83. public class ProducerServiceApplication {
  84. public static void main(String[] args) {
  85. SpringApplication.run(ProducerServiceApplication.class, args);
  86. }
  87. }
  88. ======Producer Properties File======
  89. server.port=8083
  90. spring.application.name=ProducerService
英文:

I am using two services namely ProducerService and ConsumerService,
and running ProducerService as ApplicationService on three ports and trying to consume ConsumerService as a client from client-side.

Is it possible to load balance without using any service registry like(eureka, consul) ??

Here, I attached the code which I have tried..

  1. ****ConsumerService****
  2. ======Consumer Controller======
  3. @RestController
  4. @RibbonClient(name="ProducerService",configuration=RibbonConfig.class)
  5. public class ConsumerController {
  6. @Autowired
  7. private RestTemplate restTemplate;
  8. @GetMapping("/hello")
  9. public String hello()
  10. {
  11. String url="http://ProducerService/value";
  12. return restTemplate.getForObject(url,String.class);
  13. }
  14. }
  15. ======Consumer Application======
  16. import org.springframework.boot.SpringApplication;
  17. import org.springframework.boot.autoconfigure.SpringBootApplication;
  18. @SpringBootApplication
  19. public class ConsumerService2Application {
  20. public static void main(String[] args) {
  21. SpringApplication.run(ConsumerService2Application.class, args);
  22. }
  23. }
  24. ======Ribbon Configuration File======
  25. import com.netflix.client.config.IClientConfig;
  26. import com.netflix.loadbalancer.AvailabilityFilteringRule;
  27. import com.netflix.loadbalancer.IPing;
  28. import com.netflix.loadbalancer.IRule;
  29. import com.netflix.loadbalancer.PingUrl;
  30. public class RibbonConfig {
  31. @Autowired
  32. IClientConfig clientConfig;
  33. @Bean
  34. public IPing ping(IClientConfig clientConfig)
  35. {
  36. return new PingUrl();
  37. }
  38. @Bean
  39. public IRule ribbonRule(IClientConfig config) {
  40. return new AvailabilityFilteringRule();
  41. }
  42. }
  43. ======Configuration File======
  44. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  45. import org.springframework.context.annotation.Bean;
  46. import org.springframework.context.annotation.Configuration;
  47. import org.springframework.web.client.RestTemplate;
  48. @Configuration
  49. public class config {
  50. @LoadBalanced
  51. @Bean
  52. public RestTemplate restTemplate() {
  53. return new RestTemplate();
  54. }
  55. }
  56. ======Consumer Yaml File======
  57. server:
  58. port: 8080
  59. ProducerService:
  60. ribbon:
  61. eureka:
  62. enabled: false
  63. listOfServers: localhost:8081,localhost:8082,localhost:8083
  64. ServerListRefreshInterval: 15000
  65. ****ProducerService****
  66. ======Producer Controller======
  67. import org.springframework.beans.factory.annotation.Value;
  68. import org.springframework.web.bind.annotation.GetMapping;
  69. import org.springframework.web.bind.annotation.RestController;
  70. @RestController
  71. public class HelloController {
  72. @Value("${server.port}")
  73. private int port;
  74. @GetMapping("/value")
  75. public String hello() {
  76. return "Hello application running on port: " + port;
  77. }
  78. }
  79. ======ProducerApplication======
  80. import org.springframework.boot.SpringApplication;
  81. import org.springframework.boot.autoconfigure.SpringBootApplication;
  82. @SpringBootApplication
  83. public class ProducerServiceApplication {
  84. public static void main(String[] args) {
  85. SpringApplication.run(ProducerServiceApplication.class, args);
  86. }
  87. }
  88. ======Producer properties File======
  89. server.port=8083
  90. spring.application.name=ProducerService

答案1

得分: 1

是的,这是可能的。您需要更新application.yml,如下所示:

  1. ProducerService:
  2. ribbon:
  3. eureka:
  4. enabled: false
  5. listOfServers: localhost:8000,localhost:8001 # 根据您的服务器进行调整
  6. ServerListRefreshInterval: 2000

restTemplate中只需正确地提供服务名称。

英文:

Yes it is possible. You need to add update the application.yml as

  1. ProducerService:
  2. ribbon:
  3. eureka:
  4. enabled: false
  5. listOfServers: localhost:8000,localhost:8001 #depends upon your server
  6. ServerListRefreshInterval: 2000

in restTemplate give the service name alone properly.

答案2

得分: 0

是的,在客户端服务中,按照以下步骤操作:
在客户端服务中
在你的 pom.xml 文件中添加 spring-cloud-starter-ribbon 依赖
然后在 application.properties 文件中添加:

  1. ribbon.eureka.enables = false
  2. ProducerService.listOfServers = http://localhost:8081,http://localhost:8082,...

现在,REST 客户端知道了 ProducerService 实例的位置。

最后,在你的控制器类中,你应该添加 @RibbonClient,然后你可以通过调用 ProducerService 来进行调用和负载均衡,而不是使用 IP 地址:

  1. @RibbonClient(name = "ProducerService")
  2. @Controller
  3. public class Controller{
  4. ...
  5. }
英文:

yes , follow this step's: <br>
in client service
<br>
add spring-cloud-starter-ribbon dependency to your pom.xml file <br>
then in application.properies file add : <br>

  1. ribbon.eureka.enables = false
  2. ProducerService.listOfServers = http://localhost:8081,http://localhost:8082,..

now rest client knows ProducerService instances location <br>

finally in your controller class you should add @RibbonClient then you can call and loadbalance by calling ProducerService instead of ip address :

  1. @RibbonClient(name = &quot;ProducerService&quot;)
  2. @Controller
  3. public class Controller{
  4. ...
  5. }

huangapple
  • 本文由 发表于 2020年9月9日 17:57:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63809211.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定