前言:了解eureka的服務端和客戶端
Eureka是Netflix開發的服務發現框架,SpringCloud將它集成在自己的子項目
spring-cloud-netflix中,實現SpringCloud的服務發現功能。Eureka包含兩個組件:
Eureka Server和Eureka Client。
Eureka Server提供服務注冊服務,各個節點啟動后,會在Eureka Server中進行注
冊,這樣EurekaServer中的服務注冊表中將會存儲所有可用服務節點的信息,服務節點
的信息可以在界面中直觀的看到。
Eureka Client是一個java客戶端,用於簡化與Eureka Server的交互,客戶端同時也
有一個內置的、使用輪詢(round-robin)負載算法的負載均衡器。在應用啟動后,將會
向Eureka Server發送心跳,默認周期為30秒,如果Eureka Server在多個心跳周期內沒有
接收到某個節點的心跳,Eureka Server將會從服務注冊表中把這個服務節點移除(默認90
秒)。
一、創建一個父工程
1、父工程主要用來管理子模塊的jar包依賴
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.offcn</groupId> <artifactId>microservice_cloud_01</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>microservice_cloud_02_api</module> <module>microservice_cloud_03_provider_product_8001</module> <module>microservice_cloud_04_consumer_product_80</module> <module>microservice_cloud_06_eureka_6002</module> <module>microservice_cloud_06_eureka_6001</module> </modules> <!--父工程 手動指定pom--> <packaging>pom</packaging> <!----> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.7.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!--jdk版本1.8--> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <!--junit版本--> <junit.version>4.12</junit.version> <!--SpringCloud版本--> <spring-cloud.version>Finchley.SR2</spring-cloud.version> </properties> <!--父工程只做依賴管理,不做實際依賴--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <!--maven項目只能單繼承,因此這里使用 import--> <scope>import</scope> </dependency> <!--mybatis整合springboot--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency> <!--mysql驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <!--junit測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> </project>
二、創建一個子模塊作為第一個eureka服務端
1、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"> <parent> <artifactId>microservice_cloud_01</artifactId> <groupId>com.offcn</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservice_cloud_06_eureka_6001</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
2、配置文件
#內置的tomcat服務啟動監聽端口號
server:
port: 6001
#EurekaServer配置
eureka:
instance:
hostname: eureka6001.com
client:
register-with-eureka: false #此EurekaServer不在注冊到其他的注冊中心
fetch-registry: false #不在從其他中心中心拉取服務器信息
service-url:
defaultZone: http://eureka6002.com:6002/eureka #注冊中心訪問地址,兩個eureka之間相互注冊,構建集群,當某一個服務端癱瘓,另外一個還能用
3、主啟動類
1 package com.offcn.springcloud; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 import org.springframework.context.annotation.Configuration; 7 8 @SpringBootApplication 9 @EnableEurekaServer 10 public class EurekaServer_6001 { 11 12 public static void main(String[] args) { 13 SpringApplication.run(EurekaServer_6001.class,args); 14 } 15 }
三、創建一個子模塊作為第二個euraka服務端,構建集群
1、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"> <parent> <artifactId>microservice_cloud_01</artifactId> <groupId>com.offcn</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservice_cloud_06_eureka_6002</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> </project>
2、配置文件
#內置的tomcat服務啟動監聽端口號
server:
port: 6002
#EurekaServer配置
eureka:
instance:
hostname: eureka6002.com
client:
register-with-eureka: false #此EurekaServer不在注冊到其他的注冊中心
fetch-registry: false #不在從其他中心中心拉取服務器信息
service-url:
defaultZone: http://eureka6001.com:6001/eureka #注冊中心訪問地址,兩台euraka服務端相互注冊
3、主啟動類
1 package com.offcn.springcloud; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 @SpringBootApplication 8 @EnableEurekaServer 9 public class EurekaServer_6002 { 10 public static void main(String[] args) { 11 SpringApplication.run(EurekaServer_6002.class,args); 12 } 13 }
四、創建兩個euraka服務端遇到的問題
由於是在同一台電腦上,因此主機ip相同,為了更好的顯示效果,上面將主機ip 和域名映射
步驟1:找到C:\Windows\System32\drivers\etc下的hosts文件
步驟2:將主機ip和域名進行映射
此時我們就可以用映射的域名來代替主機ip了,可以更好的演示效果
五、創建一個子模塊作為euraka客戶端
1、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"> <parent> <artifactId>microservice_cloud_01</artifactId> <groupId>com.offcn</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservice_cloud_03_provider_product_8001</artifactId> <dependencies> <!--microservice_cloud_02_apixian--> <dependency> <groupId>com.offcn</groupId> <artifactId>microservice_cloud_02_api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--web啟動器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!--SpringBoot整合Mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!--druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <!--mysql驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--eureka-client--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> </project>
2、配置文件
server:
port: 8001
spring:
application:
name: microservice-product #這個很重要,這在以后的服務與服務之間相互調用一般都是根據這個name
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud?serverTimezone=UTC
username: root
password: ROOT
dbcp2:
min-idle: 5 #最小連接數
initial-size: 5 #初始化連接數
max-total: 20 #最大連接數
max-wait-millis: 150 #等待連接獲取的最大超時時間
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
type-aliases-package: com.offcn.springcloud.entities # 所有Entity別名類所在包,因私在mapper映射文件中,就不需要寫全類名了,只用寫類名就行
#指定mapper映射文件的位置,當mapper映射文件和mapper接口不同包的情況下,配置這個就會生效
mapper-locations: classpath:mybatis/mapper/**/*.xml
eureka:
client:
register-with-eureka: true #此EurekaServer注冊到其他的注冊中心
fetch-registry: true #從其他中心中心拉取服務器信息
service-url:
defaultZone: http://eureka6002.com:6002/eureka,http:///eureka6001.com:6001/eureka #注冊中心訪問地址
3、主啟動類
1 package com.offcn.springcloud; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 7 8 @SpringBootApplication 9 @MapperScan("com.offcn.springcloud.mapper") 10 @EnableEurekaClient 11 public class HelloApplication { 12 public static void main(String[] args) { 13 SpringApplication.run(HelloApplication.class,args); 14 } 15 16 }
六、測試
1、訪問第一個eureka服務端
2、訪問第二個服務端