使用Java创建负载均衡器


该架构包含三个应用程序

  • Load balancing server [netflix-eureka-naming-server]
  • Server application [micro-service-server]
  • Client application [micro-service-client]

系统的网络架构 networkarchitecture.png

运行应用程序的步骤

  • 安装JDK 11或最新版本。
  • 将项目的git仓库克隆到本地。
  • GitHub:https : //github.com/VishnuViswam/LOAD-BALANCER
  • 首先运行Netflix-eureka-naming-server应用程序。
  • 然后在两个端口中运行一个微服务服务器应用程序。
  • 最后运行微服务客户端。

Working and Configurations 1)负载平衡服务器 所有客户端-服务器通信都将通过此负载平衡服务器完成。

pom.xml 我们正在使用Netflix -eureka-server库来启用客户端和服务器之间的通信。

<properties>
    <java.version>11</java.version>  
    <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
</properties> 
<dependencies>
  <dependency>    
    <groupId>org.springframework.cloud</groupId>    
    <artifactId>spring-cloud-starter-config</artifactId>
  </dependency>  
  <dependency>
    <groupId>org.springframework.cloud</groupId>    
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>  </dependency> 
</dependencies>

application.properties

# application unique name
spring.application.name=netflix-eureka-naming-server
# it will be the default port which eureka naming server 
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

NetflixEurekaNamingServerApplication.java @EnableEurekaServer批注将允许eureka服务器控制此应用程序。

@SpringBootApplication
@EnableEurekaServer // to enable the communication with Eureka server 
public class NetflixEurekaNamingServerApplication { 
    public static void main(String[] args) {         SpringApplication.run(NetflixEurekaNamingServerApplication.class, args);     
                                           } 
}

运行此应用程序后,我们可以通过以下URL访问Eureka服务器仪表板

网址:http:// localhost:8761

Eureka Server Dashboard

eurekaserver1.png

2)服务器应用 要执行负载分配,此应用程序需要在两个实例中运行。 spring-cloud-starter-Netflix-eureka-client用于启用与Eureka命名服务器的通信

<dependency>
  <groupId>org.springframework.cloud</groupId>  
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

pom.XML

<properties>  
  <java.version>11</java.version>  
  <spring-cloud.version>Hoxton.SR4</spring-cloud.version> 
</properties> 
<dependencies>  
  <dependency>    
    <groupId>org.springframework.cloud</groupId>    
    <artifactId>spring-cloud-starter-config</artifactId> 
  </dependency> 
  <dependency>  
    <groupId>org.springframework.cloud</groupId>  
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>     </dependency>
</dependencies>

application. properties

# application unique name 
spring.application.name=micro-service-server
# application will be running under this port 
server.port=4000
# end point of load balancing server 
eureka.client.service-url.default-zone=http://localhost:8761/eureka

MicroServiceServerApplication.java @EnableDiscoveryClient批注将应用程序注册到eureka服务器。

@SpringBootApplication
@EnableDiscoveryClient
public class MicroServiceServerApplication { 
    public static void main(String[] args) {         SpringApplication.run(MicroServiceServerApplication.class, args);
                                           } 
}

ServerController .java 这是一个普通的休息控制器类

@RestController
@RequestMapping("/server")
public class ServerController {

    @Autowired
    private Environment environment;

    @GetMapping("/technologyInfo/{platform}")
    public ResponseModel retrieveTechnologyInfo(@PathVariable("platform") String platform) {
        ResponseModel responseModel = new ResponseModel();

        if (platform.equalsIgnoreCase("Java")) {
            responseModel.setTittle("Technology Stack");
            responseModel.setPlatform("Java");
            responseModel.setUsedFor("Secured Web Services");
        } else if (platform.equalsIgnoreCase("python")) {
            responseModel.setTittle("Technology Stack");
            responseModel.setPlatform("python");
            responseModel.setUsedFor("Machine Learning");
        } else {
            responseModel.setTittle("Technology Stack");
            responseModel.setPlatform(platform);
            responseModel.setUsedFor("Unknown platform");
        }

        responseModel.setServerPort(Short.parseShort(environment.getProperty("local.server.port")));

        return responseModel;

    }

    ;
}

ResponseModel.java 这是传统的模型类。

public class ResponseModel {
  private String tittle;
  private String platform;
  private String usedFor;
  private Short serverPort;
  /** constructor with getters and setters **/
}

在两个端口中运行服务器应用程序实例 首先,只需使用main方法将应用程序作为Java应用程序运行即可。要在另一个端口中运行另一个实例,我们需要在IDE中编辑“运行/调试配置”。

在IntelliJ中 单击“编辑配置”选项,它将在菜单栏的右上角提供。

ide1.png

它将打开一个窗口,如下所示。然后启用允许并行运行,然后按应用。

ide2.png

现在,将属性文件中的端口更改为4001。然后再次运行。

在Eclipse中 右键单击主类->单击属性->选择主类->单击新按钮并添加-Server。VM参数中的port = 4001,如下图所示。

eclipse1.png eclipse2.png

测试应用

然后选择新配置并运行。现在,这两个服务器实例将出现在eureka服务器仪表板中。

3)客户申请 该应用程序将充当编写在主服务器中的API的使用者。 它根据负载平衡器的可用性从两个主服务器实例中使用API​​。 我们还使用Netflix -eureka-client库与负载平衡器应用程序进行通信。 OpenFeign 我们正在使用OpenFeign来使用API​​,而不是使用传统的HTTP库。 OpenFeign将充当服务器和客户端之间的代理。

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId> 
</dependency>

Eureka Client and Ribbon

  • 功能区将执行客户端中服务器的自动切换
  • Eureka将帮助我们根据流量将主服务器实例动态添加到负载均衡器。
<dependency>  
  <groupId>org.springframework.cloud</groupId>  
  <artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency> 
  <groupId>org.springframework.cloud</groupId> 
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

pom.xml

<properties> 
  <java.version>11</java.version> 
  <spring-cloud.version>Hoxton.SR4</spring-cloud.version>
</properties> 
<dependencies> 
  <dependency>  
    <groupId>org.springframework.cloud</groupId>   
    <artifactId>spring-cloud-starter-config</artifactId> 
  </dependency> 
  <dependency> 
    <groupId>org.springframework.cloud</groupId> 
    <artifactId>spring-cloud-starter-openfeign</artifactId> 
  </dependency> 
  <dependency>  
    <groupId>org.springframework.cloud</groupId>  
    <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> 
  </dependency> 
  <dependency>   
    <groupId>org.springframework.cloud</groupId>  
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>  </dependency>
</dependencies>

application.properties

# application unique name 
server.servlet.contextPath=/microservice
# application will be running under this port 
spring.application.name=micro-service-client server.port=5000
# end point of load balancing server 
eureka.client.service-url.default-zone=http://localhost:8761/eureka

MicroServiceClientApplication.java @EnableDiscoveryClient批注用于在主类中向eureka服务器注册应用程序。 @EnableFeignClients批注用于连接伪装库。

@SpringBootApplication
@EnableFeignClients("com.microservices.client") 
@EnableDiscoveryClient
public class MicroServiceClientApplication {  
  public static void main(String[] args) { 
    SpringApplication.run(MicroServiceClientApplication.class, args);  
                                         } 
}

ClientController.java 这是一个普通的休息控制器类

@RestController
@RequestMapping("/client")
public class ClientController {
  @Autowired
  private ApiProxy apiProxy;
  @GetMapping("/technologyInfo/{platform}")
  public ResponseModel getTechnologyInfo(@PathVariable("platform") String platform) {
    /**API calling using proxy interface and mapping into ResponseModel named Object.**/
    ResponseModel responseModel = apiProxy.retrieveTechnologyInfo(platform);
    return responseModel;
                                                                                    }
}

ApiProxy.java 充当API和客户端之间的代理类。

  • @FeignClient(name =“ micro-service-server”)批注 将配置API公开的应用程序名称的信息。
  • @RibbonClient(name =“ micro-service-server”)批注将为负载均衡服务器名称提供客户端应用程序。
@FeignClient(name = "micro-service-server"
@RibbonClient(name = "micro-service-server")
public interface ApiProxy {
  @GetMapping("/server/technologyInfo/{platform}")
  ResponseModel retrieveTechnologyInfo(@PathVariable("platform") String platform);
}

ResponseModel.java 这是传统的模型类。

public class ResponseModel {
  private String tittle;
  private String platform;
  private String usedFor;
  private Short serverPort;
  /** constructor with getters and setters **/
}

运行客户端应用程序后,此应用程序的实例也会出现在eureka服务器仪表板中。 最后,Eureka服务器仪表板将如下所示。

eurekaserver2.png

结果 所有客户端应用程序API都可以看到负载平衡的魔力。 URI:http:// localhost:5000 / microservice / client / technologyInfo / java

回复:

{"tittle":"Technology Stack","platform":"Java","usedFor":"Secured Web Services","serverPort":4000}

刷新:

{"tittle":"Technology Stack","platform":"Java","usedFor":"Secured Web Services","serverPort":4001}

从结果中,我们可以通过识别端口更改来了解API响应是从不同服务器接收的。


原文链接:http://codingdict.com