In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Spring Cloud how to remotely call Feign and integrate Load Balancer Ribbon Fuse Hystrix), for this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more small partners who want to solve this problem find a simpler and easier way.
What is Feign?
Feign is a member of the spring cloud family bucket for remote invocation.
Features: Declarative, templated HTTP client. Make remote calls feel like "local methods" when used
Feign entry
Step 1: Modify pom file and add Feign dependency
Step 2: Modify the startup class and add the Open Feign annotation
Step 3: Write Feign interface, complete remote call, replace dao layer
Step 4: Modify controller to change dao to feign
Step 1: Modify the pom file and add Feign dependencies
org.springframework.cloud spring-cloud-starter-openfeign
Step 2: Modify the startup class and add the Open Feign annotation
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.netflix.hystrix.EnableHystrix;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableEurekaClient@EnableHystrix //Open fuses @ EnableFeigClients//Open FeigClients public class Client4Application {public static void main(String[] args) { SpringApplication.run(Client4Application.class,args);}}
Step 3: Write Feign interface, complete remote call, replace dao layer
@FeignClient(value="service name",path="controller prefix")public interface interface name { //Same as controller method}
package com.czxy.feign;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.GetMapping;import javax.servlet.http.HttpServletRequest;@FeignClient(value="service4",path="/test")public interface DataFeign { @GetMapping public ResponseEntity test() ;}
Step 4: Modify controller to change dao to feign
package com.czxy.controller;import com.czxy.dao.DataDao;import com.czxy.feign.DataFeign;import org.springframework.http.ResponseEntity;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController@RequestMapping("/data")public class DataController { @Resource //private DataDao dataDao; private DataFeign dataFeign; @GetMapping public ResponseEntity data(){ //return dataDao.data(); return dataFeign.test(); }}Feign Integration Load Balancer Ribbon
Spring Cloud completes remote invocation and Load Balancer
Option 1: Use RestTemplate and add the extra annotation @LoadBalanced
Approach 2: Using Feign, Integrated Ribbon, Automatic Load Balancer
If you need to configure ribbon for service separately, please refer to (optional)
#Load Balancer Policy Configuration service4: ribbon: #NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #Random #NFLoadBalancerRuleClassName : com.netflix.loadbalancer.BestAvailableRule #Concurrence is minimal NFLoadBalancerRuleClassName : com.netflix.loadbalancer.WeightedResponseTimeRule #Request Time Weights ConnectTimeout: 250 # Ribbon connection timeout ReadTimeout: 1000 # Ribbon data read timeout OkToRetryOnAllOperations: true #Retry all operations MaxAutoRetriesNextServer: 1 #Number of retries to switch instances MaxAutoRetries: 1 #Retries for current instance Feign integrated fuse Hystrix
Step 1: Modify the yml file and turn on the feign fuse mechanism
Step 2: Create feign interface implementation class and provide alternatives
Step 3: invoke feign, specify fallback to determine alternative
Step 1: Modify the yml file and turn on the feign fuse mechanism
feign: hystrix: enabled: true #Turn on feign fuse
Step 2: Create feign interface implementation class and provide alternatives
package com.czxy.feign;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Component;@Componentpublic class DataFeignFallback implements DataFeign { @Override public ResponseEntity test() { return ResponseEntity.ok("feign alternative"); }}
Step 3: invoke feign, specify fallback to determine alternative
@ FeinClient (value="service name," path="prefix path," fallback= alternative class.class)public interface Interface Name {package com.czxy.feign;import org.springframework.cloud.openfeign. FeinClient;import org.springframework.http.ResponseEntity;import org. springframework.web.bind.annotation.GetMapping;import javax.servlet.http. ServletRequest; @ FeinClient (value="service4," path="/test," fallback= DataFeinFallback.class)public interface DataFeign { @GetMapping public ResponseEntity test() ;} About Spring Cloud how to remotely call Feign and integrate Load Balancer Ribbon Fuse Hystrix) The answer to the question is shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts, you can pay attention to the industry information channel for more relevant knowledge.
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.