有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我使用两种服务,即ProducerService和ConsumerService, 在三个端口上以ApplicationService的形式运行ProducerService,并尝试从客户端以客户端的形式使用ConsumerService

是否可以不使用任何服务注册表(如(eureka,Consour)实现负载平衡

在这里,我附上了我尝试过的代码

****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





共 (2) 个答案

  1. # 1 楼答案

    是的,这是可能的。您需要添加更新application.yml作为

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

    在restTemplate中,正确地单独给出服务名称

  2. # 2 楼答案

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

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

    现在rest客户端知道ProducerService实例的位置

    最后,在控制器类中,您应该添加@RibbonClient,然后您可以通过调用ProducerService而不是ip地址来调用和加载平衡:

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