(一)Eureka搭建服务注册中心
首先,創建一個基礎的Spring Boot工程,命名為eureka-server, 并在pom.xml 中引入必要的依賴內容, 代碼如下:
<modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.2.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.forezp</groupId><artifactId>eureka-server</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-server</name><description>Demo project for Spring Boot</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Finchley.RELEASE</spring-cloud.version><!--<spring-cloud.version>Greenwich.RC2</spring-cloud.version>--></properties><dependencies><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><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url></repository></repositories>通過@EnableEurekaServer 注解啟動一個服務注冊中心提供給其他應用進行對話。 這一步非常簡單, 只需在一個普通的 Spring Boot 應用中添加這個注解就能開啟此功能, 比 如下面的例子:
package com.forezp.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);}}在默認設置下, 該服務注冊中心也會將自己作為客戶端來嘗試注冊它自己,所以我們需要禁用它的客戶端注冊行為, 只需在 applicatition.yml中增加如下配置:
server:port: 8761eureka:instance:hostname: localhostclient:registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/spring:application:name: eurka-server由于后續內容也都會在本地運行,為了與后續要進行注冊的服務區分,這里將服務注 冊中心的端口通過server.port屬性設置8761。
? eureka.client.register-with-eureka: 由于該應用為注冊中心,所以設置為 false, 代表不向注冊中心注冊自己。
? eureka.client.fetch-registry: 由于注冊中心的職責就是維護服務實例, 它并不需要去檢索服務, 所以也設置為 false。
在完成了上面的配置后,啟動應用并訪問 http://localhost: 8761/。可以看到如下圖所示的 Eureka 信息面板, 其中 Instances currently registered with Eureka 欄是空的, 說明該注冊中心還沒有注冊任何服務。
?
?
總結
以上是生活随笔為你收集整理的(一)Eureka搭建服务注册中心的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Word2vec学习笔记总结
- 下一篇: STM32开发 -- Secure CR