Spring clould Alibaba integrated Ribbon&Feign

Keywords: Java Spring snapshot

Spring clould Alibaba integrated Ribbon

1. Add annotation @ LoadBalanced on resttemplate

2. When calling, you can use the service name to call, which is the same way as the previous use of Eureka

The default is polling policy

@RequestMapping("/getGoods")
    public ResponseResult getGoods() {
        String url="http://goods-provide/getGoods";
        return ResponseResult.success("Successful operation",
                restTemplate.getForObject(url,Object.class));
    }

3. Custom load balancing strategy

Add on startup class

@Bean
    public IRule Irule(){
        return new RandomRule();//Random load balancing
    }

4. Configure lazy loading

# Hungry load (lazy load) started get service
ribbon:
  eager-load:
    enabled: true
    clients: goods-provide #Multiple services separated by commas

5. Nacos integrates Ribbon and supports weight load balancing algorithm

  1. Custom weight algorithm
    Create a class and inherit the AbstractLoadBalancerRule class to implement the following methods
public class IRuleConfig  extends AbstractLoadBalancerRule {
    @Autowired
    private NacosDiscoveryProperties nacosDiscoveryProperties;
    @Override
    public void initWithNiwsConfig(IClientConfig iClientConfig) {

    }
    @Override
    public Server choose(Object key) {
        try {
            BaseLoadBalancer loadBalancer = (BaseLoadBalancer)this.getLoadBalancer();
            //Get the name of the requested microservice
            String name = loadBalancer.getName();
            //Get nacos service discovery related name
            NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();
            //nacos client automatically selects instances through the weight based load balancing algorithm
            Instance instance = namingService.selectOneHealthyInstance(name);
            System.out.println("port:"+instance.getPort()+"Example:"+instance);
            return new NacosServer(instance);
        } catch (NacosException e) {
            e.printStackTrace();
            return null;
        }
    }
}
  1. Enable on startup class
  2. Create a sub module and change the port number like goods


    Start all services
  3. Configure service weight in nacos Service Center

Spring clould Alibaba integration Feign

Using steps

1. Introduce dependent user to the client

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2. Annotate the startup class: @ EnableFeignClients

Create service FeignClient

  1. Create a service folder, create an interface under the file, and add the @ FeignClient() annotation parameter to the interface, which is your microservice name
@FeignClient(name="goods-provide")
public interface FeiginClient {

    @RequestMapping("/getGoods")
    public Object getGoods();
}

  1. Inject in the controller and call
 @Autowired
    private FeiginClient feiginClient;
 @RequestMapping("/getGoods")
    public ResponseResult getGoods() {

        return ResponseResult.success("Successful operation",
                feiginClient.getGoods());
    }

Startup Test

3. Single parameter request

1. Write a request with parameters in goods

 @RequestMapping("/getGoodsWithID/{id}")
    public ResponseResult getGoodsWithID(@PathVariable Integer id){
        return ResponseResult.success("id="+id);
    }
  1. Writing interface in FeiginClient
@RequestMapping("/getGoodsWithID/{id}")
    public ResponseResult getGoodsWithID(@PathVariable Integer id);
  1. Call in controller
 @RequestMapping("/getGoodsWithID/{id}")
    public ResponseResult getGoodsWithID(@PathVariable Integer id){
        return ResponseResult.success("Successful operation",
                feiginClient.getGoodsWithID(id));
    }

Startup Test

4. Multiple parameter requests

  1. Create a sub project to put entity class
  2. Create entity class Goods to implement serialization interface and get set method
public class Goods implements Serializable {
    private String name;
    private String color;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}
  1. Depend on entity package in other projects
<dependency>
            <groupId>com.dj</groupId>
            <artifactId>GoodsPojo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
  1. Creating a method with multiple parameters in the controller of goods project
  2. Create the interface in FeiginClient and write the @ SpringQueryMap annotation, otherwise the passed value cannot be received
  3. Call in controller
@RequestMapping("/getGoodsWithObj")
    public ResponseResult getGoodsWithObj(Goods goods){
        return ResponseResult.success("Successful operation",
                feiginClient.getGoodsWithObj(goods));
    }
    

Startup Test

Posted by Fusioned on Tue, 14 Apr 2020 06:16:44 -0700