Created
June 21, 2016 15:41
-
-
Save ryanjbaxter/8452a5a591283c57e416f10535dcbef1 to your computer and use it in GitHub Desktop.
SleuthBug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; | |
import org.springframework.cloud.sleuth.sampler.AlwaysSampler; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.stereotype.Component; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RestController; | |
import org.springframework.web.client.RestTemplate; | |
import org.springframework.cloud.netflix.feign.EnableFeignClients; | |
import org.springframework.cloud.netflix.feign.FeignClient; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
@SpringBootApplication | |
@RestController | |
@EnableFeignClients | |
@EnableCircuitBreaker | |
public class SleuthSampleApplication { | |
private static final Logger LOG = Logger.getLogger(SleuthSampleApplication.class.getName()); | |
@Autowired | |
private RestTemplate restTemplate; | |
@Autowired | |
private ParticipantsBean participantsBean; | |
public static void main(String[] args) { | |
SpringApplication.run(SleuthSampleApplication.class, args); | |
} | |
@Bean | |
public RestTemplate getRestTemplate() { | |
return new RestTemplate(); | |
} | |
@Bean | |
public AlwaysSampler defaultSampler() { | |
return new AlwaysSampler(); | |
} | |
@RequestMapping("/") | |
public String home() { | |
LOG.log(Level.INFO, "you called home"); | |
return "Hello World"; | |
} | |
@RequestMapping("/callhome") | |
public String callHome() { | |
LOG.log(Level.INFO, "calling home"); | |
return restTemplate.getForObject("http://localhost:8080", String.class); | |
} | |
} | |
@Component | |
class ParticipantsBean { | |
@Autowired | |
private ParticipantsClient participantsClient; | |
@HystrixCommand(fallbackMethod = "defaultParticipants") | |
public List<Object> getParticipants(String raceId) { | |
return participantsClient.getParticipants(raceId); | |
} | |
public List<Object> defaultParticipants(String raceId) { | |
return new ArrayList<Object>(); | |
} | |
} | |
@FeignClient("participants") | |
interface ParticipantsClient { | |
@RequestMapping(method = RequestMethod.GET, value="/races/{raceId}") | |
List<Object> getParticipants(@PathVariable("raceId") String raceId); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment