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
- 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; } } }
- Enable on startup class
- Create a sub module and change the port number like goods
Start all services - 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
- 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(); }
- 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); }
- Writing interface in FeiginClient
@RequestMapping("/getGoodsWithID/{id}") public ResponseResult getGoodsWithID(@PathVariable Integer id);
- 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
- Create a sub project to put entity class
- 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; } }
- Depend on entity package in other projects
<dependency> <groupId>com.dj</groupId> <artifactId>GoodsPojo</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
- Creating a method with multiple parameters in the controller of goods project
- Create the interface in FeiginClient and write the @ SpringQueryMap annotation, otherwise the passed value cannot be received
- Call in controller
@RequestMapping("/getGoodsWithObj") public ResponseResult getGoodsWithObj(Goods goods){ return ResponseResult.success("Successful operation", feiginClient.getGoodsWithObj(goods)); }
Startup Test