In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to achieve a service discovery function in springcloud-feign. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Springcloud uses service discovery for inter-service invocation. Use the gateway gateway to shield the shunting and forwarding interface. The service discovers that the client pulls the service list from the registry to achieve client load balancing. There are two main ways for the client to achieve load balancing. One is ribbon and the other is feign based on ribbon encapsulation. This is the official explanation of feign. I have summed up the following main advantages:
(1) pluggable annotation support, including Feign comments and JAX-RS comments
(2) support pluggable HTTP encoders and decoders
(3) support Hystrix and its Fallback
(4) load balancer that supports Ribbon
(5) support HTTP request and corresponding compression.
Common concepts of micro-service invocation
(1) Service degradation:
When the pressure on the server increases sharply, according to the actual business situation and traffic, some services and pages are processed in a strategic way or in a simple way, so as to release resources to ensure the normal operation of the core transaction and efficient operation.
(2) cut-off:
The concept of fuse comes from the circuit breaker (circuit Breaker) in electronic engineering. In the Internet system, when the downstream service slows down or fails due to excessive access pressure, the upstream can temporarily cut off the call to the downstream service in order to protect the overall availability of the system. The simple understanding is that the downgrade is for the business, and the open circuit is the upstream and downstream fault.
(3) idempotency:
It means that the result of one request or multiple requests initiated by the user for the same operation is consistent, and there will be no side effects caused by multiple clicks.
Mode of use
Let's start using feign:
(1) add springcloudalibaba and openfeign dependencies
Org.springframework.cloud
Spring-cloud-starter-openfeign
Org.springframework.cloud
Spring-cloud-starter-alibaba-nacos-discovery
(2) add the annotation @ EnableDiscoveryClient to enable service discovery, and the annotation @ EnableFeignClients supports feign client.
Annotate the external interface with @ FeignClient ("serviceName") so that the interface supports calling services and therefore hot-swappable. ServiceName uses the name of the service that was invoked by the registry. The method of the @ RequestMapping annotation is the interface method of the service provider, which looks very similar to the way springmvc is used, yes, because this is a contract supported by feign for mvc. Feign defaults to @ RequestLine. It can support all kinds of contracts. Similarly, he can use the default HttpUrlConnection or httpclient or okhttp3.
(3) modify application.peoperties configuration file # service name
-spring.application.name=feign-service
# Service without port 0 will randomly select one
-server.port=0
# name of service cluster
-spring.cloud.nacos.discovery.cluster-name=consumer-service # nacos registry address
-spring.cloud.nacos.discovery.server-addr=nacos.blog.ailijie.top
Ok, complete. Here are the advanced uses of feign
Advanced use of feign
(1) feign log configuration
Create a feign log level and a retry mechanism for feign, so that feign can log each request at the same time using the retry mechanism.
(2) the circuit breakage of feign
Configure the fallbackFactory factory in the feignclient comments to solve the problem of circuit breakage. The advantage of using factory is that you can handle exception information. You can also use fallback processing. If you use fallback,fallbackFactory, it will not take effect. Take a look, Factory needs to return an implementation class of IProviderService, which can handle exceptions in the create method. In this way, when an exception occurs on the server or goes offline, the disconnection method will be used.
(2) Advanced configuration of feign
# enable okhttp
-feign.okhttp.enabled=true
# enable hystrix disconnection. Downgrade without enabling feign will not take effect.
-feign.hystrix.enabled=true
# hystrix break timeout
-hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds = 2000
-provider-service.ribbon.ConnectTimeout = 1000
-provider-service.ribbon.ReadTimeout = 1000
# retry all operation requests
-provider-servicee.ribbon.OkToRetryOnAllOperations = true
# switch the number of retries of the instance
-provider-service.ribbon.MaxAutoRetriesNextServer = 2
# number of retries on the current instance
-provider-service.ribbon.MaxAutoRetries = 1
# set log level
-logging.level.com.wotrd.feignservice.service.IProviderService=DEBUG
The timeout of the open circuit needs to be greater than the read timeout, otherwise the retry will not take effect.
After reading the above, do you have any further understanding of how to implement a service discovery function in springcloud-feign? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.