Spring Cloud Introductory Eureka-consumer service consumption (i)

Source: Internet
Author: User
Tags abstract definition

Here's an introduction: LoadBalancerClient interface, which is an abstract definition of a load balancer client, let's look at how to consume the service using the Load Balancer Client interface provided by spring Cloud.

Refer to the Eureka-server built in the previous article as the Service registry, Eureka-client as the service provider as the basis.

1, Pom.xml and eureka-client the same configuration

<?XML version= "1.0" encoding= "UTF-8"?><Projectxmlns= "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.example</groupId>    <Artifactid>Eurekaconsumer</Artifactid>    <version>0.0.1-snapshot</version>    <Packaging>Jar</Packaging>    <name>Eurekaconsumer</name>    <Description>Demo Project for Spring Boot</Description>    <Parent>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter-parent</Artifactid>        <version>1.4.0.RELEASE</version>        <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.boot</groupId>            <Artifactid>Spring-boot-starter-web</Artifactid>        </Dependency>        <Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-test</Artifactid>            <Scope>Test</Scope>        </Dependency>    </Dependencies>        <dependencymanagement>        <Dependencies>            <Dependency>               <groupId>Org.springframework.cloud</groupId>               <Artifactid>Spring-cloud-dependencies</Artifactid>               <version>Dalston.sr3</version>               <type>Pom</type>               <Scope>Import</Scope>            </Dependency>        </Dependencies>    </dependencymanagement>    <Build>        <Plugins>            <plugin>                <groupId>Org.springframework.boot</groupId>                <Artifactid>Spring-boot-maven-plugin</Artifactid>            </plugin>        </Plugins>    </Build></Project>

2, application, Server uses 8000,client 8001 ports, here we use 8002

spring.application.name=eurekaconsumerserver.port=8002
Address of the #指定eureka-servcer Registration Center eureka.client.serviceurl.defaultzone=http://localhost:8000/eureka/

3. Spring Cloud uses the rest API, where we initializeRestTemplate,用来真正发起REST请求

 PackageCom.example.demo;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.web.client.RestTemplate, @EnableDiscoveryClient @springbootapplication Public classeurekaconsumerapplication {@Bean Publicresttemplate resttemplate () {return Newresttemplate (); }     Public Static voidMain (string[] args) {Springapplication.run (eurekaconsumerapplication.class, args); }}

4. Create a consumer interface

 PackageCom.example.demo.controller;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.cloud.client.ServiceInstance;Importorg.springframework.cloud.client.loadbalancer.LoadBalancerClient;Importorg.springframework.web.bind.annotation.GetMapping;ImportOrg.springframework.web.bind.annotation.RestController;Importorg.springframework.web.client.RestTemplate; @RestController Public classClientconsumercontroller {@Autowired loadbalancerclient loadbalancerclient;    @Autowired resttemplate resttemplate; @GetMapping ("/consumer")     PublicString All () {//initiate a REST request        returnResttemplate.getforobject (GETURL ("Eurekaclient", "/all"), String.class); }    /*** Get the specified URL *@paramClientapplicationname The specified service provider name *@paramInterfaceName The interface name that needs to be consumed *@return     */    Privatestring GetUrl (String clientapplicationname, String InterfaceName) {//use Loadbalancerclient's choose function to load-balance a eurekaclient service instanceServiceinstance serviceinstance =Loadbalancerclient.choose (clientapplicationname); //get previous Eurekaclient/all interface addressString url = "/http" + serviceinstance.gethost () + ":" + serviceinstance.getport () +InterfaceName;        System.out.println (URL); returnURL; }    }

Finally start the Registration service server, service provider client, service consumption consumer, you can see, access to HTTP://LOCALHOST:8000/Service center results are as follows:

Then visit Http://localhost:8002/consumer, go to consume Eurekaclient's all interface

It can be seen that consumer services, need to specify the service provider, and the need to consume the interface, and the introduction of loadbalancerclient, need to Host,port, and the configuration is also very unfriendly, the following article will introduce the Ribbon

Spring Cloud Introductory Eureka-consumer service consumption (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.