Formatting changes
This commit is contained in:
parent
ea3fe7aab9
commit
4fece2266e
@ -9,20 +9,19 @@ import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableDiscoveryClient
|
||||
@EnableCircuitBreaker
|
||||
@RibbonClient(name = "travel-agency-service", configuration = RibbonConfiguration.class)
|
||||
public class Application {
|
||||
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
RestTemplate restTemplate(){
|
||||
return new RestTemplate();
|
||||
}
|
||||
@LoadBalanced
|
||||
@Bean
|
||||
RestTemplate restTemplate() {
|
||||
return new RestTemplate();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
|
@ -7,13 +7,13 @@ import org.springframework.context.annotation.Configuration;
|
||||
@ConfigurationProperties(prefix = "bean")
|
||||
public class ClientConfig {
|
||||
|
||||
private String message = "Message from backend is: %s <br/> Services : %s";
|
||||
private String message = "Message from backend is: %s <br/> Services : %s";
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
}
|
@ -16,42 +16,39 @@ import java.util.List;
|
||||
@RestController
|
||||
public class ClientController {
|
||||
|
||||
@Autowired
|
||||
private DiscoveryClient discoveryClient;
|
||||
@Autowired
|
||||
private DiscoveryClient discoveryClient;
|
||||
|
||||
@Autowired
|
||||
private ClientConfig config;
|
||||
@Autowired
|
||||
private ClientConfig config;
|
||||
|
||||
@Autowired
|
||||
private TravelAgencyService travelAgencyService;
|
||||
@Autowired
|
||||
private TravelAgencyService travelAgencyService;
|
||||
|
||||
@RequestMapping("/deals")
|
||||
public String getDeals() {
|
||||
return travelAgencyService.getDeals();
|
||||
}
|
||||
@RequestMapping("/deals")
|
||||
public String getDeals() {
|
||||
return travelAgencyService.getDeals();
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String load() throws UnknownHostException {
|
||||
@GetMapping
|
||||
public String load() {
|
||||
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String resourceUrl
|
||||
= "http://travel-agency-service:8080";
|
||||
ResponseEntity<String> response
|
||||
= restTemplate.getForEntity(resourceUrl, String.class);
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
String resourceUrl = "http://travel-agency-service:8080";
|
||||
ResponseEntity<String> response = restTemplate.getForEntity(resourceUrl, String.class);
|
||||
|
||||
String serviceList = "";
|
||||
if (discoveryClient != null) {
|
||||
List<String> services = this.discoveryClient.getServices();
|
||||
String serviceList = "";
|
||||
if (discoveryClient != null) {
|
||||
List<String> services = this.discoveryClient.getServices();
|
||||
|
||||
for (String service : services) {
|
||||
for (String service : services) {
|
||||
|
||||
List<ServiceInstance> instances = this.discoveryClient
|
||||
.getInstances(service);
|
||||
List<ServiceInstance> instances = this.discoveryClient.getInstances(service);
|
||||
|
||||
serviceList += ("[" + service + " : " + ((!CollectionUtils.isEmpty(instances))?instances.size():0)+ " instances ]");
|
||||
}
|
||||
}
|
||||
serviceList += ("[" + service + " : " + ((!CollectionUtils.isEmpty(instances)) ? instances.size() : 0) + " instances ]");
|
||||
}
|
||||
}
|
||||
|
||||
return String.format(config.getMessage(), response.getBody(),serviceList);
|
||||
}
|
||||
return String.format(config.getMessage(), response.getBody(), serviceList);
|
||||
}
|
||||
}
|
||||
|
@ -26,30 +26,30 @@ import org.springframework.context.annotation.Bean;
|
||||
|
||||
public class RibbonConfiguration {
|
||||
|
||||
@Autowired
|
||||
IClientConfig ribbonClientConfig;
|
||||
@Autowired
|
||||
IClientConfig ribbonClientConfig;
|
||||
|
||||
/**
|
||||
* PingUrl will ping a URL to check the status of each server.
|
||||
* Say Hello has, as you’ll recall, a method mapped to the /path; that means that Ribbon will get an HTTP 200 response when it pings a running Backend Server
|
||||
*
|
||||
* @param config Client configuration
|
||||
* @return The URL to be used for the Ping
|
||||
*/
|
||||
@Bean
|
||||
public IPing ribbonPing(IClientConfig config) {
|
||||
return new PingUrl();
|
||||
}
|
||||
/**
|
||||
* PingUrl will ping a URL to check the status of each server.
|
||||
* Say Hello has, as you’ll recall, a method mapped to the /path; that means that Ribbon will get an HTTP 200 response when it pings a running Backend Server
|
||||
*
|
||||
* @param config Client configuration
|
||||
* @return The URL to be used for the Ping
|
||||
*/
|
||||
@Bean
|
||||
public IPing ribbonPing(IClientConfig config) {
|
||||
return new PingUrl();
|
||||
}
|
||||
|
||||
/**
|
||||
* AvailabilityFilteringRule will use Ribbon’s built-in circuit breaker functionality to filter out any servers in an “open-circuit” state:
|
||||
* if a ping fails to connect to a given server, or if it gets a read failure for the server, Ribbon will consider that server “dead” until it begins to respond normally.
|
||||
*
|
||||
* @param config Client configuration
|
||||
* @return The Load Balancer rule
|
||||
*/
|
||||
@Bean
|
||||
public IRule ribbonRule(IClientConfig config) {
|
||||
return new AvailabilityFilteringRule();
|
||||
}
|
||||
/**
|
||||
* AvailabilityFilteringRule will use Ribbon’s built-in circuit breaker functionality to filter out any servers in an “open-circuit” state:
|
||||
* if a ping fails to connect to a given server, or if it gets a read failure for the server, Ribbon will consider that server “dead” until it begins to respond normally.
|
||||
*
|
||||
* @param config Client configuration
|
||||
* @return The Load Balancer rule
|
||||
*/
|
||||
@Bean
|
||||
public IRule ribbonRule(IClientConfig config) {
|
||||
return new AvailabilityFilteringRule();
|
||||
}
|
||||
}
|
||||
|
@ -8,20 +8,19 @@ import org.springframework.web.client.RestTemplate;
|
||||
@Service
|
||||
public class TravelAgencyService {
|
||||
|
||||
private final RestTemplate restTemplate;
|
||||
private final RestTemplate restTemplate;
|
||||
|
||||
public TravelAgencyService(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
public TravelAgencyService(RestTemplate restTemplate) {
|
||||
this.restTemplate = restTemplate;
|
||||
}
|
||||
|
||||
@HystrixCommand(fallbackMethod = "getFallbackName", commandProperties = {
|
||||
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
|
||||
})
|
||||
public String getDeals() {
|
||||
return this.restTemplate.getForObject("http://travel-agency-service:8080/deals", String.class);
|
||||
}
|
||||
@HystrixCommand(fallbackMethod = "getFallbackName", commandProperties =
|
||||
{ @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") })
|
||||
public String getDeals() {
|
||||
return this.restTemplate.getForObject("http://travel-agency-service:8080/deals", String.class);
|
||||
}
|
||||
|
||||
private String getFallbackName() {
|
||||
return "Fallback";
|
||||
}
|
||||
private String getFallbackName() {
|
||||
return "Fallback";
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,7 @@ import com.baeldung.spring.cloud.kubernetes.client.Application;
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class SpringContextIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
}
|
||||
|
@ -8,7 +8,7 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application implements CommandLineRunner {
|
||||
public class Application implements CommandLineRunner {
|
||||
|
||||
private static final Log log = LogFactory.getLog(TravelAgencyController.class);
|
||||
|
||||
|
@ -27,7 +27,8 @@ public class TravelAgencyController {
|
||||
List<TravelDeal> travelDealList = travelDealRepository.findAll();
|
||||
if (!travelDealList.isEmpty()) {
|
||||
int randomDeal = new Random().nextInt(travelDealList.size());
|
||||
return travelDealList.get(randomDeal).toString();
|
||||
return travelDealList.get(randomDeal)
|
||||
.toString();
|
||||
} else {
|
||||
return "NO DEALS";
|
||||
}
|
||||
@ -38,9 +39,17 @@ public class TravelAgencyController {
|
||||
public String get() throws UnknownHostException {
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.append("Host: ").append(InetAddress.getLocalHost().getHostName()).append("<br/>");
|
||||
stringBuilder.append("IP: ").append(InetAddress.getLocalHost().getHostAddress()).append("<br/>");
|
||||
stringBuilder.append("Type: ").append("Travel Agency").append("<br/>");
|
||||
stringBuilder.append("Host: ")
|
||||
.append(InetAddress.getLocalHost()
|
||||
.getHostName())
|
||||
.append("<br/>");
|
||||
stringBuilder.append("IP: ")
|
||||
.append(InetAddress.getLocalHost()
|
||||
.getHostAddress())
|
||||
.append("<br/>");
|
||||
stringBuilder.append("Type: ")
|
||||
.append("Travel Agency")
|
||||
.append("<br/>");
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
}
|
||||
|
@ -10,91 +10,83 @@ import java.util.Date;
|
||||
@Document(collection = "travel_deal")
|
||||
public class TravelDeal {
|
||||
|
||||
@Id
|
||||
private BigInteger id;
|
||||
@Id
|
||||
private BigInteger id;
|
||||
|
||||
private String destination;
|
||||
private String destination;
|
||||
|
||||
private String description;
|
||||
private String description;
|
||||
|
||||
@Field("deal_price")
|
||||
private double dealPrice;
|
||||
@Field("deal_price")
|
||||
private double dealPrice;
|
||||
|
||||
@Field("old_price")
|
||||
private double oldPrice;
|
||||
@Field("old_price")
|
||||
private double oldPrice;
|
||||
|
||||
@Field("departure_date")
|
||||
private Date departureDate;
|
||||
@Field("departure_date")
|
||||
private Date departureDate;
|
||||
|
||||
@Field("arrival_date")
|
||||
private Date arrivalDate;
|
||||
@Field("arrival_date")
|
||||
private Date arrivalDate;
|
||||
|
||||
public BigInteger getId() {
|
||||
return id;
|
||||
}
|
||||
public BigInteger getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(BigInteger id) {
|
||||
this.id = id;
|
||||
}
|
||||
public void setId(BigInteger id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getDestination() {
|
||||
return destination;
|
||||
}
|
||||
public String getDestination() {
|
||||
return destination;
|
||||
}
|
||||
|
||||
public void setDestination(String destination) {
|
||||
this.destination = destination;
|
||||
}
|
||||
public void setDestination(String destination) {
|
||||
this.destination = destination;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public double getDealPrice() {
|
||||
return dealPrice;
|
||||
}
|
||||
public double getDealPrice() {
|
||||
return dealPrice;
|
||||
}
|
||||
|
||||
public void setDealPrice(double dealPrice) {
|
||||
this.dealPrice = dealPrice;
|
||||
}
|
||||
public void setDealPrice(double dealPrice) {
|
||||
this.dealPrice = dealPrice;
|
||||
}
|
||||
|
||||
public double getOldPrice() {
|
||||
return oldPrice;
|
||||
}
|
||||
public double getOldPrice() {
|
||||
return oldPrice;
|
||||
}
|
||||
|
||||
public void setOldPrice(double oldPrice) {
|
||||
this.oldPrice = oldPrice;
|
||||
}
|
||||
public void setOldPrice(double oldPrice) {
|
||||
this.oldPrice = oldPrice;
|
||||
}
|
||||
|
||||
public Date getDepartureDate() {
|
||||
return departureDate;
|
||||
}
|
||||
public Date getDepartureDate() {
|
||||
return departureDate;
|
||||
}
|
||||
|
||||
public void setDepartureDate(Date departureDate) {
|
||||
this.departureDate = departureDate;
|
||||
}
|
||||
public void setDepartureDate(Date departureDate) {
|
||||
this.departureDate = departureDate;
|
||||
}
|
||||
|
||||
public Date getArrivalDate() {
|
||||
return arrivalDate;
|
||||
}
|
||||
public Date getArrivalDate() {
|
||||
return arrivalDate;
|
||||
}
|
||||
|
||||
public void setArrivalDate(Date arrivalDate) {
|
||||
this.arrivalDate = arrivalDate;
|
||||
}
|
||||
public void setArrivalDate(Date arrivalDate) {
|
||||
this.arrivalDate = arrivalDate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TravelDeal{" +
|
||||
"id=" + id +
|
||||
", destination='" + destination + '\'' +
|
||||
", description='" + description + '\'' +
|
||||
", dealPrice=" + dealPrice +
|
||||
", oldPrice=" + oldPrice +
|
||||
", departureDate=" + departureDate +
|
||||
", arrivalDate=" + arrivalDate +
|
||||
'}';
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "TravelDeal{" + "id=" + id + ", destination='" + destination + '\'' + ", description='" + description + '\'' + ", dealPrice=" + dealPrice + ", oldPrice=" + oldPrice + ", departureDate=" + departureDate + ", arrivalDate=" + arrivalDate + '}';
|
||||
}
|
||||
}
|
||||
|
@ -9,5 +9,4 @@ public interface TravelDealRepository extends MongoRepository<TravelDeal, String
|
||||
|
||||
public List<TravelDeal> findByDestination(String destination);
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user