一、采用Eureka作為服務注冊與發現的元件
1)首先建立一個maven主工程。
建立一個主Maven工程,在其pom檔案引入依賴,spring Boot版本為2.0.3.RELEASE,Spring Cloud版本為Finchley.RELEASE。這個pom檔案作為父pom檔案,起到依賴版本控制的作用,其他module工程繼承該pom。
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>
<groupId>com.eBao</groupId>
<artifactId>SpringCloudPom</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>sc-f-chapter1</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
<relativePath/>
</parent>
<modules>
<module>server</module>
<module>client</module>
</modules>
<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>
</properties>
<dependencies>
<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>
2)建立2個model工程:一個model工程作為Eureka Server,另一個作為Eureka Client。
server建立如下:
右鍵工程->建立model-> 選擇spring initialir->下一步(填寫項目名)->下一步(選擇cloud discovery->eureka server) ,然後一直下一步就行了。
建立完成後檢視建立的model中pom含有spring-cloud-starter-netflix-eureka-server的依賴即可,無需自己引入。
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>
<groupId>com.example</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- <modules>
<module>eureka-server</module>
<module>service-hi</module>
</modules>-->
<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>Greenwich.M1</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)啟動服務中心server
一個注解@EnableEurekaServer,這個注解需要在springboot工程的啟動application類上加:
package com.example.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
4)eureka server的配置檔案appication.properties
eureka是一個高可用的元件,它沒有後端緩存,每一個執行個體注冊之後需要向注冊中心發送心跳(是以可以在記憶體中完成),在預設情況下erureka server也是一個eureka client ,必須要指定一個 server。
server.port=8761
#目前執行個體的主機名稱
eureka.instance.hostname=localhost
#false代表不向注冊中心注冊自己(因為本身就是注冊中心),預設是true,如果不設為false,啟動會報找不到注冊中心的錯誤
eureka.client.register-with-eureka=false
#注冊中心用于維護服務執行個體,無需檢索服務,故設為false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
5)啟動工程、打開浏覽器通路:http://localhost:8761 ,界面如下:
No application available 沒有服務被發現、因為沒有注冊服務
6)現在來建立一個服務 (eureka client)
建立過程同server類似:
右鍵工程->建立model-> 選擇spring initialir->下一步(填寫項目名)->下一步(選擇cloud discovery->eureka Discovery) ,然後一直下一步就行了。
建立完成後檢視建立的model中pom含有spring-cloud-starter-netflix-eureka-client的依賴即可,無需自己引入。
pom檔案如下、這裡引入了spring-boot-starter-web的依賴:
<?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.example</groupId>
<artifactId>client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.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>Greenwich.M1</spring-cloud.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
7)通過注解@EnableEurekaClient 表明自己是一個eureka-client
package com.example.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
return "hi " + name + " ,i am from port:" + port;
}
}
還需要在配置檔案中注明自己的服務注冊中心的位址,application.properties配置檔案如下:
server.port=8762
spring.application.name=service-hi
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
8)啟動工程,打開http://localhost:8761 ,即eureka server 的網址:
你會發現一個服務已經注冊在服務中了,服務名為SERVICE-HI ,端口為7862
這時打開 http://localhost:8762/hi?name=forezp ,你會在浏覽器上看到 :
hi forezp,i am from port:8762
注:
這裡可以啟動多個執行個體
啟動eureka-server 工程;啟動service-hi工程,它的端口為8762;将service-hi的配置檔案的端口改為8763,并啟動,這時你會發現:service-hi在eureka-server注冊了2個執行個體,這就相當于一個小的叢集。
如何在IDEA啟動多個執行個體:
點選Edit Configuration
打開配置後,将預設的Single instance only(單執行個體)的鈎去掉即可