摘自官方說明:
Spring Cloud Netflix provides Netflix OSS integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms. With a few simple annotations you can quickly enable and configure the common patterns inside your application and build large distributed systems with battle-tested Netflix components. The patterns provided include Service Discovery (Eureka), Circuit Breaker (Hystrix), Intelligent Routing (Zuul) and Client Side Load Balancing (Ribbon)..
通過上述可以了解到:
Eureka:服務(wù)發(fā)現(xiàn)
Hystrix:斷路器
Zuul:智能路由
Ribbon:客戶端負(fù)載均衡
先圖話后
Eureka:實(shí)際上在整個(gè)過程中維護(hù)者每個(gè)服務(wù)的生命周期。每一個(gè)服務(wù)都要被注冊到Eureka服務(wù)器上,,這里被注冊到Eureka的服務(wù)又稱為Client,。Eureka通過心跳來確定服務(wù)是否正常,。Eureka只做請求轉(zhuǎn)發(fā),。同時(shí)Eureka是支持集群的呦?。,?!
Zuul:類似于網(wǎng)關(guān),反向代理,。為外部請求提供統(tǒng)一入口,。
Ribbon/Feign:可以理解為調(diào)用服務(wù)的客戶端。
Hystrix:斷路器,,服務(wù)調(diào)用通常是深層的,,一個(gè)底層服務(wù)通常為多個(gè)上層服務(wù)提供服務(wù),那么如果底層服務(wù)失敗則會(huì)造成大面積失敗,,Hystrix就是就調(diào)用失敗后觸發(fā)定義好的處理方法,,從而更友好的解決出錯(cuò)。也是微服務(wù)的容錯(cuò)機(jī)制,。
多說不如看代碼
下面我們將搭建一個(gè)單Eureka服務(wù)器的案例,,我們將會(huì)注冊兩個(gè)微服務(wù),并配置Zuul,。
目標(biāo):
1.模擬外部請求,,訪問暴漏端口(Eureka監(jiān)聽端口)訪問其中一個(gè)內(nèi)部服務(wù)。
2.演示服務(wù)間如何通過Feign進(jìn)行調(diào)用,。
綜述:
我們要寫的包括:
一個(gè)Eureka服務(wù)器
兩個(gè)微服務(wù)
抽取出公共的POM部分,,讓我們專注重點(diǎn)
<project xmlns="http://maven./POM/4.0.0" xmlns:xsi="http://www./2001/XMLSchema-instance"
xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xbz.eureka.demo</groupId>
<artifactId>demo-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>1.7</java.version>
<file.encoding>UTF-8</file.encoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix</artifactId>
<version>1.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- 引入的依賴 -->
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${file.encoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo./libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
Eureka單例服務(wù)器(即不將自身作為一個(gè)Client)
我們只需要三個(gè)步驟就可以啟動(dòng)服務(wù)器
1.POM引入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
2.搞個(gè)Main啟動(dòng)類
@EnableEurekaServer
@EnableZuulProxy
package com.xbz.eureka.demo.server;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableEurekaServer
@EnableZuulProxy
public class EnurekaServer {
public static void main(String[] args) {
new SpringApplicationBuilder(EnurekaServer.class).web(true).run(args);
}
}
3.重點(diǎn):application.yml
#單例模式啟動(dòng)Eureka Server
server:
port: 8761 #啟動(dòng)端口
eureka:
client:
registerWithEureka: false #false:不作為一個(gè)客戶端注冊到注冊中心
fetchRegistry: false #為true時(shí),可以啟動(dòng),,但報(bào)異常:Cannot execute request on any known server
zuul:
prefix: /techouse #為zuul設(shè)置一個(gè)公共的前綴
#ignoredServices: '*'
routes:
cloud-client: #隨便定義,,當(dāng)不存在serviceId時(shí),默認(rèn)該值為serviceId(就是注冊服務(wù)的名稱,,屬性spring.application.name)
path: /usersystem/** #匹配/techouse/usersystem/** 均路由到cloud-client
serviceId: cloud-client #指定路由到的serviceId
ribbon:
eureka:
enabled: false #配置zuul路由時(shí)用將此屬性設(shè)置為false
cloud-client:
ribbon:
listOfServers: 127.0.0.1:8800 #為cloud-client服務(wù)指定一組服務(wù)地址,,應(yīng)該是用于負(fù)載均衡
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
兩個(gè)微服務(wù)
服務(wù)cluod-client
客戶端pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
這個(gè)服務(wù)我們在上面的application.yml中配置了,等下我們就可以用Eureka服務(wù)器地址訪問此服務(wù),。
該服務(wù)包含一個(gè)HellController,,和ServerApplication(啟動(dòng)類)
HelloController.java
package com.xbz.eureka.demo.client.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@RestController
public class HelloController {
@RequestMapping("/hello/{fallback}")
@HystrixCommand(fallbackMethod="helloFallbackMethod")/*調(diào)用方式失敗后調(diào)用helloFallbackMethod*/
public String hello(@PathVariable("fallback") String fallback){
if("1".equals(fallback)){
throw new RuntimeException("...");
}
return "hello";
}
public String helloFallbackMethod(String fallback){
return "fallback 參數(shù)值為:"+fallback;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
ServerApplication.java
@EnableEurekaClient,確保該應(yīng)用注冊到Eureka服務(wù)器
@EnableHystrix 斷路器生效
package com.xbz.eureka.demo.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
public class ServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceApplication.class, args);
}
}
重要的application.xml
server:
port: 8800
spring:
application:
name: cloud-client #為你的應(yīng)用起個(gè)名字,,該名字將注冊到eureka注冊中心
eureka:
instance:
statusPageUrlPath: ${management.context-path}/info
healthCheckUrlPath: ${management.context-path}/health
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #去哪里注冊,,eureka服務(wù)地址
服務(wù)cluod-client-consumer
這個(gè)服務(wù)要調(diào)用cloud-client服務(wù)中的/hello/{fallback}方法
使用@Feign(“服務(wù)名稱”),接口中隨便定義方法,,然后@RequestMapping(“/hello/{fallback}”)即可,內(nèi)部調(diào)用就是如此簡單,。
涉及HelloService(演示如何使用@Feign)調(diào)用服務(wù),。HelloController演示調(diào)用服務(wù)的輸出結(jié)果,以及ConsumerApplication啟動(dòng)類,。
HelloService.java
package com.xbz.eureka.demo.consumer;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient("cloud-client")
public interface HelloService {
@RequestMapping("/hello/{fallback}")
public String hello(@PathVariable("fallback") String fallback);
}
HelloController.java
package com.xbz.eureka.demo.consumer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
private HelloService helloServcie;
@RequestMapping("/test/{fallback}")
public String hello(@PathVariable("fallback") String fallback){
String res=helloServcie.hello(fallback);
return "調(diào)用服務(wù)結(jié)果為"+res;
}
}
啟動(dòng)類ConsumerApplication.java
@EnableEurekaClient注冊到Eureka
@EnableFeignClients 內(nèi)部使用Feign調(diào)用服務(wù)
@EnableZuulProxy 必須要有這個(gè)注解
package com.xbz.eureka.demo.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableEurekaClient
@EnableZuulProxy
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
重要application.xml
server:
port: 8080
spring:
application:
name: cloud-consumer #為你的應(yīng)用起個(gè)名字,,該名字將注冊到eureka注冊中心
eureka:
instance:
statusPageUrlPath: ${management.context-path}/info
healthCheckUrlPath: ${management.context-path}/health
client:
serviceUrl:
defaultZone: http://hadoopMaster:8761/eureka/ #去哪里注冊,eureka服務(wù)地址
至此,,我們的服務(wù)就搭建起來了?。?!
按順序啟動(dòng)三個(gè)類:
EurekaServer
ServiceApplication
ConsumerApplication
正常情況下,,訪問http://127.0.0.1:8761將看到如下界面
圖中展示了我們啟動(dòng)的兩個(gè)服務(wù)。
1.訪問http://127.0.0.1:8080/test/1測試服務(wù)內(nèi)部調(diào)用,,且失敗會(huì)調(diào)用fallback方法返回值,。
2.訪問http://127.0.0.1:8080/test/2測試服務(wù)內(nèi)部調(diào)用,則正常調(diào)用cloud-client的服務(wù),。
3.我們可以使用統(tǒng)一入口,,調(diào)用cloud-client服務(wù):
訪問的url:http://127.0.0.1:8761/techouse/usersystem/hello/2
以上便是一個(gè)簡單的微服務(wù)搭建嘍!??!
|