springcloud two load balancing strategy

Preface:

  Before writing by Ribbon + RestTemplate implementation calls service, here I will talk about the difference between the two systems of

A, springcloud load balancing strategy

  1、Ribbon 

Is based on a set of client load balancing tool Netflix Ribbon implemented similar Nginx when the main function of providing client software load balancing algorithm LB is load balancing, centralized (F5), in-process (Nginx), consumers can automatically see from in Eureka to get a list of services corresponding to the default polling RoundRobinRule

The figure is RestTemplate comes with a 7-balancing policy

 When we realize Ribbon RestTemplate before by calling + services, in the way of obtaining RestTemplate added @LoadBalanced, implements the default polling, if you need to change its balanced policy, declared wanted in the configuration class balancing strategy

Specific implementation reference: https://www.cnblogs.com/guanyuehao0107/p/11819375.html

@Configuration
 public  class RestConfig { 

    @Bean     // to implement call interface by RestTemplate 
    @LoadBalanced      // represents RestTemplate open load balancing 
    public RestTemplate getRestTemplate () {
         return   new new RestTemplate (); 
    } 
    // re-create a balanced policy, said they did not use the default 
    @Bean
     public IRule getIReule () {// the object by obtaining a IRule,
         return   new new RandomRule ();   // achieve the purpose, we used random reselected, instead of the default mode in rotation 
    } 
}

2, the consumer controller layer

    // declare an interface
 //     Private static String HTTP_NAME Final = " HTTP: // localhost : 8001 / Product /"; 
    Private  static   Final String HTTP_NAME = "HTTP: // microService-PRODUCT" ; 
    @Autowired 
    Private RestTemplate RestTemplate; 
    @Resource 
    Private DiscoveryClient Client;    // I want this micro-services by others to discover 
    @GetMapping ( "/ consum / the find / {the above mentioned id}" )
     public the User the FindOne (@PathVariable ( "the above mentioned id" ) Integer the above mentioned id) { 
        String url = HTTP_NAME + " / Product / the FindOne / "+ ID;
         return   (the User) restTemplate.getForObject (URL,User.class);
    }
    @GetMapping("/consum/findAll")
    public List<User> getAll(){
        List<User> users = restTemplate.getForObject(HTTP_NAME + "/product/list", List.class);
        return users;
    }

 

2, Fegin configuration 

  Feign is a declarative Web services client, a Web service client makes writing very easy, in api layer only needs to create an interface, then you can add annotations on top

Proceed as follows:

1, import dependence, create a micro-service interface, as well as consumer services are necessary to introduce micro

 <!-- Feign依赖 -->
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-openfeign</artifactId>
     <version>2.1.0.RELEASE</version>
</dependency>

2, create a layer consumer

@RestController
public class Usercontroller {
@Resource
//调用的是API层的接口 private UserService userService;
@GetMapping(
"/consum/find/{id}") public User findone(@PathVariable("id")Integer id){ return userService.findById(id); } @GetMapping("/consum/findAll") public List<User> getAll(){ return userService.findAll(); } }

3, at the start of the group of consumers module plus  @EnableFeignClients (basePackages = { "com.ghh"}) // a further scan interface Feign

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages = {"com.ghh"}) //扫描另外一个Feign的接口
public class ConsumStart_Feign {
    public static void main(String[] args) {
        SpringApplication.run(ConsumStart_Feign.class,args);
    }
}

4, in api layer to create an interface, and add a comment @FeignClient (value = "MICROSERVICE-PRODUCT "), provider of the table name to call or service names

@FeignClient(value = "MICROSERVICE-PRODUCT")
public interface UserService {
        @GetMapping("/product/findone/{id}")
        public User findById(@PathVariable("id") Integer id);
        @GetMapping("/product/list")
        public List<User> findAll();
}

Third, the difference between the Ribbon + RestTemplate and Feign

 1, the actual development, the call to service may be more than one dependent, the interface is often a multiple call, if Ribbon + RestTemplate, you need to have to create a configuration class in each module, and each community javaweb also tend oriented programming interface

 2, usually self-service package for each micro-client class to wrap some of these services depend on the call, so, on the basis of Feign made further encapsulated in realization Feign, we only need to create an interface and using annotations ways to configure it (previously marked Dao Interface Mapper above notes, now is a microblogging service interface to the top mark a comment Feign), complete interface is bound to a service provider

 3, using Feign method is invoked by the interface Rest service, the request is sent to the server Eureka, find the service interface directly through Feign. Feign combines Ribbon technology, it also supports load balancing.

Guess you like

Origin www.cnblogs.com/guanyuehao0107/p/11831814.html