In the previous article, I spoke about the registration and discovery of services. In a microservices architecture, the business is split into a separate service, and the service-to-service communication is based on HTTP restful. Spring Cloud has two modes of service invocation, one ribbon+resttemplate and the other feign. In this article, we first explain the following based on Ribbon+rest.
I. INTRODUCTION of the Ribbon
Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies.—–摘自官网
The Ribbon is a load balancer client that can control some of the behavior of HTT and TCP very well. The feign is integrated with the Ribbon by default.
The ribbon has implemented these configuration beans by default:
IClientConfig ribbonClientConfig: DefaultClientConfigImplIRule ribbonRule: ZoneAvoidanceRuleIPing ribbonPing: NoOpPingServerList ribbonServerList: ConfigurationBasedServerListServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilterILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer
Ii. preparatory work
This article is based on the project of the previous article, start the Eureka-server project, start the Service-hi project, its port is 8762, change the port of the Service-hi configuration file to 8763, and start At this point you will find: Service-hi registered 2 instances in Eureka-server, which is equivalent to a small cluster. Visit localhost:8761:
Third, build a service consumer
Re-create a new Spring-boot project, named: Service-ribbon;
In its pom.xml files are introduced to start dependent Spring-cloud-starter-eureka, Spring-cloud-starter-ribbon, Spring-boot-starter-web, the code is 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.forezp</groupid> <artifactId>service-ribbon</artifactId> <version>0.0.1-SNAPSHOT< /version> <packaging>jar</packaging> <name>service-ribbon</name> <description>de Mo Project for Spring boot</description> <parent> <groupid>org.springframework.boot</groupi D> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.release</versi On> <relativePath/> <!--lookup parent from repository---</parent> <properties> <project.build.sourceencoding>utf-8</project.build.sOurceencoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> < ;java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactid>spring-cloud-starter-eureka</ artifactid> </dependency> <dependency> <groupid>org.springframework.cloud< ;/groupid> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>sprin g-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.s Pringframework.boot</groupid> <artifactId>spring-boot-starter-test</artifactId> &L T;scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artif Actid>spring-cloud-dependencies</artifactid> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> & lt;/dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <ARTIFACTID>SPRING-BOOT-MAVEN-PL ugin</artifactid> </plugin> </plugins> </build> <repositories> <repository> <id>spring-milestones</id> <name>spring MILESTONES</NAME&G T <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled > </snapshots> </repository> </repositories></project>
Enterprise Springcloud Tutorial (ii) service consumer (Rest+ribbon)