SpringBoot7AM 03022021
SpringBoot7AM 03022021
-------------------------------------------------------------
LoadBalancerClient (LBC)
*) Spring Cloud Netflix has provided Ribbon, that provides Impl for
LoadBalancerClient(I) which must be used at consumer side.
[Client Side Load Balancer]
*** Server Side Load Balance: Running Producer App multiple times
[Horizontal Scaling]
Provide as:
eureka.instance.instance-id=${spring.application.name}:${random.value}
--application.properties--
server.port=8761
eureka.client.fetch-registry=false
eureka.client.register-with-eureka=false
--------------------------------------------------------------
2. Producer Application
Name : SpringCloudLBCBranchService
Dep : Eureka Discovery Client, Web
---application.properties---
server.port=9904
spring.application.name=BRANCH-SERVICE
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
eureka.instance.instance-id=${spring.application.name}:${random.value}
#eureka.client.fetch-registry=true
#eureka.client.register-with-eureka=true
-----------------------------------
> RestController
package in.nareshit.raghu.rest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/branch")
public class BranchRestController {
@Value("${server.port}")
private String port;
@GetMapping("/info")
public ResponseEntity<String> getData() {
return ResponseEntity.ok("FROM BRANCH SERVICE => " + port);
}
}
------------------------------------------------------
3. Consumer Application
Name : SpringCloudLBCCompanyService
Dep : Eureka Discovery Client, Web, Ribbon
*) RestConsumer
package in.nareshit.raghu.consumer;
import java.net.URI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
@Component
public class BranchRestConsumer {
*) RestController
package in.nareshit.raghu.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import in.nareshit.raghu.consumer.BranchRestConsumer;
@RestController
@RequestMapping("/company")
public class CompanyRestController {
@Autowired
private BranchRestConsumer consumer;
@GetMapping("/details")
public ResponseEntity<String> viewMsg() {
String body ="FROM COMPANY =>" + consumer.getBranchInfo();
return ResponseEntity.ok(body);
}
}
--------------Execution----------------------
1) Run Eureka Server (one time)
2) Run Producer (BranchService) 3 times by chaning PORT number
3) Run Consumer (CompanyService ) 1 time
4) Goto Eureka Server and check all instances
5) Click on (Company) Consumer link and convert Full URL
http://192.168.0.8:8686/actuator/info
http://192.168.0.8:8686/company/details
6) Refresh to see LoadBalancer output.
---------------------------------------------------
Q) What are meaning for below words?
RELEASE, GA, MileStone, SNAPSHOT