SpringCloud service registration and discovery of Eureka (a)

The basic architecture of a Eureka

Spring Cloud encapsulates Netflix Eureka module developed to implement the service registration and discovery (please compare Zookeeper).

Eureka using the CS design architecture. Eureka Server as a service registration function of the server, which is a service registry.

And other micro-services system, use the client to connect to Eureka Eureka Server and maintain a heartbeat connection. Such systems maintenance personnel can monitor each micro system service is functioning properly by Eureka Server. SpringCloud some of the other modules (such as Zuul) can be found in other micro-services system through Eureka Server, and implementation of relevant logic.

Two of the three roles in Eureka

    

  • Eureka Server service registration and discovery

  • Service Provider The service provider will register their services to Eureka, so that the service consumer to find

  • Service Consumer service consumer to obtain a list of registered services from Eureka, thereby Consumer Services

Construction of three steps

  • Create a eureka-server project

    • pom.xml


     
     <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-server</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>${spring-cloud.version}</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>

     

    • YML

      server:
        port: 8761
      ​
      eureka:
        instance:
          hostname: localhost
        client:
          registerWithEureka: false
          fetchRegistry: false
          serviceUrl:
            defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

       

    • Startup class

      @SpringBootApplication
      @EnableEurekaServer
      public class EurekaServerApplication {
      ​
          public static void main(String[] args) {
              SpringApplication.run(EurekaServerApplication.class, args);
          }
      ​
      }

       

    • Start results

  • Create a producer-service

    • pom.xml

       1   <dependencies>
       2         <dependency>
       3             <groupId>org.springframework.boot</groupId>
       4             <artifactId>spring-boot-starter-actuator</artifactId>
       5         </dependency>
       6         <dependency>
       7             <groupId>org.springframework.boot</groupId>
       8             <artifactId>spring-boot-starter-web</artifactId>
       9         </dependency>
      10         <dependency>
      11             <groupId>org.springframework.cloud</groupId>
      12             <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
      13         </dependency>
      14 15         <dependency>
      16             <groupId>org.springframework.boot</groupId>
      17             <artifactId>spring-boot-starter-test</artifactId>
      18             <scope>test</scope>
      19         </dependency>
      20     </dependencies>
      21 22     <dependencyManagement>
      23         <dependencies>
      24             <dependency>
      25                 <groupId>org.springframework.cloud</groupId>
      26                 <artifactId>spring-cloud-dependencies</artifactId>
      27                 <version>${spring-cloud.version}</version>
      28                 <type>pom</type>
      29                 <scope>import</scope>
      30             </dependency>
      31         </dependencies>
      32     </dependencyManagement>
      33     <!-- 微服务info内容详细信息 -->
      34     <build>
      35         <finalName>microservicecloud</finalName>
      36         <resources>
      37             <resource>
      38                 <directory>src/main/resources</directory>
      39                 <filtering>true</filtering>
      40             </resource>
      41         </resources>
      42         <plugins>
      43             <plugin>
      44                 <groupId>org.apache.maven.plugins</groupId>
      45                 <artifactId>maven-resources-plugin</artifactId>
      46                 <configuration>
      47                     <delimiters>
      48                         <delimit>$</delimit>
      49                     </delimiters>
      50                 </configuration>
      51             </plugin>
      52         </plugins>
      53     </build>
    • YML

      Eureka: 
        Client: 
          serviceUrl: 
            defaultzone: HTTP: // localhost: 8761 / Eureka / 
        instance:          
          instance -id: product- service8080 # Host Name: The name of the service to modify 
          the prefer -ip-address: to true    # access path to display the IP address of 
      server : 
        Port: 8080 
      the Spring: 
        the Application: 
          name: Product - service 
      # micro-content service info detailed 
      info: 
        the app.name: Product - Servic 
        company.name: www.topcheer.com

       

    • Startup class

      @SpringBootApplication
      public class ProducerServiceApplication {
      ​
          public static void main(String[] args) {
              SpringApplication.run(ProducerServiceApplication.class, args);
          }
      ​
      }
      ​
    • result:

                    

Guess you like

Origin www.cnblogs.com/dalianpai/p/11687145.html