Spring-boot 2 Multi-modular project and Eurekaserver construction

Source: Internet
Author: User

Spring Boot because of its

1, easy to develop and maintain. 2, a single micro-service start fast. 3, local modification deployment is easy. 4, the technology stack is not subject to language restrictions and other advantages by more and more companies pay attention to. Spring-boot also integrates many frameworks for microservices development (such as configuration management, service discovery, circuit breakers, intelligent routing, micro-proxies, control buses, one-time tokens, global locks, leading elections, distributed sessions, cluster states), which frees us from tedious configuration for deploying microservices.

Let's learn how to build an e-commerce project using Spring-boot.

Spring Boot 2.0.0-snapshot requires Java 8 and the Spring Framework 5.0.2 or more, Maven 3.2 or Gradle 4.

This article uses the spring Boot 2.0.0-snapshot Java9 and the spring Framework above 5.0.2.RELEASE, Maven 3.3.9. The development tool uses the Spring Suit tool 3.9.1 (STS), which is officially provided by Sping.

One, spring multi-Modular project construction

First we need to build a Spring-boot parent project, and a multi-module project is defined by referring to one or more sub-modules by a parent POM.

    <!--define the path to the Spring-cloud -    <groupId>Com.hzt.cloud</groupId>    <Artifactid>Esupermarket</Artifactid>    <!--Customizing the version of the parent project -    <version>1.0.0</version>    <!--If the parent project, the package must be a pom -    <Packaging>Pom</Packaging>

and delete all files in the root directory, leaving only Pom.xml.

    <!--To inherit the current project Spring-boot 2.0 -    <Parent>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter-parent</Artifactid>        <version>2.0.0.RELEASE</version>    </Parent>

Then we define the version of the spring-boot we need, so our project is using the Spring-boot 2.0 version.

 <!--  Defines a submodule that inherits from this parent class, and the module fills in the sub-module Artifactid  -->  <  modules  >  <  module  >  eureka-server</ module  >  <  Span style= "COLOR: #800000" >module  >  eureka-server1</ module  >  </ modules  > 

Also insert the submodule that we are about to define, so that the module can use the dependencies introduced in the parent module.

<dependencymanagement>        <Dependencies>        <!--Join Spring-boot dependency on the Web -            <Dependency>                <groupId>Org.springframework.boot</groupId>                <Artifactid>Spring-boot-starter-web</Artifactid>            </Dependency>            <!--Unified Dependency Management -            <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>

Join Spring-boot about the Web and the unified dependency Management module.

<!--Warehouse Management -    <repositories>        <Repository>            <ID>Spring-milestones</ID>            <name>Spring Milestones</name>            <URL>Https://repo.spring.io/milestone</URL>            <Snapshots>                <enabled>False</enabled>            </Snapshots>        </Repository>    </repositories>

This allows the pom of our parent module to be built successfully.

Second, sub-module Eurekaserver service Construction

Right-click the parent module, new, Mavenmodule, to create a submodule

1, the Pom.xml configuration

  

<modelversion>4.0.0</modelversion>    <!--project name, no GroupID and name tags required -    <Artifactid>Eureka-server1</Artifactid>    <!--Packing Method -    <Packaging>Jar</Packaging>        <!--The inherited parent class, which is the parent module -    <Parent>        <groupId>Com.hzt.cloud</groupId>        <Artifactid>Esupermarket</Artifactid>        <!--version label defined for the parent class -        <version>1.0.0</version>        <RelativePath/>    </Parent>    <Dependencies>    <!--introduction of dependency on Eureka-server -        <Dependency>            <groupId>Org.springframework.cloud</groupId>            <Artifactid>Spring-cloud-starter-netflix-eureka-server</Artifactid>        </Dependency>
<!--for registration center Access account authentication
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactid>spring-boot-starter-security</artifactid>
</Dependency> </Dependencies>

Among them, Spring-boot 2.0 and 2.0 were introduced eureka-server before the artifactid difference.

2. Startup class

 @SpringBootApplication //  start annotations, Represents an ingress class for this spring-boot  @EnableEurekaServer //  This line of annotations represents the registration of a component for a service, and this annotation applies only to Eureka  //  @EnableDiscoveryClient  //  public  class   eurekaserverapplication { public  static  void   main ( String[] args) {Springapplication.run (eurekaserverapplication. ) class     

We can define different annotations based on different service discovery methods. @enablediscoveryclient Annotations can support other service discovery methods.

3. APPLICATION.YML configuration file

Server:  #配置eurekaServer的端口号  port:8761security:  basic:    #代表开启账号密码认证, login eurekaserver need to enter the account password    Enabled:true   User:    name:root    Password:123eureka:  client:    service-url:       # Represents the address registered to the Eureka Server      defaultzone:http://root:[email Protected]:8761/eureka      #该服务为注册中心, adding this line represents not registering themselves in the registry    Register-with-eureka:false    #由于注册中心呢的职责是维护实例, false to indicate that it does not need to retrieve the service    Fetch-registry:false    #配置主机名  instance:    hostname:peer1    #配置服务名spring:  application:    name:eureka-server    

In the same vein, we can configure a eurekaServer1 to build eurekaserver high-availability clusters. Where Peer1 is the load address configured on my local machine, pointing to www.peer1.com. Start the server.

  

Spring-boot 2 Multi-modular project and Eurekaserver construction

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.