springCloud Finchley 微服務架構從入門到精通【二】Eureka服務註冊與發現
阿新 • • 發佈:2019-01-27
一、開發工具說明
為了防止程式碼執行錯誤,建議使用一致的版本:
開發工具:Eclipse : Version: Neon.3 Release (4.6.3)
開發spring cloud應用推薦使用 idea或者spring官方提供的STS工具,筆者由於公司內部使用習慣等原因,還是選擇了Eclipse安裝STS外掛
JDK : jdk1_8_131
MAVEN :maven 3.5
二、建立Eureka註冊中心
1、建立一個Maven主專案
刪除其目錄下的src包,只剩下pom.xml
這裡打包選擇pom,最終其子專案重複依賴統一提取到這裡,這就是主專案的主要作用
2、在主專案上建立一個springboot專案
選擇 Eureka Server ,專案建立完成
此時pom.xml為:
<?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>
<groupId>com.mayi.springcloud</groupId>
<artifactId>commonservice-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>commonservice-eureka</name >
<description>eureka 註冊中心</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.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>
<spring-cloud.version>Finchley.M9</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>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
3、配置bootstrap.yml
將resources目錄下的application.properties改名為bootstrap.yml(建議使用格式化ymal語言,好處自己琢磨吧^_^),ymal 校驗比較嚴格,我的是換行空兩格,保持統一,編輯器也是有提示的
server:
port: 8761
spring:
application:
name: service-registry
eureka:
instance:
prefer-ip-address: true
client:
fetch-registry: false
register-with-eureka: false
server:
wait-time-in-ms-when-sync-empty: 0
解釋:
- 配置為server :
fetch-registry: false
register-with-eureka: false
4、啟動類配置
新增@EnableEurekaServer註解即可
package com.mayi.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class CommonserviceEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(CommonserviceEurekaApplication.class, args);
}
}
使用SpringBootApp啟動,訪問地址:localhost:8761
正常開啟以上網頁,說明Eureka註冊中心配置成功。
三、建立客戶端專案
1、pom.xml 同上,bootstrap.yml簡單程式碼如下:
spring:
application:
name: service-eureka-client
server:
port: 8800
2、啟動類新增@EnableEurekaClient註解
這裡建議使用@EnableEurekaClient註解來宣告客戶端,因為我們使用的是Eureka註冊中心,如果使用zookeeper或其他註冊中心使用@EnableDiscoveryClient,而對於Eureka註冊中心來說兩者都可
package com.mayi.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class CommonserviceEurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(CommonserviceEurekaClientApplication.class, args);
}
}
3、啟動專案
如圖,客戶端註冊成功:
接下來,我會依次整新文章,直至整個架構完成,如有興趣的朋友加我微信加入spring cloud社群群
或關注微信公眾號:java架構師修行