I have following service:
#FeignClient(name = "person", fallback = FeignHystrixFallback.class)
public interface PersonService {
#RequestMapping(value = "/find", method = RequestMethod.GET)
Person findPerson(#RequestParam("name") String name);
}
How to change the default timeout and thread pool size?
There are other people that have run into this issue and have posted questions and have answers. The most relevant is this post:
Feign builder timeouts not working
If you are wanting to manage the configuration of Feign you would want to check out the Feign documentation looking at the "configuration" attribute of the #FeignClient annotation.
Set custom configuration for this interface
#FeignClient(name="person", configuration = FeignConfig.class)
and make configuration
public class FeignConfig {
public static final int FIVE_SECONDS = 5000;
#Bean
public Request.Options options() {
return new Request.Options(FIVE_SECONDS, FIVE_SECONDS);
}
}
Related
I'm having some issues with #ConditionaOnProperty and #Configuration behavior not being updated based on the changes in the application properties (config file).
Here's what I have
Configuration
#Configuration
public class RandomRestConfig {
#Value("${external.message.root.uri}")
private String rootUri;
#Bean
#RefreshScope
public RestTemplate randomRestTemplate() {
return new RestTemplateBuilder()
.rootUri(rootUri)
.build();
}
}
Conditional service
#Service
#RefreshScope
#ConditionalOnProperty(value = "external.message.enabled", havingValue = "true")
public class RandomRestService {
#Autowired
#Qualifier("randomRestTemplate")
private RestTemplate restTemplate;
public String getMessageFromService() {
final var response = restTemplate.getForEntity("/trips/trip-text", String.class);
return response.getBody();
}
}
Usage via controller
#RefreshScope
#RestController
public class MessageRestController {
#Value("${message: No message found}")
private String message;
private RandomRestService randomRestService;
public MessageRestController(Optional<RandomRestService> optionalRestService) {
optionalRestService.ifPresent(service -> this.randomRestService = service);
}
#GetMapping("/external-message")
String getExternalMessage() {
if (randomRestService == null) {
throw new RuntimeException("Invalid request - rest is disabled");
}
return randomRestService.getMessageFromService();
}
}
Now, what I'm trying to achieve are
Change the value of the rootUri. I changed it in the config file but it didn't take effect, the old URI is still in effect.
Change the value of external.message.enabled from false to true, but the service is still null in MessageRestController. I was expecting that the bean will be updated.
Now, with both scenarios, I manually triggered the actuator /refresh endpoint and both properties were visible in the response.
[
"config.client.version",
"external.message.root.uri",
"external.message.enabled"
]
Am I missing something? Or is it possible at all?
Thank you!
I have an application with eureka, ribbon and feign. I have a feign RequestInterceptor, but the problem is that I need to know which is the host where I'm making the call. So far, with my current code I just can get the path with the RequestTemplate, but not the host.
Any idea?
I'm new to this as well, but I believe I've just learned something relevant to your question.
In the service you're creating, each instance can be given some unique identifier tied to a property included in its instance profile. Some .properties examples below:
application.properties (shared properties between instances)
spring.application.name=its-a-service
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
application-instance1.properties
server.port=5678
instance.uid=some-unique-property
application-instance2.properties
server.port=8765
instance.uid=other-unique-property
This service, as an extremely contrived example will show, can send out #Value annotated attributes to be consumed by the Ribbon app:
#SpringBootApplication
#EnableDiscoveryClient
#RestController
public class ItsAServiceApplication {
#Value("${server.port}")
private int port;
#Value("${instance.uid}")
private String instanceIdentifier;
public static void main(String[] args) {
SpringApplication.run(ItsAServiceApplication.class, args);
}
#RequestMapping
public String identifyMe() {
return "Instance: " + instanceIdentifier + ". Running on port: " + port + ".";
}
}
And just to complete the example, the Ribbon app that might consume these properties could look like this:
#SpringBootApplication
#EnableDiscoveryClient
#RestController
public class ServiceIdentifierAppApplication {
#Autowired
private RestTemplate restTemplate;
public static void main(String[] args) {
SpringApplication.run(ServiceIdentifierAppApplication.class, args);
}
#GetMapping
public String identifyMe() {
return restTemplate.getForEntity("http://its-a-service", String.class).getBody();
}
#Bean
#LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
Results:
Reloading the rest template created by the services
As I said earlier, I'm pretty new to this and have just been learning myself. Hopefully this has given you an idea for how you might send these properties! I would imagine creating a dynamic property identifier through Spring Cloud Config would be ideal here.
I have two services registered with eureka. Service C calls service A. Service C is feign client. I want implement feign client manually. But I catch an exception:
com.netflix.client.ClientException: Load balancer does not have
available server for client: service-test-a
Application class:
#EnableEurekaClient
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Feign interface:
#Component
public interface FeignService {
#RequestLine("GET /")
public String getServiceA();
}
Feign config:
#Configuration
#Import(FeignClientsConfiguration.class)
public class MyConfig {
}
Controller:
#RestController
public class Controller {
private FeignService feignService;
#Autowired
public void Controller() {
feignService = Feign.builder()
.client(RibbonClient.create())
.target(FeignService.class, "http://service-test-a");
}
#RequestMapping(value = "/build", method = RequestMethod.GET)
public String getServiceC() {
return feignService.getServiceA();
}
}
What am I doing wrong?
AFAIK, there is no easy way of using OpenFeign with eureka. There is no guide or example for that. Also I guess that it may require some additional implementations and configuration.
Instead, please try to use Spring Cloud Feign. It provides full integration with eureka and ribbon without any additional implementation. You can use Spring Cloud Feign with just a few changes in your above code.
Please refer to Spring Cloud Feign
According to the samples here (actually - timestamp task), I have implemented a small task class:
#SpringBootApplication
#EnableTask
#EnableConfigurationProperties({ RestProcessorTaskProperties.class })
public class RestProcessorTaskApplication {
public static void main(String[] args) {
SpringApplication.run(RestProcessorTaskApplication.class, args);
}
#Autowired
private RestProcessorTaskProperties config;
// some fields and beans
#Bean
public CommandLineRunner run(RestTemplate restTemplate) {
return args -> {
// doing some stuff
};
}
}
and then I've created Properties class (in the same package)
#ConfigurationProperties("RestProcessor")
public class RestProcessorTaskProperties {
private String host = "http://myhost:port";
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
}
But after I've registered task on my local Spring Cloud Data Server, I see numerous parameters, that, I suppose, was added automatically. I those mean parameters like:
abandon-when-percentage-full java.lang.Integer
abandoned-usage-tracking java.lang.Boolean
acceptors java.lang.Integer
access-to-underlying-connection-allowed java.lang.Boolean
and others...
Is it possible somehow to hide (or remove) them, so that when launching task I could configure only those parameters, that was added by me (single host property in my example above)?
By default Spring Cloud Data Flow will show you all the available properties for a boot application. However, you can create a whitelist of properties that you wish to show.
Here is a link to the Spring Cloud Data Flow reference doc that will discuss how to do this: http://docs.spring.io/spring-cloud-dataflow/docs/current/reference/htmlsingle/#spring-cloud-dataflow-stream-app-whitelisting.
And here is link to the timestamp starter app that has an example of this: https://github.com/spring-cloud/spring-cloud-task-app-starters/tree/master/spring-cloud-starter-task-timestamp
I have configured Zuul with static routes to other micro services. Is there a way to enable CircuitBreaker when calling out to other services?
As you said Zuul will automatically wrap every routes inside Ribbon and Hystrix. But it's also quite easy to integrate with Ribbon and Hystrix between microservices. You can also use Feign to handle REST calls.
Imagine you have two services serviceA and serviceB, and that you want serviceA to call serviceB using Ribbon and Hystrix. Let's assume you have Eureka server running on the localhost and default port (8761)
serviceA
#SpringBootApplication
#EnableDiscoveryClient
public class ServiceAapplication{
public static void main(String[] args) {
SpringApplication.run(ServiceAapplication.class, args);
}
}
#RestController()
#RequestMapping("/rest")
class DummyRestController{
#RequestMapping(method = RequestMethod.GET, path = "/hello")
String hello(){
return "Hello World!";
}
}
ServiceB
#SpringBootApplication
#EnableDiscoveryClient
#EnableCircuitBreaker
#EnableFeignClients
public class ServiceBapplication{
public static void main(String[] args) {
SpringApplication.run(ServiceBapplication.class, args);
}
}
#FeignClient(value = "serviceA") //must be same name as the service name in Eureka
#RibbonClient(name = "serviceA") //must be same name as the service name in Eureka
interface ServiceAClient {
#RequestMapping(method = RequestMethod.GET, value = "/rest/hello")
String helloFromServiceA();
}
#RestController()
#RequestMapping("/foo")
class DummyRestController{
private final ServiceAclient client;
#Autowired
DummyRestController(ServiceAclient client){
this.client = client;
}
#RequestMapping(method = RequestMethod.GET, path = "/bar")
String hello(){
return client.helloFromServiceA();
}
}
Now if you do a GET on serviceB with foo/bar it will use:
Eureka to find the host and port for serviceA
Ribbon to load balance between multiple instances of serviceA
The whole thing is wrapped into a Hystrix command
Because of the #EnableCircuitBreaker annotation, your serviceB will expose a Hystrix stream. If you run a HystrixDashboard server you can connect to this stream and you will see a helloFromServiceA command on the dashboard.
You can configure Ribbon and Hystrix in your usual configuration file or using a separate configuration class in the #FeignClient and #RibbonClient annotations. You can find more info here
Important: If you want Ribbon to retry on a different instance during a timeout, make sure the Hystrix timeout is higher than the Ribbon time out. See this answer