java B2B2C原始碼電子商務平臺 ---搭建Eureka註冊中心
阿新 • • 發佈:2018-12-07
一 建立一個Spring Boot工程,命名為eureka-server,並在pom.xml中引入必要的依賴,程式碼如下。願意瞭解原始碼的朋友直接求求交流分享技術:二一四七七七五六三三
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.7.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!--<dependency>--> <!--<groupId>org.springframework.boot</groupId>--> <!--<artifactId>spring-boot-starter-actuator</artifactId>--> <!--</dependency>--> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
二 通過@EnableEurekaServer註解啟動一個服務註冊中心提供給其他應用程式進行對話,只需要在Spring Boot應用中新增下面這個註解就能開啟此功能。
@EnableEurekaServer @SpringBootApplication public class Application { public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }
三 在預設情況下,服務註冊中也會將自己作為客戶端來嘗試註冊它自己,所以需要禁用它的客戶端行為。
application.properties中增加如下配置。
spring.application.name=eureka-server server.port=1111 eureka.instance.hostname=localhost # 關閉保護機制 #eureka.server.enable-self-preservation=false eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/ logging.file=${spring.application.name}.log
說明:
eureka.client.register-with-eureka:由於該應用為註冊中心,所以設定為false,代表不向註冊中心註冊自己。
eureka.client.fetch-registry:由於註冊中心的職責就是維護服務例項,它並不需要去檢索服務,所以也設定為false。
整體程式碼結構如下: