一、eureka
1.服務端
pom檔案
添加eureka-server
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
yaml檔案
# eureka叢集互相注冊
server:
port: 7002
#eureka:
# instance:
# hostname: eureka7002.com
# client:
# register-with-eureka: false
# fetch-registry: false
# com.fox.springcloud.service-url:
# defaultzone: http://eureka7001.com:7001/eureka/
eureka:
instance:
hostname: eureka7002.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 20000
啟動類
添加@EnableEurekaServer注解
2.用戶端(服務注冊+restTemplate)
pom檔案
添加eureka-client maven坐标
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
yaml檔案
eureka:
client:
register-with-eureka: true
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
fetch-registry: true
instance:
instance-id: payment8001
prefer-ip-address: true
lease-renewal-interval-in-seconds: 10
lease-expiration-duration-in-seconds: 30
啟動類
添加@EnableEurekaClient、@EnableDiscoveryClient注解
備注:@EnableEurekaClient隻适用于使用Eureka作為注冊中心的場景,@EnableDiscoveryClient可以适用于其他注冊中心的場景比如nacos等。
調用遠端服務
使用服務名代替服務提供者的ip端口調用
擷取注冊中心的服務
@Resource
private DiscoveryClient discoveryClient;
可以通過discoveryClient擷取注冊中心的服務和對應的instances
3.用戶端(openfeign)
pom檔案
添加eureka-client和openfeign
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
yaml檔案
eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
feign:
client:
config:
default:
ReadTimeout: 5000
ConnectTimeout: 5000
啟動類
添加@EnableFeignClients注解
調用遠端服務
PaymentFeignService中的服務名和接口路徑要和服務提供者一緻,項目中注入PaymentFeignService即可調用
@Component
@FeignClient("PAYMENT-SERVICE")
public interface PaymentFeignService {
@GetMapping("/payment/lb")
public String getPaymentLB();
@GetMapping("/payment/timeout")
public String getTimeout();
}
二、zookeeper
1.zookeeper服務端安裝
下載下傳zookeeper
wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.5.9/apache-zookeeper-3.5.9-bin.tar.gz
zookeeper安裝後要注意檢查防火牆是否關閉
檢視防火牆狀态
systemctl status firewalld.service
關閉防火牆
systemctl stop firewalld.service
java項目連接配接zookeeper時要注意和伺服器版本一緻。
進入bin目錄下,啟動zookeeper
./zkServer.sh start
連接配接zookeeper
./zkCli.sh
2.用戶端(服務注冊調用)
pom檔案
<!--SpringBoot整合Zookeeper用戶端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<exclusions>
<!--先排除自帶的zookeeper3.5.3-->
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.5.9版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.5.9</version>
</dependency>
yaml檔案
spring:
application:
# 服務别名---注冊zookeeper到注冊中心的名稱
name: cloud-provider-payment
cloud:
zookeeper:
# 預設localhost:2181
connect-string: 192.168.137.200:2181
啟動類
添加@EnableDiscoveryClient注解
調用遠端服務
通過服務名代替ip端口
zookeeper節點類型
1、PERSISTENT--持久化目錄節點
用戶端與zookeeper斷開連接配接後,該節點依舊存在
2、PERSISTENT_SEQUENTIAL-持久化順序編号目錄節點
用戶端與zookeeper斷開連接配接後,該節點依舊存在,隻是Zookeeper給該節點名稱進行順序編号
3、EPHEMERAL-臨時目錄節點
用戶端與zookeeper斷開連接配接後,該節點被删除
4、EPHEMERAL_SEQUENTIAL-臨時順序編号目錄節點
用戶端與zookeeper斷開連接配接後,該節點被删除,隻是Zookeeper給該節點名稱進行順序編号
三、consul
1.consul服務端安裝
consul下載下傳安裝官網寫的非常詳細https://www.consul.io/downloads
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install consul
檢視是否安裝成功
consul -version
啟動consul
window啟動
consul agent dev
centos啟動(0.0.0.0允許遠端通路)
consul agent -dev -client 0.0.0.0 -ui &
檢視程序是否啟動成功
netstat -anp|grep 8500
檢視Consul叢集的成員
consul members
2.用戶端(服務注冊調用)
pom檔案
添加consul-discovery
<!--SpringCloud consul-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
yaml檔案
spring:
application:
# 服務别名---注冊consul到注冊中心的名稱
name: cloud-provider-payment
cloud:
consul:
host: 192.168.137.200
port: 8500
discovery:
service-name: ${spring.application.name}
啟動類
添加@EnableDiscoveryClient注解
調用遠端服務
通過服務名代替ip端口
3.使用過程中遇到的問題
問題一、注冊服務提供者連接配接不上consul
使用telnet ip端口報錯
telnet: connect to address 192.168.137.200: No route to host
檢視防火牆狀态
firewall-cmd --state
發現防火牆是running狀态,關閉防火牆,再telnet成功
service firewalld stop
問題二、consul中服務健康狀态為unhealth
頁面顯示叉
點進去
通過output資訊判斷可能是使用了服務運作時的主機名,檢視主機名确實是DESKTOP-JOKG37G
127.0.0.1 DESKTOP-JOKG37G
原因是springcloud服務注冊到consul的是主機名,consul所在的伺服器不能識别該主機名導緻
解決方法:
一、在consul伺服器/etc/hosts檔案中配置主機名映射到Springcloud服務ip
二、指定注冊到consul的ip位址spring.cloud.consul.discovery.hostname
spring:
application:
name: cloud-consumer-order
cloud:
consul:
host: 192.168.137.200
port: 8500
discovery:
hostname: 192.168.137.166
service-name: ${spring.application.name}