illustrate:
ribbon is a role in spring-cloud as a service consumer, through which the client can consume the service provider's services.
For example, in this example, the service provider registers in the registration center. The service provider provides a service interface, returning a hello string. We call this interface through ribbon, and then obtain the service provider's service without exposing the address of the real service provider.
premise:
Follow the previous tutorials to build a registration center and service provider. You can use a sharded registration center here or you can not use it. This is temporarily set to be a sharded registration center that was set up before using, and the service provider can only provide one.
Preparation:
1. Start the registration center
According to the previous tutorial, use peer1 and peer2 to start the registration center of two shards. If it is a single node, you can directly start the project.
After startup, you can check localhost:1111 or localhost:1112, as shown in the figure
2. Start the service provider
In order to see the load balancing situation, two service providers need to be started
According to the previous tutorial, two terminals are enabled to start the specified ports (two identical services can also configure different ports in their respective configuration files). The two terminal instructions are as follows:
cd targetjava -jar SpringCloudDemo-0.0.1-SNAPSHOT.jar --server.port=8080
cd targetjava -jar SpringCloudDemo-0.0.1-SNAPSHOT.jar --server.port=8081
Startup results:
At this point, the preparations have been completed
text:
1. Ribbon service construction
Create a new maven project without using the template. The project is called robbin-customer, and the import dependencies are referenced as follows:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hellxz</groupId> <artifactId>ribbon-customer</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> <version>RELEASE</version> </dependency> </dependency> </dependency> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>RELEASE</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencies> </dependencyManagement></project>
Create a new springboot startup class and hand over the RestTemplate to the Spring container for management
package com.cnblogs.hellxz;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.web.client.RestTemplate;/** * @Author : Hellxz * @Description: Consumer Application* @Date : 2018/4/16 15:45 */@EnableDiscoveryClient@SpringBootApplicationpublic class CustomerApplication { @Bean //Support this Bean to the spring container @LoadBalanced //Enable load balancing through this annotation RestTemplate restTemplate(){ return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(CustomerApplication.class, args); }}Create an application.yml in the src/resources directory for configuration registration information. This article uses a sharded registration center. Please configure a defaltZone for a single node.
server: port: 9000 #Specify the service port for ribbon-customer spring: application: name: ribbon-customer #Specify the application name #Specify the eureka registration center address eureka: client: serviceUrl: defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/
Create a CustomerController in the startup class directory, inject RestTemplate to call the service interface
package com.cnblogs.hellxz;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;/** * @Author : Hellxz * @Description: Consumer Application* @Date : 2018/4/16 15:54 */@RestControllerpublic class CustomerController { @Autowired //Inject restTemplate private RestTemplate restTemplate; @RequestMapping(value="/ribbon-customer", method=RequestMethod.GET) public String helloCustomer(){ //Commented here because I took it out of the straight-chain access service provider interface before, so the result will not be returned, and an error will be reported //return restTemplate.getForEntity("http://localhost:8080/hello",String.class).getBody(); //Use restTemplate to call the microservice interface return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody(); }}Note: Line 24 of the above code gives an error demonstration. If an error occurs when accessing the ribbon interface and a white error page appears, please check here
At this point, the ribbon consumer application is built and the test is started
test:
Visit http://localhost:1111/ We found that this ribbon consumer application has been registered in the registration center
Visit http://localhost:9000/ribbon-customer
Remember that if there is access in the service provider project, the information will be printed, because two service providers have been started, here you can test the load balancing of ribbon.
View service provider output
The second terminal does not, and it is still displayed in the Resolving eureka endpoints via configuration line
Refresh the page to view the terminal. Since ribbon's default load balancing implementation is polling, it may occur to access the same service multiple times. Refresh the page several times, and it will definitely be displayed in another Termical!
Conclusion:
As a service consumer, Ribbon can not expose the interface address to the user while obtaining the services provided by the service provider. It can be seen that when calling the service interface here, the service provider's service name is used instead of the host name. This feature is very important in the service governance framework.
Since I learned the jhipster generator first, I would like to predict it in advance and then there will be a technology called Feign that can replace Ribbon. This series of blogs is all study notes. In actual operation, there may be a situation where an application is both a service provider and a service consumer.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.