Discovery and consumption of services
With service centers and service providers, let's implement a service consumer:
Service consumers mainly complete two tasks-service discovery and service consumption, Service discovery task is done by the Eureka client , and the task of service consumption is done by the Ribbon .
The Ribbon is a load balancer based on HTTP and TCP clients. It can be configured to poll access for load balancing purposes by configuring the Ribbonserverlist server-side list through the client.
When the ribbon and Eureka are used simultaneously, the Ribbon's service instance manifest ribbonserverlist is discoveryenableniewserverlist rewritten.
The ribbon gives responsibility to eureka to determine whether the server has been started.
Example: Building a simple example of service discovery and consumption:
1. The previous section of the/hello service is started by Jar-jar using a different two ports, in order to test the Ribbon client load Balancing function, the following command to start two different services:
Java-jar Helloworld-0.0.1-snapshot.jar--server.port=8081-jar Helloworld-0.0.1-snapshot.jar--server.port =8082
These two instance services can be seen in the service registry after launch, as follows:
2. Create a new base spring boot project to serve as a consumer, named Ribbon-consumer, to join the dependency.
<?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.dcz</groupId> <artifactid>rebbon-consumer</artifactid > <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>rebbon-cons Umer</name> <description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> & Lt;artifactid>spring-boot-starter-parent</artifactid> <version>1.3.8.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> <pro Ject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties& Gt <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></dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-ribbon</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupid>org. Springframework.cloud</groupid> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <SCOPE&G TImport</scope> </dependency> </dependencies> </dependencyManagement> <BUILD&G T <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
3. On Rebbonconsumerapplication, on the main class of the app you just created, register the app as a Eureka client app with @enablediscoveryclient annotations for service discovery capabilities.
The resttemplate spring Bean instance is created in the main class, and client load balancing is turned on through the @loadbalanced annotations.
PackageCom.dcz;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;Importorg.springframework.cloud.client.loadbalancer.LoadBalanced;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.web.client.RestTemplate; @EnableDiscoveryClient//access to service discovery capabilities@SpringBootApplication Public classrebbonconsumerapplication {@Bean @LoadBalanced//turn on load balancingresttemplate resttemplate () {return Newresttemplate (); } Public Static voidMain (string[] args) {Springapplication.run (rebbonconsumerapplication.class, args); }}
4. Create the Consumercontroller controller and implement the/ribbon-consumer interface, and make calls to the service center service by injecting the resttemplate.
We have to make calls through the service name, which is very important in the service governance framework.
PackageCom.dcz;ImportCom.netflix.discovery.converters.Auto;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestMethod;ImportOrg.springframework.web.bind.annotation.RestController;Importorg.springframework.web.client.RestTemplate;/*** Created by Administrator on 2017/5/13.*/@RestController Public classConsumercontroller {@Autowired resttemplate resttemplate; @RequestMapping (Value= "/ribbon-consumer", method =requestmethod.get) PublicString Helloconsumer () {returnResttemplate.getforentity ("Http://HELLO-SERVICE/hello", String.class). GetBody (); }}
6. Finally, configure the location of the Eureka Service Center in the application.properties file and set up a different port.
spring.application.name=ribbon-consumerserver.port=9000eureka.client.serviceUrl.defaultZone= http://peer1:1111/eureka/
7. Launch the Rebbon-consumer app, and then we'll see the Ribbon-consumer service in the Service center panel.
8. Initiate a GET request through Http://localhost:9000/rebbon-consumer in the browser.
We can refresh multiple times and open the control bar of the app's two different ports to discover how Rebbon calls two apps in a poll.
Spring Cloud Service Discovery and consumption