服務(wù)治理所解決的問題
在傳統(tǒng)rpc遠(yuǎn)程調(diào)用中,服務(wù)與服務(wù)依賴關(guān)系,管理比較復(fù)雜,所以需要使用服務(wù)治理,管理服務(wù)與服務(wù)之間依賴關(guān)系,可以實(shí)現(xiàn)服務(wù)調(diào)用、負(fù)載均衡、容錯等,實(shí)現(xiàn)服務(wù)發(fā)現(xiàn)與注冊。
在服務(wù)注冊與發(fā)現(xiàn)中,有一個注冊中心,當(dāng)服務(wù)器啟動的時候,會把當(dāng)前自己服務(wù)器的信息 比如 服務(wù)地址通訊地址等以別名方式注冊到注冊中心上。
另一方(消費(fèi)者|服務(wù)提供者),以該別名的方式去注冊中心上獲取到實(shí)際的服務(wù)通訊地址,讓后在實(shí)現(xiàn)本地rpc調(diào)用遠(yuǎn)程。
服務(wù)治理所使用的技術(shù)手段
1、通過注冊中心實(shí)現(xiàn)服務(wù)的注冊與發(fā)現(xiàn) ,需要一個注冊中心,常用的有zookeeper、Eureka、Consul 等
2、通過本地RPC接口調(diào)用需要的服務(wù),常用Feign客戶端、RestTemplate等方式
服務(wù)發(fā)現(xiàn)與注冊的簡單技術(shù)實(shí)現(xiàn)
Eureka實(shí)現(xiàn)服務(wù)的注冊與發(fā)現(xiàn)(springcloud整合Eureka)
pom.xml 依賴項
<?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">
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- springboot整合eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- springboot整合fegnin客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
application.yml中配置
### 服務(wù)名稱(服務(wù)注冊到eureka名稱)
spring:
application:
name: app-itmayiedu-order
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka
###因為該應(yīng)用為注冊中心,不會注冊自己,集群為true
register-with-eureka: false
###是否需要從eureka上獲取注冊信息,集群為true
fetch-registry: false
在啟動類Application上加入注解 @EnableEurekaClient
@SpringBootApplication
@EnableEurekaClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
然后在使用到服務(wù)是就可以使用服務(wù)器名稱(application.yml中配置的應(yīng)用名稱代替服務(wù)地址進(jìn)行服務(wù)調(diào)用)
zookeeper實(shí)現(xiàn)服務(wù)的注冊與發(fā)現(xiàn)(springcloud整合zookeeper)
zookeeper的整合跟Eureka非常類似
pom.xml 依賴項
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
</dependency>
<!--zk工具包 保持客戶端zookeeper版本與服務(wù)器端一致-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
注意zookeeper指定版本是為了服務(wù)端和客戶端版本保持一致,防止兼容性的問題出現(xiàn)
application.yml中配置
spring:
application:
name: app-order-service
cloud:
zookeeper:
discovery:
register: true
enabled: true
instance-id: 1
root: /demo
connect-string: 39.104.124.245:2181
在啟動類Application上加入注解 @EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
然后在使用到服務(wù)是就可以使用服務(wù)器名稱(application.yml中配置的應(yīng)用名稱代替服務(wù)地址進(jìn)行服務(wù)調(diào)用)