廢話不多說直接上步驟:
環境WIN10 + idea + spring boot 2.1.2.RELEASE
創建註冊中心
第一步:創建maven項目
第二步:選擇maven結構
第三步:輸入名稱
第四步:生成結構
這種創建方式呢需要自己加後續的東西,pom文件,啓動類,yml文件等…
下面說一下這種創建方式比較簡單自己省去加東西的時間:
第一步:創建項目
第二步:選擇Spring Initializr
第三步:輸入自己的包路徑和工程名
第四步:如下圖後即可完成
第五步:添加修改yml文件
server:
port: 8761
eureka:
client:
registerWithEureka: false #false:不作爲一個客戶端註冊到註冊中心
fetchRegistry: false #爲true時,可以啓動,但報異常:Cannot execute
如果是properties文件
spring.application.name=eureka-server
#服務註冊中心端口號
server.port=8761
#服務註冊中心實例的主機名
eureka.instance.hostname=localhost
#是否向服務註冊中心註冊自己
eureka.client.register-with-eureka=false
#是否檢索服務
eureka.client.fetch-registry=false
#服務註冊中心的配置內容,指定服務註冊中心的位置
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
第六步:啓動類加上@EnableEurekaServer
第七步:加載pom中jar包,下載完成後不報錯開始啓動
第八步:啓動完成端口默認是yml的8761
最後:訪問下路徑http://localhost:8761/
註冊中心啓動完成。
創建服務提供者
第一步:
第二步:
完成pom如下:
<?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.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hlw</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
application配置文件如下yml:
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false #false:不作爲一個客戶端註冊到註冊中心
fetchRegistry: false #爲true時可以啓動,但報異常:Cannot execute
如果是properties文件:
spring.application.name=eureka-client
server.port=8762
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
啓動類如下:
@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
打開eureka註冊中心後: