Ribbon is a role as a service consumer in spring-cloud, through which the client can consume the service of the service provider.
For example, in this example, the service provider registers with the registry. The service provider provides a service interface and returns a hello string. We call this interface through the ribbon to obtain the service provider without exposing the address of the real service provider. services
premise:
Follow the previous tutorials to build a registration center and service provider. The sharded registry can be used here or not. It is temporarily set to use the sharded registry set up before, and the service provider can only provide one.
Ready to work:
1. Start the registration center
According to the previous tutorial, use peer1 and peer2 to start the registration center of the two shards respectively. If it is a single node, you can start the project directly.
After startup, you can view localhost:1111 or localhost:1112, as shown in the figure
2. Start the service provider
Here, in order to view the load balancing situation, you need to start two service providers
According to the previous tutorial, open two terminals respectively to start the specified port (two identical services can also be configured with different ports in their respective configuration files). The two terminal commands are as follows:
- cd target
- java -jar SpringCloudDemo-0.0.1-SNAPSHOT.jar --server.port=8080
- cd target
- java -jar SpringCloudDemo-0.0.1-SNAPSHOT.jar --server.port=8081
Startup result:
So far, the preparatory work has been completed
text:
1. Ribbon service construction
Create a new maven project without using templates. The project name is robbin-customer, and the import dependencies are 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>
- </dependencies>
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>RELEASE</version>
- <scope>import</scope>
- <type>pom</type>
- </dependency>
- </dependencies>
- </dependencyManagement>
- </project>
Create a new springboot startup class and hand 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
- @SpringBootApplication
- public class CustomerApplication {
- [url=home.php?mod=space&uid=4377]@Bean[/url] //Give 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 to configure registration information. This article uses a fragmented 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 address of the eureka registration center
- eureka:
- client:
- serviceUrl:
- defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/
Create a CustomerController in the same directory of the startup class, 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
- */
- @RestController
- public class CustomerController {
- @Autowired
- //Inject restTemplate
- private RestTemplate restTemplate;
- @RequestMapping(value="/ribbon-customer", method=RequestMethod.GET)
- public String helloCustomer(){
- //Comment out here, because I took it for granted that I used the interface of the direct access service provider before, so it will not return the result, and it will report an error
- //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 there is an error accessing the ribbon interface and a white error page appears, please check here
http://www.ljhseo.com/
http://www.xyrjkf.net/
http://www.xyrjkf.cn/
http://www.xyrjkf.com.cn/
http://www.zjdygsi.cn/
http://www.zjdaiyun.cn/
http://www.jsdygsi.cn/
http://www.xyrjkf.top/
http://www.xyrjkf.com/
http://www.daiyunzj.cn/
http://ljhseo.com/
http://xyrjkf.net/
http://xyrjkf.cn/
http://xyrjkf.com.cn/
http://zjdygsi.cn/
http://zjdaiyun.cn/
http://jsdygsi.cn/
http://xyrjkf.top/
http://xyrjkf.com/
http://daiyunzj.cn/
So far, the ribbon consumer application has been built and the test has been started.
test:
Visit http://localhost:1111/ and we find that the ribbon consumer application has been registered in the registry
访问http://localhost:9000/ribbon-customer
Remember that if there is access in the method shown in the service provider project, the information will be printed, because two service providers are started, and the load balancing of the ribbon can be tested here.
View service provider output
The second terminal does not, and still displays the line Resolving eureka endpoints via configuration
Refresh the page to view the terminal. Since the default load balancing implementation of ribbon is round-robin, it may appear that the same service may be accessed multiple times, and the page will be refreshed several times, and it will definitely be displayed in another term!
Conclusion:
As a service consumer, Ribbon can obtain the service provided by the service provider without exposing the interface address to the user. It can be seen that when calling the service interface here, the service name of the service provider is used instead of the host name, which is very important in the service governance framework.
Since I learned the jhipster generator first, I predict in advance that there will be a technology called Feign that can replace Ribbon. This series of blogs are study notes. In actual operation, there may be an application that is both a service The provider is in turn a service consumer.