2. Service invocation
2.1 LoadBalancer integrates RestTemplate to call other micro services
2.1.1 introduction
2.1.1.1 introduction to resttemplate
RestTemplate is a network request framework in Spring Resources that accesses third-party RESTful API interfaces. The design principle of RestTemplate is similar to that of other spring templates (such as JdbcTemplate), which provides a simple method with default behavior for performing complex tasks.
RestTemplate is used to consume REST services, so the main methods of RestTemplate are closely connected with some methods of HTTP protocol of REST, such as HEAD, GET, POST, PUT, DELETE, OPTIONS and other methods. The corresponding methods of these methods in RestTemplate class are headForHeaders(), getForObject(), postForObject(), put(), DELETE().
RestTemplate supports common Http protocol request methods, such as post, get, delete, etc., so it is easy to build restful API with RestTemplate
2.1.1.2 LoadBalancer load balancing
Load balancing refers to allocating the load to multiple execution units. There are two common ways of load balancing. An independent process unit that forwards requests to different execution units through load balancing strategy, such as Nginx. The other is to encapsulate the form of load balancing logic generation to the client of the service consumer. The client of the service consumer maintains an information list of the service provider, has an information table, and allocates the request to multiple service providers through the load balancing strategy, so as to achieve the purpose of load balancing. SpringCloud Original client load balancing scheme Ribbon Has been abandoned and replaced by SpringCloud LoadBalancer,LoadBalancer yes Spring Cloud Commons It belongs to the second method mentioned above, which encapsulates the load balancing logic into the client and runs in the process of the client. stay Spring Cloud In the component micro service system, LoadBalancer As a load balancer for service consumers, there are two ways to use it. One is and RestTemplate The other is and Feign Combined, Feign It has been integrated by default LoadBalancer
2.1.2 LoadBalancer integration RestTemplate
2.1.2.1 payment
- Configuration file yml
In application In the YML configuration file, spel is used to specify the port, which indicates that the port parameter exists. The port parameter is used, and the default 9001 port is not used. When starting the service, you can specify that the service uses a different port by specifying - Dport=9000
server port: ${port:9001}
- Configure the Controller layer for the caller to call
@RestController @RequestMapping("/payment") public class PaymentController { @Value("${server.port}") private String serverPort; @GetMapping("/{id}") public ResponseEntity<String> payment(@PathVariable("id")Integer id){ return ResponseEntity.ok("order number=" + id + ",Payment successful,server.port=" + serverPort); }
2.1.2.2 consumer (order)
- Add dependency
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> //Provide load balancing and integrate RestTemplate <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
- Configure LoadBalanced in the initiator class
@Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); }
- Configure Controller layer
//Inject RestTemplate to call other services @Autowired private RestTemplate restTemplate; //Configure the interface and call the resttemplate method @GetMapping("/lb/{id}") public ResponseEntity<String> getPayMentByLb(@PathVariable("id") Integer id){ //Payment service refers to the application name of the server String result = restTemplate.getForObject("http://payment-service" + "/payment/" + id,String.class); return ResponseEntity.ok(result); }
2.1.2.3 start and test
Start two consumer microservice projects with ports 9002 and 9001 respectively. The configuration is as follows
Access the interface under the order micro service for many times. Load balancing works, and ports 90019002 appear in turn
2.2 call OpenFeign and call other micro services through PaymentClient
2.2.1 introduction
Feign is a declarative HTTP client component designed to make it easier to write HTTP clients. OpenFeign adds support for Spring MVC annotations and integrates Spring Cloud LoadBalancer and Spring Cloud CircuitBreaker. When feign is used, it provides load balancing and circuit degradation functions
2.2.2 simple use
- Adding dependencies under consumer microservice Engineering
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency
- Enable Feign function
Use @ EnableFeignClients to turn on the Feign function
- Create Feign client
In the @ FeignClient annotation, "payment service" is the service name. Use this name to get the corresponding service from the Eureka service list to create the LoadBalancer client. You can also use the URL attribute to specify the URL of the service
-
Call at the controller layer
-
Start the server port 90019002, and the consumer server project, access the address, and realize Feign integrated load balancing LoadBalancer component