目錄
Eureka基礎(chǔ)概念
概述
Eureka Serve
Eureka Client
@EnableEurekaServer
?@EnableEurekaClient
java代碼實戰(zhàn)
實戰(zhàn)架構(gòu)
父工程pom文件?
eureka-server服務(wù)
student-service服務(wù)
teacher-service服務(wù)
?測試
Eureka基礎(chǔ)概念
概述
? Eureka 又稱 服務(wù)注冊中心,全部服務(wù)都需要進行注冊才能使用,也是微服務(wù)架構(gòu)中必不可少的一個組件。
Spring Cloud 封裝了 Netflix 公司開發(fā)的 Eureka 模塊來實現(xiàn)服務(wù)治理 ? ? ? 在傳統(tǒng)的rpc遠程調(diào)用框架中,管理每個服務(wù)與服務(wù)之間依賴關(guān)系比較復(fù)雜,管理比較復(fù)雜,所以需要使用服務(wù)治理,管理服務(wù)于服務(wù)之間依賴關(guān)系,可以實現(xiàn)服務(wù)調(diào)用、負(fù)載均衡、容錯等,實現(xiàn)服務(wù)發(fā)現(xiàn)與注冊。 ?
?Eureka采用了CS的設(shè)計架構(gòu),Eureka Server 作為服務(wù)注冊功能的服務(wù)器,它是服務(wù)注冊中心。而系統(tǒng)中的其他微服務(wù),使用 Eureka的客戶端連接到 Eureka Server并維持心跳連接。這樣系統(tǒng)的維護人員就可以通過 Eureka Server 來監(jiān)控系統(tǒng)中各個微服務(wù)是否正常運行。
在服務(wù)注冊與發(fā)現(xiàn)中,有一個注冊中心。當(dāng)服務(wù)器啟動的時候,會把當(dāng)前自己服務(wù)器的信息 比如 服務(wù)地址通訊地址等以別名方式注冊到注冊中心上。另一方(消費者|服務(wù)提供者),以該別名的方式去注冊中心上獲取到實際的服務(wù)通訊地址,然后再實現(xiàn)本地RPC調(diào)用RPC遠程調(diào)用框架,核心設(shè)計思想:在于注冊中心,因為使用注冊中心管理每個服務(wù)與服務(wù)之間的一個依賴關(guān)系(服務(wù)治理概念)。在任何rpc遠程框架中,都會有一個注冊中心(存放服務(wù)地址相關(guān)信息(接口地址))
Eureka Serve
Eureka Server提供服務(wù)注冊服務(wù) 各個微服務(wù)節(jié)點通過配置啟動后,會在EurekaServer中進行注冊,這樣EurekaServer中的服務(wù)注冊表中將會存儲所有可用服務(wù)節(jié)點的信息,服務(wù)節(jié)點的信息可以在界面中直觀看到。?
注意:一個微服務(wù),既可以是服務(wù)提供者,又可以是服務(wù)消費者,因此eureka將服務(wù)注冊、服務(wù)發(fā)現(xiàn)等功能統(tǒng)一封裝到了eureka-client端 ??
Eureka Client
EurekaClient通過注冊中心進行訪問,是一個Java客戶端,用于簡化Eureka Server的交互,客戶端同時也具備一個內(nèi)置的、使用輪詢(round-robin)負(fù)載算法的負(fù)載均衡器。在應(yīng)用啟動后,將會向Eureka Server發(fā)送心跳(默認(rèn)周期為30秒)。
如果Eureka Server在多個心跳周期內(nèi)沒有接收到某個節(jié)點的心跳,EurekaServer將會從服務(wù)注冊表中把這個服務(wù)節(jié)點移除(默認(rèn)90秒)?
@EnableEurekaServer
在項目啟動類上使用@EnableEurekaServer,可以將項目作為SpringCloud中的注冊中心。
在SpringCloud中當(dāng)你需要使用Eureka
Eureka注冊中心的時候你在配置Eureka的服務(wù)端的時候需要在啟動類上添加@EnableEurekaServer注解
?@EnableEurekaClient
在項目啟動類上添加@EnableEurekaClient,本服務(wù)啟動后會自動注冊進eureka服務(wù)中。
沒使用注解的時候,他們還是經(jīng)過一個注冊的方法,(注冊到服務(wù)中心) 他們應(yīng)該是根據(jù)配置文件來的。
java代碼實戰(zhàn)
實戰(zhàn)架構(gòu)
建立三個springboot工程,其中一個作為Eureka Server注冊中心,其他倆個為倆個不同的端口的service端,客戶端向8082端口的studentservice發(fā)送一個請求(/getInfo)以后,8082端口的studentservice需要往teacherservice發(fā)送一個請求(/getTeacher)返回數(shù)據(jù)。
父工程pom文件?
<groupId>org.example</groupId>
<artifactId>eurek-test</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eurek-serve</module>
<module>student-service</module>
<module>teacher-service</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.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.version>Hoxton.SR10</spring-cloud.version>
<mysql.version>5.1.47</mysql.version>
<mybatis.version>2.1.1</mybatis.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- springCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--nacos的管理依賴-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
eureka-server服務(wù)
pom文件
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--eureka服務(wù)端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
Application 程序?
@SpringBootApplication
@EnableEurekaServer
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
yml配置文件?
server:
port: 8001 # 服務(wù)端口
spring:
application:
name: eurekaserver # eureka的服務(wù)名稱
eureka:
client:
service-url: # eureka的地址信息
defaultZone: http://127.0.0.1:8081/eureka
瀏覽器訪問http://127.0.0.1:8001/?可以看到web端
student-service服務(wù)
pom文件
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Application 程序?
@SpringBootApplication
@EnableEurekaClient
public class StudentApplication {
public static void main(String[] args) {
SpringApplication.run(StudentApplication.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
RestTemplate是Spring提供的用于訪問Rest服務(wù)的客戶端,RestTemplate提供了多種便捷訪問遠程Http服務(wù)的方法,能夠大大提高客戶端的編寫效率。?
使用restTemplate訪問restful接口非常的簡單粗暴無腦。
(url, requestMap, ResponseBean.class)這三個參數(shù)分別代表?
REST請求地址、請求參數(shù)、HTTP響應(yīng)轉(zhuǎn)換被轉(zhuǎn)換成的對象類型。
yml配置文件?
server:
port: 8082
spring:
application:
name: studentservice
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8081/eureka
?Teacher類
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher implements Serializable {
private String name;
private String sex;
}
?StudentController
@RestController
public class StudentController implements Serializable {
@Resource
RestTemplate restTemplate;
@GetMapping("/getInfo")
public Teacher getInfo(){
String url="http://teacherservice/getTeacher";
Teacher teacher = restTemplate.getForObject(url, Teacher.class);
return teacher;
}
}
teacher-service服務(wù)
?pom文件
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Application 程序?
@SpringBootApplication
public class TeacherApplication {
public static void main(String[] args) {
SpringApplication.run(TeacherApplication.class,args);
}
}
yml配置文件?
server:
port: 8083
spring:
application:
name: teacherservice
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8081/eureka
?Teacher類
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher implements Serializable {
private String name;
private String sex;
}
?StudentController
@RestController
public class TeacherController {
@GetMapping("/getTeacher")
public Teacher getInfo(){
return new Teacher("張三","男");
}
}
?測試
瀏覽器訪問:http://localhost:8082/getInfo文章來源:http://www.zghlxwxcb.cn/news/detail-792274.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-792274.html
到了這里,關(guān)于SpringCloud之Eureka注冊中心解讀的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!