springcloud提供了一整套可行的構建分佈式系統的方案,使的企業/開發人員可以快速溝通分佈式系統,今天快速構建下springcloud的註冊中心Eureka。html
COORDINATE ANYTHING: DISTRIBUTED SYSTEMS SIMPLIFIEDjava
Building distributed systems doesn't need to be complex and error-prone. Spring Cloud offers a simple and accessible programming model to the most common distributed system patterns, helping developers build resilient, reliable, and coordinated applications. Spring Cloud is built on top of Spring Boot, making it easy for developers to get started and become productive quickly.spring
使用的idea終極版,springcloud項目自己就是springboot項目(springboot與springcloud的關係)。apache
首先在idea中建立項目:file-->new-->projectspringboot
兩個默認next後選擇組件app
默認next->finishmaven
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.spc</groupId> <artifactId>eurekaserver</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eurekaserver</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</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> </project>
說明:分佈式
(1)springcloud自己是springboot項目,新建springboot項目。ide
(2)須要兩個starter,eureka-server和eureka-clientspring-boot
package com.spc.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaserverApplication { public static void main(String[] args) { SpringApplication.run(EurekaserverApplication.class, args); } }
說明:
比springboot項目多了一個標籤:@EnableEurekaServer,該標籤用於標註是註冊中心。
spring: application: name: registry server: port: 8761 eureka: client: register-with-eureka: false service-url: defaultZone: http://localhost:8761/eureka/
說明:
(1)name爲項目名稱;
(2)port: 8761爲啓動端口。
(3)eureka下面的配置暫時不用,後續用於在高可用配置的時候,做爲客戶端註冊到另外一臺註冊中心,實現高可用。
I'm 軟件老王,若是以爲還能夠的話,關注下唄!若有不許確或疑問的地方,可經過討論區、QQ溝通,多謝!
原文出處:https://www.cnblogs.com/ruanjianlaowang/p/11204981.html