Formatting changes

This commit is contained in:
caroline 2019-03-18 14:35:22 +01:00
parent ea3fe7aab9
commit 4fece2266e
10 changed files with 151 additions and 157 deletions

View File

@ -9,20 +9,19 @@ import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient @EnableDiscoveryClient
@EnableCircuitBreaker @EnableCircuitBreaker
@RibbonClient(name = "travel-agency-service", configuration = RibbonConfiguration.class) @RibbonClient(name = "travel-agency-service", configuration = RibbonConfiguration.class)
public class Application { public class Application {
@LoadBalanced @LoadBalanced
@Bean @Bean
RestTemplate restTemplate(){ RestTemplate restTemplate() {
return new RestTemplate(); return new RestTemplate();
} }
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(Application.class, args); SpringApplication.run(Application.class, args);
} }
} }

View File

@ -7,13 +7,13 @@ import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix = "bean") @ConfigurationProperties(prefix = "bean")
public class ClientConfig { 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() { public String getMessage() {
return message; return message;
} }
public void setMessage(String message) { public void setMessage(String message) {
this.message = message; this.message = message;
} }
} }

View File

@ -16,42 +16,39 @@ import java.util.List;
@RestController @RestController
public class ClientController { public class ClientController {
@Autowired @Autowired
private DiscoveryClient discoveryClient; private DiscoveryClient discoveryClient;
@Autowired @Autowired
private ClientConfig config; private ClientConfig config;
@Autowired @Autowired
private TravelAgencyService travelAgencyService; private TravelAgencyService travelAgencyService;
@RequestMapping("/deals") @RequestMapping("/deals")
public String getDeals() { public String getDeals() {
return travelAgencyService.getDeals(); return travelAgencyService.getDeals();
} }
@GetMapping @GetMapping
public String load() throws UnknownHostException { public String load() {
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
String resourceUrl String resourceUrl = "http://travel-agency-service:8080";
= "http://travel-agency-service:8080"; ResponseEntity<String> response = restTemplate.getForEntity(resourceUrl, String.class);
ResponseEntity<String> response
= restTemplate.getForEntity(resourceUrl, String.class);
String serviceList = ""; String serviceList = "";
if (discoveryClient != null) { if (discoveryClient != null) {
List<String> services = this.discoveryClient.getServices(); List<String> services = this.discoveryClient.getServices();
for (String service : services) { for (String service : services) {
List<ServiceInstance> instances = this.discoveryClient List<ServiceInstance> instances = this.discoveryClient.getInstances(service);
.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);
} }
} }

View File

@ -26,30 +26,30 @@ import org.springframework.context.annotation.Bean;
public class RibbonConfiguration { public class RibbonConfiguration {
@Autowired @Autowired
IClientConfig ribbonClientConfig; IClientConfig ribbonClientConfig;
/** /**
* PingUrl will ping a URL to check the status of each server. * PingUrl will ping a URL to check the status of each server.
* Say Hello has, as youll recall, a method mapped to the /path; that means that Ribbon will get an HTTP 200 response when it pings a running Backend Server * Say Hello has, as youll 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 * @param config Client configuration
* @return The URL to be used for the Ping * @return The URL to be used for the Ping
*/ */
@Bean @Bean
public IPing ribbonPing(IClientConfig config) { public IPing ribbonPing(IClientConfig config) {
return new PingUrl(); return new PingUrl();
} }
/** /**
* AvailabilityFilteringRule will use Ribbons built-in circuit breaker functionality to filter out any servers in an open-circuit state: * AvailabilityFilteringRule will use Ribbons 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. * 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 * @param config Client configuration
* @return The Load Balancer rule * @return The Load Balancer rule
*/ */
@Bean @Bean
public IRule ribbonRule(IClientConfig config) { public IRule ribbonRule(IClientConfig config) {
return new AvailabilityFilteringRule(); return new AvailabilityFilteringRule();
} }
} }

View File

@ -8,20 +8,19 @@ import org.springframework.web.client.RestTemplate;
@Service @Service
public class TravelAgencyService { public class TravelAgencyService {
private final RestTemplate restTemplate; private final RestTemplate restTemplate;
public TravelAgencyService(RestTemplate restTemplate) { public TravelAgencyService(RestTemplate restTemplate) {
this.restTemplate = restTemplate; this.restTemplate = restTemplate;
} }
@HystrixCommand(fallbackMethod = "getFallbackName", commandProperties = { @HystrixCommand(fallbackMethod = "getFallbackName", commandProperties =
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") })
}) public String getDeals() {
public String getDeals() { return this.restTemplate.getForObject("http://travel-agency-service:8080/deals", String.class);
return this.restTemplate.getForObject("http://travel-agency-service:8080/deals", String.class); }
}
private String getFallbackName() { private String getFallbackName() {
return "Fallback"; return "Fallback";
} }
} }

View File

@ -11,8 +11,7 @@ import com.baeldung.spring.cloud.kubernetes.client.Application;
@SpringBootTest(classes = Application.class) @SpringBootTest(classes = Application.class)
public class SpringContextIntegrationTest { public class SpringContextIntegrationTest {
@Test @Test
public void contextLoads() { public void contextLoads() {
} }
} }

View File

@ -8,7 +8,7 @@ import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication @SpringBootApplication
public class Application implements CommandLineRunner { public class Application implements CommandLineRunner {
private static final Log log = LogFactory.getLog(TravelAgencyController.class); private static final Log log = LogFactory.getLog(TravelAgencyController.class);

View File

@ -27,7 +27,8 @@ public class TravelAgencyController {
List<TravelDeal> travelDealList = travelDealRepository.findAll(); List<TravelDeal> travelDealList = travelDealRepository.findAll();
if (!travelDealList.isEmpty()) { if (!travelDealList.isEmpty()) {
int randomDeal = new Random().nextInt(travelDealList.size()); int randomDeal = new Random().nextInt(travelDealList.size());
return travelDealList.get(randomDeal).toString(); return travelDealList.get(randomDeal)
.toString();
} else { } else {
return "NO DEALS"; return "NO DEALS";
} }
@ -38,9 +39,17 @@ public class TravelAgencyController {
public String get() throws UnknownHostException { public String get() throws UnknownHostException {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Host: ").append(InetAddress.getLocalHost().getHostName()).append("<br/>"); stringBuilder.append("Host: ")
stringBuilder.append("IP: ").append(InetAddress.getLocalHost().getHostAddress()).append("<br/>"); .append(InetAddress.getLocalHost()
stringBuilder.append("Type: ").append("Travel Agency").append("<br/>"); .getHostName())
.append("<br/>");
stringBuilder.append("IP: ")
.append(InetAddress.getLocalHost()
.getHostAddress())
.append("<br/>");
stringBuilder.append("Type: ")
.append("Travel Agency")
.append("<br/>");
return stringBuilder.toString(); return stringBuilder.toString();
} }
} }

View File

@ -10,91 +10,83 @@ import java.util.Date;
@Document(collection = "travel_deal") @Document(collection = "travel_deal")
public class TravelDeal { public class TravelDeal {
@Id @Id
private BigInteger id; private BigInteger id;
private String destination; private String destination;
private String description; private String description;
@Field("deal_price") @Field("deal_price")
private double dealPrice; private double dealPrice;
@Field("old_price") @Field("old_price")
private double oldPrice; private double oldPrice;
@Field("departure_date") @Field("departure_date")
private Date departureDate; private Date departureDate;
@Field("arrival_date") @Field("arrival_date")
private Date arrivalDate; private Date arrivalDate;
public BigInteger getId() { public BigInteger getId() {
return id; return id;
} }
public void setId(BigInteger id) { public void setId(BigInteger id) {
this.id = id; this.id = id;
} }
public String getDestination() { public String getDestination() {
return destination; return destination;
} }
public void setDestination(String destination) { public void setDestination(String destination) {
this.destination = destination; this.destination = destination;
} }
public String getDescription() { public String getDescription() {
return description; return description;
} }
public void setDescription(String description) { public void setDescription(String description) {
this.description = description; this.description = description;
} }
public double getDealPrice() { public double getDealPrice() {
return dealPrice; return dealPrice;
} }
public void setDealPrice(double dealPrice) { public void setDealPrice(double dealPrice) {
this.dealPrice = dealPrice; this.dealPrice = dealPrice;
} }
public double getOldPrice() { public double getOldPrice() {
return oldPrice; return oldPrice;
} }
public void setOldPrice(double oldPrice) { public void setOldPrice(double oldPrice) {
this.oldPrice = oldPrice; this.oldPrice = oldPrice;
} }
public Date getDepartureDate() { public Date getDepartureDate() {
return departureDate; return departureDate;
} }
public void setDepartureDate(Date departureDate) { public void setDepartureDate(Date departureDate) {
this.departureDate = departureDate; this.departureDate = departureDate;
} }
public Date getArrivalDate() { public Date getArrivalDate() {
return arrivalDate; return arrivalDate;
} }
public void setArrivalDate(Date arrivalDate) { public void setArrivalDate(Date arrivalDate) {
this.arrivalDate = arrivalDate; this.arrivalDate = arrivalDate;
} }
@Override @Override
public String toString() { public String toString() {
return "TravelDeal{" + return "TravelDeal{" + "id=" + id + ", destination='" + destination + '\'' + ", description='" + description + '\'' + ", dealPrice=" + dealPrice + ", oldPrice=" + oldPrice + ", departureDate=" + departureDate + ", arrivalDate=" + arrivalDate + '}';
"id=" + id + }
", destination='" + destination + '\'' +
", description='" + description + '\'' +
", dealPrice=" + dealPrice +
", oldPrice=" + oldPrice +
", departureDate=" + departureDate +
", arrivalDate=" + arrivalDate +
'}';
}
} }

View File

@ -9,5 +9,4 @@ public interface TravelDealRepository extends MongoRepository<TravelDeal, String
public List<TravelDeal> findByDestination(String destination); public List<TravelDeal> findByDestination(String destination);
} }