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
@@ -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 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
* @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 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
* @return The URL to be used for the Ping
*/
@Bean
public IPing ribbonPing(IClientConfig config) {
return new PingUrl();
}
/**
* 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.
*
* @param config Client configuration
* @return The Load Balancer rule
*/
@Bean
public IRule ribbonRule(IClientConfig config) {
return new AvailabilityFilteringRule();
}
/**
* 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.
*
* @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() {
}
}