spring-cloud service discovery and consumption (take ribbon as an example)

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:

  1. cd target
  2. java -jar SpringCloudDemo-0.0.1-SNAPSHOT.jar --server.port=8080
copy code
  1. cd target
  2. java -jar SpringCloudDemo-0.0.1-SNAPSHOT.jar --server.port=8081
copy code

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:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.hellxz</groupId>
  7. <artifactId>ribbon-customer</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <parent>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-parent</artifactId>
  12. <version>1.5.9.RELEASE</version>
  13. <relativePath/>
  14. </parent>
  15. <dependencies>
  16. <dependency>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-web</artifactId>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.springframework.cloud</groupId>
  22. <artifactId>spring-cloud-starter-eureka</artifactId>
  23. <version>RELEASE</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-ribbon</artifactId>
  28. <version>RELEASE</version>
  29. </dependency>
  30. </dependencies>
  31. <dependencyManagement>
  32. <dependencies>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-dependencies</artifactId>
  36. <version>RELEASE</version>
  37. <scope>import</scope>
  38. <type>pom</type>
  39. </dependency>
  40. </dependencies>
  41. </dependencyManagement>
  42. </project>
copy code

Create a new springboot startup class and hand the RestTemplate to the Spring container for management

  1. package com.cnblogs.hellxz;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  5. import org.springframework.cloud.client.loadbalancer.LoadBalanced;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.web.client.RestTemplate;
  8. /**
  9. * @Author : Hellxz
  10. * @Description: consumer application
  11. * @Date : 2018/4/16 15:45
  12. */
  13. @EnableDiscoveryClient
  14. @SpringBootApplication
  15. public class CustomerApplication {
  16. [url=home.php?mod=space&uid=4377]@Bean[/url] //Give this Bean to the spring container
  17. @LoadBalanced //Enable load balancing through this annotation
  18. RestTemplate restTemplate(){
  19. return new RestTemplate();
  20. }
  21. public static void main(String[] args) {
  22. SpringApplication.run(CustomerApplication.class, args);
  23. }
  24. }
copy code

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.

  1. server:
  2. port: 9000 #Specify the service port for ribbon-customer
  3. spring:
  4. application:
  5. name: ribbon-customer #Specify the application name
  6. #Specify the address of the eureka registration center
  7. eureka:
  8. client:
  9. serviceUrl:
  10. defaultZone: http://peer1:1111/eureka/,http://peer2:1112/eureka/
copy code

Create a CustomerController in the same directory of the startup class, inject RestTemplate to call the service interface

  1. package com.cnblogs.hellxz;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import org.springframework.web.client.RestTemplate;
  7. /**
  8. * @Author : Hellxz
  9. * @Description: consumer application
  10. * @Date : 2018/4/16 15:54
  11. */
  12. @RestController
  13. public class CustomerController {
  14. @Autowired
  15. //Inject restTemplate
  16. private RestTemplate restTemplate;
  17. @RequestMapping(value="/ribbon-customer", method=RequestMethod.GET)
  18. public String helloCustomer(){
  19. //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
  20. //return restTemplate.getForEntity("http://localhost:8080/hello",String.class).getBody();
  21. //Use restTemplate to call the microservice interface
  22. return restTemplate.getForEntity("http://hello-service/hello", String.class).getBody();
  23. }
  24. }
copy code

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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325381872&siteId=291194637