現在微服務架構越來越普及,目前接觸到主要有dubbo和springcloud
在springcloud中使用euerkaserver作為服務的注冊和發現的中心
簡單搭建EuerkaServer
1 在eclipse建立一個maven項目,在pom.xml檔案中添加相關依賴
<!-- 添加eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 添加springboot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
完整的pom.xml
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- Your own application should inherit from spring-boot-starter-parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<artifactId>EuerkaServer</artifactId>
<groupId>com.wxf.SpringCloud</groupId>
<name>Spring Boot Actuator Sample</name>
<description>Spring Boot Actuator Sample</description>
<version>0.0.1-SNAPSHOT</version>
<url>http://projects.spring.io/spring-boot/</url>
<organization>
<name>Pivotal Software, Inc.</name>
<url>http://www.spring.io</url>
</organization>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
-->
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2 添加啟動類
在啟動類加入注解@EnableEurekaServer
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableEurekaServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3 修改application.properties内容
spring.application.name=com.wxf.SpringCloud.EuerkaServer.actuator.Application
server.port=1111
#強制不注冊到注冊伺服器
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
#注冊中心位址
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
#驅逐下線的服務,間隔,5秒,預設是60,建議開發和測試環境配置
#org.springframework.cloud.netflix.eureka.server.EurekaServerConfigBean.evictionIntervalTimerInMs
eureka.server.evictionIntervalTimerInMs=5000
4 登陸管理頁面

搭建生産者
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">
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringCloud.Client</artifactId>
<groupId>com.wxf.SpringCloud</groupId>
<name>Spring Boot Actuator Sample</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!--端口監控依賴包-->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
啟動類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.properties
spring.application.name=Application-Provider
server.port=8181
#注冊中心位址
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
management.endpoints.enabled-by-default=false
接下來我們進入euerka管理頁面
搭建消費者
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">
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringCloud.Client</artifactId>
<groupId>com.wxf.SpringCloud</groupId>
<name>Spring Boot Actuator Sample</name>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--服務調用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
啟動類
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@EnableDiscoveryClient
@EnableFeignClients
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
控制層
controller
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class controller {
@Autowired
Service serviceAFeignClient;
@RequestMapping(value = "/*",method = RequestMethod.GET)
@ResponseBody
public String getHello(){
String hi = serviceAFeignClient.hi();
if (hi.isEmpty()){
hi="遠端調用失敗";
}
else {
hi="收到遠端資訊"+hi;
}
return hi;
}
}
service
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
@Component
@FeignClient(value = "Application-Provider") //這裡的name對應調用服務的spring.applicatoin.name
public interface Service {
@RequestMapping(value = "/hi")
String hi();
}
運作後在euerka注冊中心中可以到到服務
通路消費者,成功調用了提供者暴露的接口
上述内容已經完成了簡單的服務注冊、發現與調用。
注意事項:
注解:
-
@EnableEurekaServer
啟動服務注冊中心
-
@EnableDiscoveryClient
注冊服務至注冊中心(不單是euerka)
-
@EnableEurekaClient
注冊服務至注冊中心(隻能是euerka)
-
@EnableFeignClients
需要在啟動項上加上才可以調用FeignClient
-
@FeignClient(value = “Application-Provider”)
調用外部服務接口
依賴包:
<!-- euerka注冊中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- 外部服務調用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
疑問
服務是如何注冊和發現的?
點選這裡
Euerka Server
- 注冊:各個微服務啟動時,會通過Eureka Client向Eureka Server進行注冊自己的資訊(例如服務資訊和網絡資訊),Eureka Server會存儲該服務的資訊。
- 調用:服務消費者在調用服務時,本地Eureka Client沒有的情況下,會到Eureka Server拉取資訊。
-
維護:
Cancel 服務下線–Eureka用戶端在程式關閉時向Eureka伺服器發送取消請求
Eviction 服務剔除–當Eureka用戶端連續90秒沒有向Eureka伺服器發送服務續約,即心跳,Eureka伺服器會将該服務執行個體從服務注冊清單删除,即服務剔除。
renew 服務續約–Eureka Client會每隔30秒發送一次心跳來續約。 通過續約來告知Eureka Server該Eureka客戶仍然存在,沒有出現問題。
服務資訊如何同步?
為保證服務的高可用,一般會部署多個EuerkaServer,防止其中某個注冊中心出現當機影響服務的調用。
每個Eureka Server同時也是Eureka Client,多個Eureka Server之間通過P2P複制的方式完成服務系統資料庫的同步。同步時,被同步資訊不會同步出去。也就是說有3個Eureka Server,Server1有新的服務資訊時,同步到Server2後,Server2和Server3同步時,Server2不會把從Server1那裡同步到的資訊同步給Server3,隻能由Server1自己同步給Server3。
負載均衡?
點這裡
點這裡
在springcloud中,使用的是ribbon來解決負載均很的問題,而常見的負載均衡政策包括有:
IRule 是 ribbon負載均衡的父接口
RandomRule | 随機 | 随機選擇一個服務執行個體 |
RoundRobinRule | 輪詢-(預設) | 開啟一個計數器count,在while循環中周遊服務清單,取到服務後計數器會+1,連續10次取不到會報異常 |
WeightedResponseTimeRule | 權重 | 即根據響應時間來選擇服務的執行個體,時間短則權重大,預設情況下每隔30秒會計算一次各個服務執行個體的權重 |
RetryRule | 重試-輪詢 | 當輪詢至某個執行個體沒有響應時,會在逾時時間内進行重試,超出時間會傳回null |
BestAvailableRule | 優選政策(我自己取得名字) | 會過濾掉故障的執行個體,并找出并發請求數最小的一個,是以該政策的特性是選出最空閑的執行個體。 |
PredicateBasedRule | 過濾 | 先通過内部定義的一個過濾器過濾出一部分服務執行個體清單,然後再采用線性輪詢的方式從過濾出來的結果中選取一個服務執行個體。 |
ZoneAvoidanceRule | 組合過濾 | 以ZoneAvoidancePredicate為主過濾條件和以AvailabilityPredicate為次過濾條件組成,過濾後繼續采用線性輪詢的方式 |