1. 程式人生 > >搭建微服務歷程-1 搭建註冊中心

搭建微服務歷程-1 搭建註冊中心

搭建註冊中心

1.建立mvn專案。引入pom檔案

  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>AeurekaServer</groupId>
  <artifactId>AeurekaServer</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>

  <name>eureka-server</name>

    <description>Demo project for Spring Boot</description>    <parent>        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.6.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.cloud</groupId>            <artifactId>spring-cloud-starter-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>Dalston.SR3</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></project>

2.編寫application.properties檔案

#server.port=1111表示設定該服務註冊中心的埠號

server.port=1111

#eureka.instance.hostname=localhost表示設定該服務註冊中心的hostname

eureka.instance.hostname=localhost

#由於我們目前建立的應用是一個服務註冊中心,而不是普通的應用,

#預設情況下,這個應用會向註冊中心(也是它自己)註冊它自己,設定為false表示禁止這種預設行為

eureka.client.register-with-eureka=false

#表示不去檢索其他的服務,因為服務註冊中心本身的職責就是維護服務例項,它也不需要去檢索其他服務

eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

3.寫入口類

@EnableEurekaServer

@SpringBootApplication

public class EurekaServerApplication {

    public static void main(String[] args) {

        SpringApplication.run(EurekaServerApplication.class, args);

    }

}

啟動這個spring boot服務 在網頁上輸入http://localhost:1111 就可以看到註冊中心頁面了