Eureka Hands-on cluster configuration __eureka

Source: Internet
Author: User

First, create Eurekaserver

1, create the first server, create a springboot project

1 Java code is as follows, using port 7070:

@EnableEurekaServer
@SpringBootApplication
@RestController public
class Eurekaserverapplication {
	@RequestMapping ("/exec") public  
	String Execinfo () {
		System.out.println ("Server01");//danielinbiti return
		"Server01";
	}
    public static void Main (string[] args) {
        springapplication.run (eurekaserverapplication.class, args);
    }

2) application.properties configuration is as follows

server.port=7070
spring.application.name=server-01
eureka.client.register-with-eureka=true
Eureka.client.fetch-registry=false
eureka.client.serviceurl.defaultzone=http://localhost:7070/eureka/, http://localhost:7020/eureka/
eureka.instance.metadatamap.instanceid=${spring.application.name}:${ Spring.application.instance_id:${random.value}}

2, create a second server, create a springboot project

1 Java code below, using port 7020

@EnableEurekaServer
@SpringBootApplication
@RestController public
class Eurekaserverapplication {
	@RequestMapping ("/exec") public  
	String Execinfo () {
		System.out.println ("Server02");
		return "Server02";
	}
    public static void Main (string[] args) {
        springapplication.run (eurekaserverapplication.class, args);
    }


2) application.properties

server.port=7020
spring.application.name=server-01
eureka.client.register-with-eureka=true
Eureka.client.fetch-registry=false
eureka.client.serviceurl.defaultzone=http://localhost:7070/eureka/, http://localhost:7020/eureka/
eureka.instance.metadatamap.instanceid=${spring.application.name}:${ Spring.application.instance_id:${random.value}}

Second, the creation of Eurekaclient project

1, Java code

@SpringBootApplication @EnableEurekaClient @RestController @EnableAutoConfiguration public class 
	eurekaclientapplication {@Autowired private discoveryclient discoveryclient;
	@RequestMapping ("/getinfo") public String GetInfo () {return "EkClient2";
	@Bean @LoadBalanced public resttemplate resttemplate (Resttemplatebuilder builder) {return builder.build ();
	
	@Autowired private Resttemplate resttemplate; @RequestMapping ("/exec") public string Execinfo () {return resttemplate.getforobject ("http://server-01/exec/", string
	. Class); @RequestMapping ("/registered") public String getregistered () {list<serviceinstance> List = Discov  
        Eryclient.getinstances ("STORES");  
        System.out.println (Discoveryclient.getlocalserviceinstance ());  
          System.out.println ("discoveryclient.getservices (). Size () =" + discoveryclient.getservices (). Size ()); Comment by Danielinbiti for (String S:discoveryclient.getserviceS ()) {System.out.println ("services" + s);  
            list<serviceinstance> serviceinstances = discoveryclient.getinstances (s); for (Serviceinstance si:serviceinstances) {System.out.println ("services:" + S + ": gethost () =" + Si.  
                GetHost ());  
                SYSTEM.OUT.PRINTLN ("services:" + S + ": getport () =" + Si.getport ());  
                SYSTEM.OUT.PRINTLN ("services:" + S + ": getserviceid () =" + Si.getserviceid ());  
                SYSTEM.OUT.PRINTLN ("services:" + S + ": geturi () =" + Si.geturi ());  
            SYSTEM.OUT.PRINTLN ("services:" + S + ": getmetadata () =" + Si.getmetadata ());  
        } System.out.println (List.size ());  
        if (list!= null && list.size () > 0) {System.out.println (list.get (0). GetURI ());  
    Return to "success"; public static void Main (string[] args) {SpringapplicatiOn.run (Eurekaclientapplication.class, args); }
}

2, application.properties configuration file

server.port=6060
spring.application.name=client-02
eureka.client.serviceurl.defaultzone=http:// localhost:7070/eureka/
eureka.instance.metadatamap.instanceid=${pro_test:5555}

Third, after starting the above services, then IE Address bar access

Http://localhost:6060/exec will come back and forth Server01,server02


Four, note the point:

Resttemplate creation is a need to add a load policy, @LoadBalanced

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.