Connect Limits-Service To Spring Cloud
Connect Limits-Service To Spring Cloud
1. spring.application.name=limits-service
2. spring.cloud.config.uri=http://localhost:8888
1. <dependency>
2. <groupId>org.springframework.cloud</groupId>
3. <artifactId>spring-cloud-starter-feign</artifactId>
4. <version>1.4.4.RELEASE</version>
5. </dependency>
Once the dependency is added, enable the Feign to scan the
clients by adding the annotation @EnableFeignClients.
Define an attribute in the @EnableFeignClients annotation.
The attribute is the name of the package that we want to scan.
@EnableFeignClients("com.javatpoint.microservices.currencyc
onversionservice")
We have enabled the Feign in our project. Now, we will use the
Feign to invoke the service.
@FeignClient(name="currency-exchange-
service", url="localhost:8000")
@Autowired
private CurrencyExchangeServiceProxy proxy;
I t a u t o m a t i c a l l y i n t e r a c t s w i t h N e t fl i x S e r v i c e
Discovery (Eureka) because it is a member of the Netflix
family.
1. <dependency>
2. <groupId>org.springframework.cloud</groupId>
3. <artifactId>spring-cloud-starter-netflix-ribbon</
artifactId>
4. </dependency>
@RibbonClient(name="currency-exchange-
service")
1. spring.application.name=currency-conversion-service
2. server.port=8100
3. currency-exchange-service.ribbon.listOfServers=http://
localhost:8000, http://localhost:8001
Naming server
The naming server is a computer application that implements
a network service for responding to queries against a directory
service.
1. spring.application.name=netflix-eureka-naming-server
2. server.port=8761
3. eureka.client.register-with-eureka=false
4. eureka.client.fetch-registry=false
Connecting Microservices to
Eureka naming server
Step : Select the currency-conversion-service project.
1. <dependency>
2. <groupId>org.springframework.cloud</groupId>
3. <artifactId>spring-cloud-starter-netflix-eureka-client</
artifactId>
4. </dependency>
Open CurrencyConversionServiceApplication.java file and
enable discovery client by using the
annotation @EnableDiscoveryClient.
1. spring.application.name=currency-conversion-service
2. server.port=8100
3. e u r e k a . c l i e n t . s e r v i c e - u r l . d e f a u l t - z o n e = h t t p : / /
localhost:8761/eureka
4. currency-exchange-service.ribbon.listOfServers=http://
localhost:8000, http://localhost:8001
API Gateway
API Gateway encapsulates the internal system architecture. It
provides an API that is tailored to each client. It also has other
responsibilities such as authentication, monitoring, load
balancing, caching, request shaping and
management, and static response handling.
1. @EnableZuulProxy
2. @EnableDiscoveryClient
3. @SpringBootApplication
4. public class NetflixZuulApiGatewayServerApplication
application.properties
1. spring.application.name=netflix-zuul-api-gateway-server
2. server.port=8765
3. e u r e k a . c l i e n t . s e r v i c e - u r l . d e f a u l t - z o n e = h t t p : / /
localhost:8765/eureka
Distributed Tracing
Distributed tracing is a technique to monitor and profile the
applications, especially those built using microservice
architecture. It is also known as distributed request tracing.
Developers use distributed tracing to debug and optimize the
code.
Zipkin
Zipkin is an open-source, Java-based distributed tracing
system. It has a management console that provides a
mechanism for sending, receiving,
storing, and visualizing traces details of the subsequent
services.
With the help of the Zipkin server, we can put all the logs of all
the components in the MQ (RabbitMQ). We send the logs to
the Zipkin server where the logs consolidate.
1. <dependency>
2. <groupId>org.springframework.cloud</groupId>
3. <artifactId>spring-cloud-starter-sleuth</artifactId>
4. </dependency>
Here, the most important thing is that the Zipkin server must
listen over the RabbitMQ server. So we have to start the
RabbitMQ server in the background.
1. C:\>SET RABBIT_URI=amqp://localhost
2. C:\> java -jar zipkin-server-2.12.9-exec.jar
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
1. <dependency>
2. <groupId>org.springframework.cloud</groupId>
3. <artifactId>spring-cloud-starter-bus-amqp</artifactId>
4. </dependency>
Note: If you are using Spring Boot 2.0.0 or above versions, use the
following URL:
http://localhost:8080/actuatror/bus-refresh
1. <dependency>
2. <groupId>org.springframework.cloud</groupId>
3. <artifactId>spring-cloud-starter-netflix-hystrix</
artifactId>
4. </dependency>
1. @GetMapping("/fault-tolerance-example")
2. //configuring a fallback method
3.
@HystrixCommand(fallbackMethod="fallbackRetrieveCon
figurations")
4. public LimitConfiguration retrieveConfigurations()
5. {
6. throw new RuntimeException("Not Available");
7. }
8. //defining the fallback method
9.
public LimitConfiguration fallbackRetrieveConfigurations()
10. {
11. //returning the default configuration
12. return new LimitConfiguration(999, 9);
13. }