Service discovery in spring cloud Eureka

1. Eureka server cluster development

1. Create a parent project first

If it is not a normal demo, and there are other configurations, please note that if the service cannot be started, the label < dependency Management > may be required in the parent class dependency.

2. Create two more sub projects

1. Import dependency

     <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
            <version>2.1.2.RELEASE</version>
        </dependency>

2. Configuration file application.yml

#Built in tomcat service start listening port number
server:
  port: 6002

#EurekaServer configuration
eureka:
  instance:
    hostname: eureka6002  #The name of EurekaServer
  client:
    register-with-eureka: false #This EurekaServer is not registered to another registry
    fetch-registry: false       #Not pulling server information from other central centers
    service-url:
      defaultZone: http://eureka6001.com:6001/eureka #Registry access address

---------------------------------------
#Built in tomcat Service start listening port number
server:
port: 6001
#EurekaServer To configure
eureka:
instance:
hostname: eureka6001
client:
register-with-eureka: false #this EurekaServer Not registering to another registry
fetch-registry: false #Not pulling server information from other central centers
service-url:
defaultZone: http://eureka6002.com:6002/eureka ා address of Registration Center
 

3. Main startup class

@SpringBootApplication  //Mark this as the main startup class
@EnableEurekaServer    //Mark this as EurekaServer
public class AppStart2 {

    public static void main(String[] args) {
        SpringApplication.run(AppStart2.class, args);
    }
}

2. Microservice configuration

1. Import dependency, the above dependency

2. Configure application.yml to add

eureka:
  client:
    register-with-eureka: true #Allow registration to EurekaServer
    fetch-registry: true       #Pull server information from other central centers
    service-url:
      defaultZone: http://eureka6002.com:6002/eureka,http://eureka6001.com:6001/eureka #Registry access

3. Main startup class

@SpringBootApplication
@EnableEurekaClient  //Mark this is an Euerka client and must write
public class ProductProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(ProductProvider_8001.class,args);
    }
}

3. Startup project

Visit http://localhost:6001 / and http://localhost:6002 /

 

 

This is a demo. There are many disadvantages. For example, when hovering under status, you can see the IP in the lower left corner.

Tags: Java Tomcat Spring

Posted on Thu, 07 Nov 2019 13:12:30 -0800 by coditoergosum