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

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

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

问题

****ConsumerService****

======Consumer Controller======

@RestController
@RibbonClient(name="ProducerService", configuration=RibbonConfig.class)
public class ConsumerController {

	@Autowired
	private RestTemplate restTemplate;
	
	@GetMapping("/hello")
	public String hello()
	{
		String url = "http://ProducerService/value";
		return restTemplate.getForObject(url, String.class);
	}
}

======Consumer Application======

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerService2Application {

	public static void main(String[] args) {
		SpringApplication.run(ConsumerService2Application.class, args);
	}
}

======Ribbon Configuration File======

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AvailabilityFilteringRule;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PingUrl;

public class RibbonConfig {
	
	@Autowired
	IClientConfig clientConfig;
	
	@Bean
	public IPing ping(IClientConfig clientConfig)
	{
		return new PingUrl();
	}
	
	@Bean
    public IRule ribbonRule(IClientConfig config) {
        return new AvailabilityFilteringRule();
    }
}

======Configuration File======

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class config {	
	
	@LoadBalanced
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
  
}

======Consumer Yaml File======

server:
  port: 8080
ProducerService:
  ribbon:
    eureka:
      enabled: false
    listOfServers: localhost:8081, localhost:8082, localhost:8083
    ServerListRefreshInterval: 15000
	
	
	
****ProducerService****

======Producer Controller======

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	@Value("${server.port}")
	private int port;

	@GetMapping("/value")
	public String hello() {
		return "Hello application running on port: " + port;
	}
}

======Producer Application======

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProducerServiceApplication {

	public static void main(String[] args) {
		SpringApplication.run(ProducerServiceApplication.class, args);
	}
}

======Producer Properties File======

server.port=8083
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..

****ConsumerService****

======Consumer Controller======

@RestController
@RibbonClient(name="ProducerService",configuration=RibbonConfig.class)
public class ConsumerController {

	@Autowired
	private RestTemplate restTemplate;
	
	@GetMapping("/hello")
	public String hello()
	{
		String url="http://ProducerService/value";
		return restTemplate.getForObject(url,String.class);
	}
}

======Consumer Application======

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerService2Application {

	public static void main(String[] args) {
		SpringApplication.run(ConsumerService2Application.class, args);
	}
}

======Ribbon Configuration File======

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AvailabilityFilteringRule;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.PingUrl;

public class RibbonConfig {
	
	@Autowired
	IClientConfig clientConfig;
	
	@Bean
	public IPing ping(IClientConfig clientConfig)
	{
		return new PingUrl();
	}
	
	@Bean
    public IRule ribbonRule(IClientConfig config) {
        return new AvailabilityFilteringRule();
    }
}

======Configuration File======

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class config {	
	
	@LoadBalanced
	@Bean
	public RestTemplate restTemplate() {
		return new RestTemplate();
	}
	
  
}

======Consumer Yaml File======

server:
  port: 8080
ProducerService:
  ribbon:
    eureka:
      enabled: false
    listOfServers: localhost:8081,localhost:8082,localhost:8083
    ServerListRefreshInterval: 15000
	
	
	
****ProducerService****

======Producer Controller======
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

	@Value("${server.port}")
	private int port;

	@GetMapping("/value")
	public String hello() {
		return "Hello application running on port: " + port;
	}
}

======ProducerApplication======
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProducerServiceApplication {

	public static void main(String[] args) {
		SpringApplication.run(ProducerServiceApplication.class, args);
	}
}

======Producer properties File======

server.port=8083
spring.application.name=ProducerService




答案1

得分: 1

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

ProducerService:
  ribbon:
    eureka:
      enabled: false
    listOfServers: localhost:8000,localhost:8001 # 根据您的服务器进行调整
    ServerListRefreshInterval: 2000

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

英文:

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

ProducerService:
ribbon:
eureka:
enabled: false
listOfServers: localhost:8000,localhost:8001 #depends upon your server
ServerListRefreshInterval: 2000

in restTemplate give the service name alone properly.

答案2

得分: 0

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

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

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

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

@RibbonClient(name = "ProducerService")
@Controller
public class Controller{
    ...
}
英文:

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>

ribbon.eureka.enables = false 
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 :

@RibbonClient(name = &quot;ProducerService&quot;)
@Controller
public class Controller{
...
}

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:

确定