英文:
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 = "ProducerService")
@Controller
public class Controller{
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论