一、Eureka基本概念
Eureka是Netflix開發(fā)的服務(wù)發(fā)現(xiàn)框架遮咖,SpringCloud將它集成在自己的子項(xiàng)目 spring-cloud-netflix中滩字,實(shí)現(xiàn)SpringCloud的服務(wù)發(fā)現(xiàn)功能。Eureka包含兩個(gè)組件: Eureka Serve和Eureka Client。
Eureka Server提供服務(wù)注冊(cè)服務(wù)麦箍,各個(gè)節(jié)點(diǎn)啟動(dòng)后漓藕,會(huì)在Eureka Server中進(jìn)行注冊(cè),這樣EurekaServer中的服務(wù)注冊(cè)表中將會(huì)存儲(chǔ)所有可用服務(wù)節(jié)點(diǎn)的信息挟裂,服務(wù)節(jié)點(diǎn)的信息可以在界面中直觀的看到享钞。
Eureka Client是一個(gè)java客戶端,用于簡(jiǎn)化與Eureka Server的交互诀蓉,客戶端同時(shí)也包含一個(gè)內(nèi)置的嫩与、使用輪詢(round-robin)負(fù)載算法的負(fù)載均衡器。在應(yīng)用啟動(dòng)后交排,將會(huì) 向Eureka Server發(fā)送心跳,默認(rèn)周期為30秒划滋,如果Eureka Server在多個(gè)心跳周期內(nèi)沒有 接收到某個(gè)節(jié)點(diǎn)的心跳,Eureka Server將會(huì)從服務(wù)注冊(cè)表中把這個(gè)服務(wù)節(jié)點(diǎn)移除(默認(rèn)90 秒)埃篓。
Eureka Server之間通過復(fù)制的方式完成數(shù)據(jù)的同步处坪,Eureka還提供了客戶端緩存機(jī)制,即使所有的Eureka Server都掛掉架专,客戶端依然可以利用緩存中的信息消費(fèi)其他服務(wù)的API同窘。綜上,Eureka通過心跳檢查部脚、客戶端緩存等機(jī)制想邦,確保了系統(tǒng)的高可用性、靈活性和可伸縮性委刘。
二丧没、搭建模塊
1、父工程中定義Spring Cloud的版本
<dependencyManagement>
<dependencies>
<!--Spring Cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2锡移、單獨(dú)創(chuàng)建一個(gè)Eureka服務(wù)模塊ek-service
3呕童、配置pom文件
<dependencies>
<!--注冊(cè)中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
將該模塊注冊(cè)為Eureka的server
4、配置application.properties
#服務(wù)端口
server.port=8002
#是否將自己注冊(cè)到Eureka服務(wù)器中淆珊,本身是服務(wù)器夺饲,無需注冊(cè)
eureka.client.register-with-eureka=false
#是否從Eureka中獲取注冊(cè)信息
eureka.client.fetch-registry=false
#Eureka客戶端與Eureka服務(wù)端進(jìn)行通信的地址
eureka.client.service-url.defaultZone=http://127.0.0.1:${server.port}/eureka/
5、創(chuàng)建啟動(dòng)類
在啟動(dòng)類上添加注解@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EkServerApplication {
public static void main(String[] args) {
SpringApplication.run(EkServerApplication.class);
}
}
三施符、配置客戶端
1往声、客戶端微服務(wù)配置pom
<!--服務(wù)注冊(cè)-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、客戶端微服務(wù)配置application.properties
#指定注冊(cè)中心地址
eureka.client.service-url.defaultZone=http://127.0.0.1:8002/eureka/
#eureka服務(wù)器上獲取的是服務(wù)器的ip地址戳吝,否則是主機(jī)名
eureka.instance.prefer-ip-address=true
3浩销、添加Eureka客戶端注解
在客戶端微服務(wù)啟動(dòng)類中添加注解@EnableEurekaClient
四、Feign基本概念
- Feign是Netflix開發(fā)的聲明式骨坑、模板化的HTTP客戶端撼嗓, Feign可以幫助我們更快捷、優(yōu)雅地調(diào)用HTTP API欢唾。
- Feign支持多種注解且警,例如Feign自帶的注解或者JAX-RS注解等。
- Spring Cloud對(duì)Feign進(jìn)行了增強(qiáng)礁遣,使Feign支持了Spring MVC注解斑芜,并整合了Ribbon和Eureka,從而讓Feign的使用更加方便祟霍。
- Spring Cloud Feign是基于Netflix feign實(shí)現(xiàn)杏头,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供這兩者的強(qiáng)大功能外沸呐,還提供了一種聲明式的Web服務(wù)客戶端定義的方式醇王。
- Spring Cloud Feign幫助我們定義和實(shí)現(xiàn)依賴服務(wù)接口的定義。在Spring Cloud feign的實(shí)現(xiàn)下崭添,只需要?jiǎng)?chuàng)建一個(gè)接口并用注解方式配置它寓娩,即可完成服務(wù)提供方的接口綁定,簡(jiǎn)化了在使用Spring Cloud Ribbon時(shí)自行封裝服務(wù)調(diào)用客戶端的開發(fā)量呼渣。
五棘伴、實(shí)現(xiàn)服務(wù)調(diào)用
1、在微服務(wù)中的調(diào)用端與被調(diào)用端添加依賴
<!--服務(wù)調(diào)用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2屁置、在調(diào)用端與被調(diào)用端的啟動(dòng)類上添加注解@EnableFeignClients
若使用nacos焊夸,則均需添加配置
# nacos服務(wù)地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
3、在調(diào)用端創(chuàng)建用于調(diào)用服務(wù)的包和接口
創(chuàng)建client包
- @FeignClient注解用于指定從哪個(gè)服務(wù)中調(diào)用功能 蓝角,值為被調(diào)用微服務(wù)的名字阱穗。
- @GetMapping注解用于對(duì)被調(diào)用的微服務(wù)進(jìn)行地址映射。
- @PathVariable注解一定要指定參數(shù)名稱使鹅,否則出錯(cuò)
- @Component注解把該類實(shí)例化到spring容器中
@Component
@FeignClient("Demo-service")
public interface DemoClient {
@GetMapping("/demoservice/demo/{demoId}") //這里需要寫該方法的全路徑
public Result DemoMethod(@PathVariable("demoId") String demoId);
}
這段代碼的作用就是調(diào)用端微服務(wù)調(diào)用了名為Demo-service微服務(wù)的"/demoservice/demo/{demoId}"路徑的方法颇象。
在需要使用該方法的服務(wù)中注入DemoClient
@Autowired
private DemoClient demoClient;
現(xiàn)在即可使用demoClient.DemoMethod();
六并徘、Nacos注冊(cè)
1遣钳、基本概念
Nacos 是阿里巴巴推出來的一個(gè)新開源項(xiàng)目,是一個(gè)更易于構(gòu)建云原生應(yīng)用的動(dòng)態(tài)服務(wù)發(fā)現(xiàn)麦乞、配置管理和服務(wù)管理平臺(tái)蕴茴。Nacos 致力于幫助您發(fā)現(xiàn)、配置和管理微服務(wù)姐直。Nacos 提供了一組簡(jiǎn)單易用的特性集倦淀,幫助您快速實(shí)現(xiàn)動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、服務(wù)配置声畏、服務(wù)元數(shù)據(jù)及流量管理撞叽。Nacos 幫助您更敏捷和容易地構(gòu)建叉存、交付和管理微服務(wù)平臺(tái)。 Nacos 是構(gòu)建以“服務(wù)”為中心的現(xiàn)代應(yīng)用架構(gòu) (例如微服務(wù)范式琅束、云原生范式) 的服務(wù)基礎(chǔ)設(shè)施诈嘿。
2、為什么用Nacos
Eureka在2.0遇到性能瓶頸糠雨,停止維護(hù)才睹。相對(duì)于 Spring Cloud Eureka 來說,Nacos 更強(qiáng)大甘邀。Nacos = Spring Cloud Eureka + Spring Cloud Config琅攘。
3、Nacos下載和安裝
(1)下載地址和版本
下載地址:https://github.com/alibaba/nacos/releases
下載版本:nacos-server-1.1.4.tar.gz或nacos-server-1.1.4.zip松邪,解壓任意目錄即可
(2)啟動(dòng)nacos服務(wù)
- Linux/Unix/Mac
啟動(dòng)命令(standalone代表著單機(jī)模式運(yùn)行坞琴,非集群模式)
啟動(dòng)命令:sh startup.sh -m standalone
- Windows
啟動(dòng)命令:cmd startup.cmd 或者雙擊startup.cmd運(yùn)行文件。
訪問:http://localhost:8848/nacos
用戶名密碼:nacos/nacos
4逗抑、服務(wù)注冊(cè)
(1)置济、在service模塊配置pom
配置Nacos客戶端的pom依賴
<!--服務(wù)注冊(cè)-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
(2)、添加服務(wù)配置信息
配置application.properties锋八,在客戶端微服務(wù)中添加注冊(cè)Nacos服務(wù)的配置信息
# nacos服務(wù)地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
(3)浙于、添加Nacos客戶端注解
在客戶端微服務(wù)啟動(dòng)類中添加注解
@EnableDiscoveryClient