1 Invocation of a remote service resource 1.1 old routines
Prior to the advent of microservices, all remote service resources had to be done through resttemplate or httpclient, but both realized the invocation of remote service resources and did not provide a load balancing implementation. So programmers have to implement load balancing on their own, and when a remote service is a cluster deployment, clients that take advantage of resttemplate or httpclient encapsulation must configure the remote service's first-off information.
Tip: use the WebClient provided by spring5.x to enable responsive access to remote service resources
Tip 02: Use Resttemplate and WebClient to encapsulate the client's Reference blog: Reference post 01 Reference Bowen 02
Disadvantage: You need to implement load balancing function
Absolute Point 02: The remote Service information must be configured when the client is encapsulated, if the remote service is a huge workload when the cluster is deployed, and the client must make changes once the remote service information changes
1.2 Micro-service routines
After microservices have occurred, the encapsulated client does not need to configure the remote service information, because this information has a unified registry to help us manage, and do not need to implement load balancing function The communication between Springcloud-based microservices is mainly achieved through the Ribbon + resttemplate or feign
Tip 01: The difference between using the Ribbon + resttemplate implementation and the only resttemplate implementation is that you do not need to configure remote service information and provide load balancing capabilities
Tip 02:ribbon is responsible for obtaining remote service information from the registry and providing load balancing capabilities
2 Ribbon Knowledge points
Daigengxin .....
3 Feign Knowledge points
Pending update .....
4 Project Preparation 4.1 Registration Center Eureka Server
Create a single-instance service registration Center
4.2 Service provider Eureka client 4.3 service consumer Eureka Client
5 realized with Ribbon + resttemplate
Invoking remote service resources with Ribbon + resttemplate
Tip 01: Start the Service registration center first
5.1 Creating a remote service restful API
Provides a restful API interface for service consumers to invoke in service-provided services
5.2 Cluster Deployment service providers
The service provider is packaged and started on different ports, and this case uses idea for cluster deployment directly
5.2.1 Open App Configuration
5.2.2 Copying a copy of the original configuration
5.2.3 Modifying configuration information
You only need to modify the configuration name and the boot port
5.2.4 start two configurations separately
Tip 01: You can modify the relevant content information before each configuration starts, so that the purpose is to see the effect in the development phase, and the content information cannot be changed in production, because when the cluster is, the content must be consistent
Tip 02: If the project takes advantage of Devtools dependencies, comment out and turn off the idea's auto-compile feature
5.2.5 to the Registry Control Panel to view information
You can see that an app has started on two ports
5.2.6 accessing restful APIs separately
5.3 Writing a remote Call control class
Use Resttemplate to invoke the resource data provided by the service provider in the service consumer, refer to the documentation
"Configuring the Resttemplate Bean
"Using the Resttemplate call
Tip 01: The remote service's information is replaced directly by the application name of the remote service, which is the value of the Spring.application.name
5.4 Remote resource testing via consumer services
Tip 01:ribbon using polling to implement load balancing by default
6 using feign to achieve
Tip 01:feign The Ribbon is integrated by default
Tip 02: need to introduce feign dependencies and add @EnableFeignClients annotations on the startup class in a service that needs to use feign
<!--https://mvnrepository.com/artifact/org.springframework.cloud/ Spring-cloud-starter-openfeign- <dependency> <groupid>org.springframework.cloud </groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency >
6.1 Creating a remote service restful API
See 5.1
6.2 Writing a Remote call interface
6.3 Writing a control layer for a remote call
6.4 Remote resource testing via consumer services
7 This blog source code
Click to go
SPRINGCLOUD03 Ribbon knowledge points, feign knowledge points, using Resttemplate+ribbon to invoke resources provided by remote services, leveraging feign to invoke resources provided by remote services