一、Eureka服務(wù)注冊與發(fā)現(xiàn)
1.1 概念
-
Eureka 是 Netflix 公司開源的一個服務(wù)注冊與發(fā)現(xiàn)的組件 。
-
Eureka 和其他 Netflix 公司的服務(wù)組件(例如負載均衡、熔斷器、網(wǎng)關(guān)等) 一起,被 Spring Cloud 社區(qū)整合為Spring-Cloud-Netflix 模塊。
-
Eureka 包含兩個組件:Eureka Server (注冊中心) 和 Eureka Client (服務(wù)提供者、服務(wù)消費者)
1.2 操作
1.3 搭建 Eureka Server 服務(wù)
(1)創(chuàng)建 eureka-server 模塊
(2) 引入 SpringCloud 和 euraka-server 相關(guān)依賴
(3)完成 Eureka Server 相關(guān)配置
(4)啟動該模塊
父工程 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.liming</groupId>
<artifactId>spring-cloud-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka-provider</module>
<module>eureka-consumer</module>
<module>eureka-server</module>
</modules>
<!--spring boot 環(huán)境 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/>
</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 版本-->
<spring-cloud.version>Hoxton.SR12</spring-cloud.version>
</properties>
<!--引入Spring Cloud 依賴-->
<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>
</project>
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">
<parent>
<artifactId>spring-cloud-parent</artifactId>
<groupId>com.liming</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
EurekaApp
package com.liming.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
// 啟用EurekaServer
@EnableEurekaServer
public class EurekaApp {
public static void main(String[] args) {
SpringApplication.run(EurekaApp.class,args);
}
}
application.yml
server:
port: 8761
# eureka 配置
# eureka 一共有4部分 配置
# 1. dashboard:eureka的web控制臺配置
# 2. server:eureka的服務(wù)端配置
# 3. client:eureka的客戶端配置
# 4. instance:eureka的實例配置
eureka:
instance:
hostname: localhost # 主機名
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # eureka服務(wù)端地址,將來客戶端使用該地址和eureka進行通信
register-with-eureka: false # 是否將自己的路徑 注冊到eureka上。eureka server 不需要的,eureka provider client 需要
fetch-registry: false # 是否需要從eureka中抓取路徑。eureka server 不需要的,eureka consumer client 需要
測試: 訪問 localhost:8761
1.4 改造 Provider 和 Consumer 成為 Eureka Client
① 引入 eureka-client 相關(guān)依賴
② 完成 eureka client 相關(guān)配置
③ 啟動 測試
Provider工程
pom
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
ProviderApp
@EnableEurekaClient
application.yml
server:
port: 8001
eureka:
instance:
hostname: localhost # 主機名
client:
service-url:
defaultZone: http://localhost:8761/eureka # eureka服務(wù)端地址,將來客戶端使用該地址和eureka進行通信
spring:
application:
name: eureka-provider # 設(shè)置當(dāng)前應(yīng)用的名稱。將來會在eureka中Application顯示。將來需要使用該名稱來獲取路徑
Consumer
pom
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
ConsumerApp
@EnableEurekaClient
@SpringBootApplication
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class,args);
}
}
application.yml
server:
port: 9000
eureka:
instance:
hostname: localhost # 主機名
client:
service-url:
defaultZone: http://localhost:8761/eureka # eureka服務(wù)端地址,將來客戶端使用該地址和eureka進行通信
spring:
application:
name: eureka-consumer # 設(shè)置當(dāng)前應(yīng)用的名稱。將來會在eureka中Application顯示。將來需要使用該名稱來獲取路徑
1.5 Consumer 服務(wù) 通過從 Eureka Server 中抓取 Provider 地址,完成遠程調(diào)用
Consumer
OrderController
package com.liming.consumer.controller;
import com.liming.consumer.domain.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/add/{id}")
public Goods add(@PathVariable("id") Integer id){
//String url="http://localhost:8000/goods/findById/"+id;
//Goods goods = restTemplate.getForObject(url, Goods.class);
//服務(wù)發(fā)現(xiàn)
List<ServiceInstance> instances = discoveryClient.getInstances("EUREKA-PROVIDER");
if(instances==null||instances.size()<=0){
return null;
}
//通過某個策略拿到一個實例
ServiceInstance serviceInstance = instances.get(0);
String host = serviceInstance.getHost();
int port = serviceInstance.getPort();
System.out.println(host);
System.out.println(port);
String url="http://"+host+":"+port+"/goods/findById/"+id;
Goods goods = restTemplate.getForObject(url, Goods.class);
return goods;
}
}
1.6 Euraka配置詳解
Eureka包含四個部分的配置
-
instance
:當(dāng)前Eureka Instance實例信息配置 -
client
:Eureka Client客戶端特性配置 -
server
:Eureka Server注冊中心特性配置 -
dashboard
:Eureka Server注冊中心儀表盤配置
1、實例信息配置
eureka:
instance:
hostname: localhost # 主機名
prefer-ip-address: # 是否將自己的ip注冊到eureka中,默認false 注冊 主機名
ip-address: # 設(shè)置當(dāng)前實例ip
instance-id: # 修改instance-id顯示
lease-renewal-interval-in-seconds: 30 # 每一次eureka client 向 eureka server發(fā)送心跳的時間間隔
lease-expiration-duration-in-seconds: 90 # 如果90秒內(nèi)eureka server沒有收到eureka client的心跳包,則剔除該服務(wù)
Eureka Instance的配置信息全部保存在org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean配置類里,實際上它是com.netflix.appinfo.EurekaInstanceConfig的實現(xiàn)類,替代了netflix的com.netflix.appinfo.CloudInstanceConfig的默認實現(xiàn)。
Eureka Instance的配置信息全部以
eureka.instance.xxx
的格式配置。
2、客戶端特性配置
eureka:
client:
service-url:
# eureka服務(wù)端地址,將來客戶端使用該地址和eureka進行通信
defaultZone:
register-with-eureka: # 是否將自己的路徑 注冊到eureka上。
fetch-registry: # 是否需要從eureka中抓取數(shù)據(jù)。
1、Eureka Client客戶端特性配置是對作為Eureka客戶端的特性配置,包括Eureka注冊中心,本身也是一個Eureka Client。
2、Eureka Client特性配置全部在org.springframework.cloud.netflix.eureka.EurekaClientConfigBean中,實際上它是com.netflix.discovery.EurekaClientConfig的實現(xiàn)類,替代了netxflix的默認實現(xiàn)。
3、Eureka Client客戶端特性配置全部以eureka.client.xxx
的格式配置。
3、注冊中心端配置
eureka:
server: #是否開啟自我保護機制,默認true
enable-self-preservation:
eviction-interval-timer-in-ms: 120 2月#清理間隔(單位毫秒,默認是60*1000)
instance:
lease-renewal-interval-in-seconds: 30 # 每一次eureka client 向 eureka server發(fā)送心跳的時間間隔
lease-expiration-duration-in-seconds: 90 # 如果90秒內(nèi)eureka server沒有收到eureka client的心跳包,則剔除該服務(wù)
1、Eureka Server注冊中心端的配置是對注冊中心的特性配置。Eureka Server的配置全部在org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean里,實際上它是com.netflix.eureka.EurekaServerConfig的實現(xiàn)類,替代了netflix的默認實現(xiàn)。
2、Eureka Server的配置全部以eureka.server.xxx
的格式進行配置。
4、儀表盤配置
eureka:
dashboard:
enabled: true # 是否啟用eureka web控制臺
path: / # 設(shè)置eureka web控制臺默認訪問路徑
注冊中心儀表盤的配置主要是控制注冊中心的可視化展示。以
eureka.dashboard.xxx
的格式配置。
1.7 改造yml配置文件
改造 provider
server:
port: 8001
eureka:
instance:
hostname: localhost # 主機名
prefer-ip-address: true # 將當(dāng)前實例的ip注冊到eureka server 中。默認是false 注冊主機名
ip-address: 127.0.0.1 # 設(shè)置當(dāng)前實例的ip
instance-id: ${eureka.instance.ip-address}:${spring.application.name}:${server.port} # 設(shè)置web控制臺顯示的 實例id
lease-renewal-interval-in-seconds: 3 # 每隔3 秒發(fā)一次心跳包
lease-expiration-duration-in-seconds: 9 # 如果9秒沒有發(fā)心跳包,服務(wù)器呀,你把我干掉吧~
client:
service-url:
defaultZone: http://localhost:8761/eureka # eureka服務(wù)端地址,將來客戶端使用該地址和eureka進行通信
spring:
application:
name: eureka-provider # 設(shè)置當(dāng)前應(yīng)用的名稱。將來會在eureka中Application顯示。將來需要使用該名稱來獲取路徑
consumer
server:
port: 9000
eureka:
instance:
hostname: localhost # 主機名
client:
service-url:
defaultZone: http://localhost:8761/eureka # eureka服務(wù)端地址,將來客戶端使用該地址和eureka進行通信
spring:
application:
name: eureka-consumer # 設(shè)置當(dāng)前應(yīng)用的名稱。將來會在eureka中Application顯示。將來需要使用該名稱來獲取路徑
server
server:
port: 8761
# eureka 配置
# eureka 一共有4部分 配置
# 1. dashboard:eureka的web控制臺配置
# 2. server:eureka的服務(wù)端配置
# 3. client:eureka的客戶端配置
# 4. instance:eureka的實例配置
eureka:
instance:
hostname: localhost # 主機名
client:
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka # eureka服務(wù)端地址,將來客戶端使用該地址和eureka進行通信
register-with-eureka: false # 是否將自己的路徑 注冊到eureka上。eureka server 不需要的,eureka provider client 需要
fetch-registry: false # 是否需要從eureka中抓取路徑。eureka server 不需要的,eureka consumer client 需要
server:
enable-self-preservation: false # 關(guān)閉自我保護機制
eviction-interval-timer-in-ms: 3000 # 檢查服務(wù)的時間間隔
eureka不更新了,所以淘汰了
二、Zookeeper服務(wù)注冊與發(fā)現(xiàn)
有的老項目以前是dubbo,升級到微服務(wù),使用zookeeper做注冊中心。
zookeeper是一個分布式協(xié)調(diào)工具,可以實現(xiàn)注冊中心功能。
實質(zhì):注冊中心換成Zookeeper
2.1 下載
Zookeeper下載地址
zoo.cfg
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=F:/apache-zookeeper-3.5.6-bin/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
啟動 bin目錄下
zkServer.cmd
zookeeper-provider
pom
<artifactId>zookeeper-provider</artifactId>
<dependencies>
<!--springcloud 整合 zookeeper 組件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--zk發(fā)現(xiàn)-->
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
yml
server:
port: 8004
spring:
application:
name: zookeeper-provider
cloud:
zookeeper:
connect-string: 127.0.0.1:2181 # zk地址
主啟動類
package com.liming.zk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient //開啟發(fā)現(xiàn)客戶端
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class,args);
}
}
業(yè)務(wù)邏輯代碼直接復(fù)制粘貼過來
zookeeper-consumer
pom
<artifactId>zookeeper-consumer</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--springcloud 整合 zookeeper 組件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<!--zk發(fā)現(xiàn)-->
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.6</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
yml
server:
port: 8005
spring:
application:
name: zookeeper-consumer
cloud:
zookeeper:
connect-string: 127.0.0.1:2181 # zk地址
啟動類
package com.liming.zk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class,args);
}
}
業(yè)務(wù)邏輯代碼直接復(fù)制粘貼過來
controller只改一個
List<ServiceInstance> instances = discoveryClient.getInstances("zookeeper-provider");
測試:http://localhost:8005/order/add/5
三、Consul服務(wù)注冊與發(fā)現(xiàn)
3.1 是什么
- Consul 是由 HashiCorp 基于 GoLanguage 語言開發(fā)的,支持多數(shù)據(jù)中心,分布式高可用的服務(wù)發(fā)布和注冊服務(wù)軟件。
- 用于實現(xiàn)分布式系統(tǒng)的服務(wù)發(fā)現(xiàn)與配置。
- 使用起來也較 為簡單。具有天然可移植性(支持Linux、windows和Mac OS X);安裝包僅包含一個可執(zhí)行文件,方便部署 。
- Consul官網(wǎng):https://www.consul.io/ Consul中文文檔:https://www.springcloud.cc/spring-cloud-consul.html
四、三個注冊中心的異同
組件 | 語言 | cap | 健康檢查 | 暴露接口 | cloud集成 |
---|---|---|---|---|---|
eureka | java | ap | 支持 | http | 已經(jīng)集成 |
zookeeper | java | cp | 支持 | tcp | 已經(jīng)集成 |
consul | go | cp | 支持 | http | 已經(jīng)集成 |
cap
-
c:consustency 強一致性
-
a:avalibility 可用性文章來源:http://www.zghlxwxcb.cn/news/detail-433982.html
-
p:partition tolerance 分區(qū)容忍性文章來源地址http://www.zghlxwxcb.cn/news/detail-433982.html
到了這里,關(guān)于Eureka、Zookeeper、Consul服務(wù)注冊與發(fā)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!