1.Eureka概述
1.1.什么是Eureka
Eureka是Netflix的一個子模塊,基于REST的服務,用于定位服務,以實作云端中間層服務發現和故障轉移,
只需要使用服務的識別符號,就可以訪問到服務,而不需要修改服務呼叫的組態檔,功能類似于dubbo的注冊中心,比如Zookeeper,
Eureka在設計時遵守的是AP原則,而Zookeeper 就是則是CP原則,C一致性(Consistency)、A可用性(Availability)、P磁區容錯性(Partition tolerance),
1.2.Eureka架構

Eureka CS架構圖
Eureka包含兩個組件:Eureka Server和Eureka Client
Eureka Server提供服務注冊服務:SpringCloud中的微服務啟動后會在Eureka Server中注冊,由Server存盤所有可用服務微服務節點的資訊到服務注冊表,可以在Eureka服務啟動后界面中看到,
Eureka Client是一個Java的客戶端,用于簡化Eureka Server的互動,客戶端同時也具備內置的、輪詢(round-robin)的負載均衡器,在應用啟動后,
會向Eureka Server發送心跳(默認30秒),如果Eureka Server在多個心跳周期沒有接收到某個節點的心跳,EurekaServer將會從服務注冊表中把這個服務節點移除(默認90秒),基于EurekaServer的“自我保護”模式,認定微服務是是啟動的健康狀態,不會注銷這個微服務,
1.3.Eureka三大角色

Eureka Server:提供服務注冊與發現
Server Provider:服務提供方將自身服務注冊到Eureka,從而使服務消費方能夠找到
Servcie Consumer:服務消費方從Eureka獲取注冊服務串列,從而能夠消費服務
2.Eureka Server服務注冊中心實作
2.1.pom.xml依賴
<dependency> <groupId>org.springframework.cloud</groupId> <!--server表明是server端--> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--熱部署插件--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
2.2.application.yml
server:
port: 7001
#Eureka配置
eureka:
instance:
hostname: localhost #Eureka服務端的實體名稱
client:
register-with-eureka: false #表示是否向Eureka注冊中心注冊自己,false表示不注冊自己
fetch-registry: false #如果為false,則表示自己為注冊中心
service-url: #服務注冊頁面地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
2.3.啟動類
注意:在啟動類上添加@EnableEurekaServer注解標識這個是Server服務注冊類
@SpringBootApplication @EnableEurekaServer //標識是Eureka服務的啟動類,可以接受消費者注冊進來 public class EurekaServer_7001 { public static void main(String[] args) { SpringApplication.run(EurekaServer_7001.class, args); } }
啟動SpringBoot專案,訪問服務注冊頁面地址http://localhost:7001/:

3.Service Provider服務提供方
3.1.pom.xml依賴
<!--微服務provider方引入eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <!--沒有帶sever表示就是client客戶端--> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--actuator:完善監控資訊--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--需要拿到物體類,引入api module--> <dependency> <groupId>com.fengye</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
3.2.application.yml
#spring的配置
spring:
application:
name: springcloud-provider-dept #默認注冊進eureka中的實體名稱(顯示大寫)
#eureka的配置,確定客戶端服務注冊到eureka服務串列內
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
3.3.啟動類
@SpringBootApplication @EnableEurekaClient //在服務啟動后自動注冊到eureka中 public class DeptServiceProvider { public static void main(String[] args) { SpringApplication.run(DeptServiceProvider.class, args); } }
啟動注冊中心Server與Provider Client類,發現在Eureka注冊中心已經注冊上了實體:

3.4.注冊中心實體顯示細節配置
①主機名稱:服務名稱修改

#eureka的配置,確定客戶端服務注冊到eureka服務串列內
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: springcloud-provider-dept-8001 #修改eureka上默認的服務描述資訊
②訪問資訊有IP地址資訊提示
![]()
instance:
instance-id: springcloud-provider-dept-8001 #修改eureka上默認的服務描述資訊
prefer-ip-address: true #訪問路徑可以顯示ip地址
③微服務info內容詳細資訊顯示
(1)provider service中pom.xml添加actuator配置:
<!--actuator:完善監控資訊--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
(2)父工程中修改pom.xml添加構建build資訊:
<build> <finalName>springcloud_helloworld</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <delimiters> <delimit>$</delimit> </delimiters> </configuration> </plugin> </plugins> </build>
(3)provider service中的application.yml中添加info配置:
#info配置
info:
app.name: fengye-springcloud
company.name: blog.fengye.com
build.artifactId: $project.artifactId$
build.version: $project.version$
之后點擊服務重定向info頁面不會顯示error頁面,會重定向獲取到info資料:

4.Service Consumer服務消費方
其實服務方和消費方在配置時候沒有任何區別,它們都屬于Eureka Client組件,只是涉及服務間的呼叫,所以就把被調方稱為提供方,呼叫方稱為消費方,就好比訂單微服務,
訂單服務肯定需要去調商品微服務介面,所以這個訂單微服務對于商品來講可以理解服務提供方,一個微服務即可以是服務方也同時是提供方,
4.1.pom.xml
<!--微服務provider方引入eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <!--沒有帶sever表示就是client客戶端--> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.4.6.RELEASE</version> </dependency> <!--actuator:完善監控資訊--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.fengye</groupId> <artifactId>springcloud-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
4.2.application.yml
#spring的配置
spring:
application:
name: springcloud-consumer-dept #默認注冊進eureka中的實體名稱(顯示大寫)
#eureka的配置,確定客戶端服務注冊到eureka服務串列內
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: springcloud-consumer-dept-8002 #修改eureka上默認的服務描述資訊
prefer-ip-address: true #訪問路徑可以顯示ip地址
4.3.啟動類
@SpringBootApplication @EnableEurekaClient //標識這是一個eureka的client客戶端 public class DeptServiceConsumer { public static void main(String[] args) { SpringApplication.run(DeptServiceConsumer.class, args); } }
啟動消費者微服務,訪問埠:http://localhost:7001/,發現消費中服務也已經注冊進來了:

4.4.服務發現
服務發現是指服務的提供者provider可以通過對外暴露一個服務的發現介面獲取Eureka Server中注冊的服務資訊,同時也可以讓consumer呼叫者進行介面訪問,獲得
介面請求的資料,
服務發現比較簡單,主要是微服務之間進行介面的呼叫、通信,主要配置如下:
(1)注入DiscoveryClient介面:
@RestController @RequestMapping("/dept") public class DeptController { @Autowired //對外暴露一個服務發現的介面 private DiscoveryClient client; //獲取注冊進來的微服務的一些訊息 @GetMapping("/discovery") public Object discovery(){ //獲取微服務串列的清單 List<String> services = client.getServices(); System.out.println("discovery=>:" + services); //得到一個具體的微服務資訊,通過具體的微服務Application id List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT"); for (ServiceInstance instance : instances) { System.out.println( instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri() + "\t" + instance.getServiceId() ); } return this.client; } }
(2)啟動類開啟服務發現注解:
@SpringBootApplication @EnableEurekaClient //在服務啟動后自動注冊到eureka中 @EnableDiscoveryClient //服務發現 public class DeptServiceProvider { public static void main(String[] args) { SpringApplication.run(DeptServiceProvider.class, args); } }
(3)服務消費者訪問介面請求地址:
@RestController public class DeptConsumerController { /** * 消費者:不應該有service實作層 * RestTemplate:提供多種便捷的訪問遠程http服務的方法,提供簡單的restful服務模板(使用簡單無腦粗暴) * 引數型別:(url, requestMap, ResponseBean.class)分別代表REST請求地址、請求引數、HTTP回應轉換被轉換成的物件型別 */ @Autowired private RestTemplate restTemplate; //服務端的請求地址,這里是本機localhost,也可以是其他任意的服務機ip地址 private static final String REST_URL_PREFIX = "http://localhost:8001"; //測驗@EnableDiscoveryClient,消費端可以呼叫服務發現 @RequestMapping("/consumer/dept/discovery") public Object discovery(){ return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class); } }
最終訪問8002埠呼叫服務提供者請求發現介面,訪問到資料:

示例代碼已上傳至Github地址:
https://github.com/devyf/SpringCloud_Study/tree/main/springcloud_hello
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/271828.html
標籤:Java
上一篇:MySQL視圖了解一下
下一篇:延遲佇列的常用的實作方式
