023-spring Boot Service Registration and discovery

Source: Internet
Author: User
Tags zookeeper client

I. Overview

Service invocation

1.1. Nginx Mode

  

1.2. Registration Center

  

Second, the registration center "zookeeper" 2.1, the installation of zookeeper3.4.112.2, service providers, need to start the service, the Service information (Ip,port) registered to the registration center.

In the mall-product of the previous section, POM

        <Dependency>            <groupId>Org.apache.curator</groupId>            <Artifactid>Curator-x-discovery-server</Artifactid>            <version>2.12.0</version>        </Dependency>

Add configuration

zookeeper.address=127.0.0.1:2181

Add Registration Class Serviceregister

@Component Public classServiceregisterImplementsApplicationrunner {@Value ("${zookeeper.address}")    PrivateString zkaddress; @Override Public voidRun (applicationarguments args)throwsException {curatorframework client= Curatorframeworkfactory.newclient (zkaddress,NewRetryonetime (1000));        Client.start ();        Client.blockuntilconnected (); Serviceinstance<Object> instance = Serviceinstance.builder (). Name ("Product"). Address ("127.0.0.1"). Port (8080). Build ();//Registered address and Port Servicediscovery<Object> discovery = Servicediscoverybuilder.builder (Object.class). Client (client). BasePath ("/soa"). build ();        Discovery.registerservice (instance);    Discovery.start (); }}

You can view the zookeeper client again

LS///soa//soa/product/d5770a1f-640f-4b17-b5d0-bfb2fd4b0623

View Registration Service Details

 {"Name": "Product", "id": "d5770a1f-640f-4b17-b5d0-bfb2fd4b0623", "Address": "127.0.0.1", "Port": 8080, "Sslport" : null , "payload": null , " REGISTRATIONTIMEUTC ": 1523196340298," servicetype ":" DYNAMIC "," Urispec ": null  , "Enabled": true  }czxid  = 0x10ctime  = Sun Apr 22:05:40 CST 2018mzxid  = 0x10mtime  = Sun Apr 22:05:40 CST 2018pzxid  = 0x10cversion  = 0dataversion  = 0aclversion  = 0ephemeralowner  = 0x1007c571a950003 Datalength  = 216numchildren  = 0 
2.3. Caller

Pom

        <Dependency>                            <groupId>Org.apache.curator</groupId>                        <Artifactid>Curator-x-discovery</Artifactid>                   <version>2.12.0</version>        </Dependency>

When making a call, you need to get the service address from the registry and then call it based on the service address that was obtained.

Call

     Public Static voidMain (string[] args)throwsException {curatorframework client= Curatorframeworkfactory.newclient ("127.0.0.1:2181",NewRetryonetime (1000));        Client.start ();        Client.blockuntilconnected (); Servicediscovery<Object> discovery = Servicediscoverybuilder.builder (Object.class). Client (client). BasePath ("/soa"). build (); Collection<ServiceInstance<Object>> instances = discovery.queryforinstances ("Product"); Instances.foreach (item)-{System.out.println (item.getaddress ());            System.out.println (Item.getport ()); Resttemplate RT=Newresttemplate (); String String= Rt.getforobject ("/http" + item.getaddress () + ":" + item.getport () + "/SOA/PRODUCT/1", String.class);            System.out.println (string); Response<Product> Fromjson =NewGson (). Fromjson (String,NewTypetoken<response<product>>() {}.gettype ()); System.out.println (NewGson (). ToJson (Fromjson));    }); }

If you have more than one service, you can increase the load class

View Source: Https://github.com/bjlhx15/spring-boot.git

In the

One of the Mall-product

Register Registration Center

One of the Mall-webz

The client is the test code, LoadBalance is responsible for the Equalization class

023-spring Boot Service Registration and discovery

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.