這是一個簡單的springcloud工程瞭恰,基于eureka+feign+hystrix
github地址:https://github.com/weiess/springCloud.git
簡單的解釋下:
eureka是服務(wù)注冊與發(fā)現(xiàn)的
feign是用來調(diào)用服務(wù)的,其實還有restTemplate,原理和feign一樣,都是通過httpclient去掉用服務(wù)
hystrix斷路器其實就是幫助處理異常的晓铆,意思是坏快,調(diào)用服務(wù)接口的時候草巡,服務(wù)掛了,可以用斷路器解決
因為cloud項目比較大眠寿,在實際項目中會有很多子項目躬翁,所以筆者這邊也用了父子maven搭建了一個簡單的springcloud項目,先看下結(jié)構(gòu):
com.yzl.cloud是一個父工程澜公,其主要作用就是管理jar姆另,留下pom.xml
即可喇肋,但是pom中要加入<modules>標(biāo)簽管理子項目坟乾,其他的子項目都在父工程里,pom中要加入父項目坐標(biāo)也就是 <parent>標(biāo)簽蝶防,如果大家對于父子工程不太懂的可以自行百度甚侣,筆者這里不多說。
給大家看下springcloud相關(guān)依賴:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yzl.cloud</groupId>
<artifactId>com.yzl.cloud</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>com.yzl.Eureka</module>
<module>com.yzl.Feign</module>
<module>com.yzl.Client</module>
</modules>
<name>com.yzl.cloud</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.0.4.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.1.7.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
springcloud本質(zhì)還是基于springboot间学,所以spring-boot-starter-parent我們必須要引用殷费。
Eureka
我們先搭建服務(wù)注冊中心,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>
<parent>
<groupId>com.yzl.cloud</groupId>
<artifactId>com.yzl.cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.yzl.Eureka</groupId>
<artifactId>com.yzl.Eureka</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>com.yzl.Eureka Maven Webapp</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>com.yzl.Eureka</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
搭建Eureka主要就一個jar:spring-cloud-starter-netflix-eureka-server
我們只需要在yml配置幾個參數(shù):
server:
port: 8080
spring:
application:
name: eurka-server
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
eureka-server-u-r-l-context: http://${eureka.instance.hostname}:${server.port}/eureka/
這里注意spring. application.name: eurka-server這個參數(shù)一定要加低葫,表示自己注冊的服務(wù)名字
由于引入的cloud jar版本不同详羡,里面的參數(shù)可能稍微不一樣,所以大家配置yml的時候最好根據(jù)快捷提示來配置
啟動類加一個注解@@EnableEurekaServer
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
@EnableEurekaServer 表示自動開啟Eureka服務(wù)端嘿悬,也表示它是服務(wù)器
這樣一個簡單的服務(wù)注冊中心就搭建完了
我們根據(jù)端口號啟動下:
以上頁面就表示Eureka搭建完成
Client
客戶端表示提供服務(wù)实柠,概念上就是所謂的生產(chǎn)者,它讓自己的接口注冊到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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.yzl.cloud</groupId>
<artifactId>com.yzl.cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.yzl.Eureka</groupId>
<artifactId>com.yzl.Eureka</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>com.yzl.Eureka Maven Webapp</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>com.yzl.Eureka</finalName>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
沒錯善涨,最簡單的生產(chǎn)者搭建就是一個spring-cloud-starter-netflix-eureka-client
當(dāng)然實際項目中窒盐,一個生產(chǎn)者就是一個模塊,里面包括相應(yīng)的業(yè)務(wù)以及其他功能需求钢拧;
生產(chǎn)者提供接口的時候只要在接口中加入@EnableEurekaClient注解
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaClient
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
@EnableEurekaClient 表示自動開啟Eureka客戶端蟹漓,也表示它是生產(chǎn)者
yml:
server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka/
spring:
application:
name: eurka-client
這里是最簡單的生產(chǎn)者搭建,所以許多配置筆者也沒加源内,實際中可以根據(jù)自己需求加入相應(yīng)配置葡粒,注意eureka:
client:service-url這個屬性是必須的,這表示生產(chǎn)者要找的服務(wù)注冊中心地址膜钓。
暴露的接口嗽交,其實就是controller中的類:
package com.yzl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class HelloController {
@GetMapping("/getData")
public String getData(String name){
return "hello get"+name;
}
@PostMapping("/postData")
public String postData(String name){
return "hello post"+name;
}
}
controller和servcie其實和普通的項目一樣,沒有其他操作呻此,在啟動類加入相關(guān)注解轮纫,就會自動把controller的接口暴露在Eureka上;
我們啟動項目焚鲜,可以看到Eureka上多了一個注冊服務(wù):
這個紅字表示:
這是因為Eureka進入了自我保護機制掌唾,默認(rèn)情況下放前,如果EurekaServer在一定時間內(nèi)沒有接收到某個微服務(wù)實例的心跳時,EurekaServer將會注銷該實例(默認(rèn)90s)糯彬。但是當(dāng)網(wǎng)絡(luò)發(fā)生故障時凭语,微服務(wù)與EurekaServer之間無法通信,這樣就會很危險了撩扒,因為微服務(wù)本身是很健康的似扔,此時就不應(yīng)該注銷這個微服務(wù),而Eureka通過自我保護機制來預(yù)防這種情況搓谆,當(dāng)網(wǎng)絡(luò)健康后炒辉,該EurekaServer節(jié)點就會自動退出自我保護模式;
大家可以無視這段紅字或者可以關(guān)閉自我保護機制:
eureka.server.enable-self-preservation#設(shè)置為false泉手,關(guān)閉自我保護
我們主要看紅框框的那一塊黔寇,表示有服務(wù)注冊到Eureka里,這里的Application表一個注冊服務(wù)斩萌,它的名字也就是yml配置的:
spring:
application:
name: eurka-client
注冊到Eureka會默認(rèn)大寫缝裤。
這樣一個簡單的生產(chǎn)者就搭建完畢,它會將自己提供的接口暴露給Eureka颊郎,然后消費者通過方法去Eureka尋找相關(guān)接口憋飞,然后調(diào)用,完全解決耦合問題姆吭,這樣各個模塊自己獨立開發(fā)榛做,需要相關(guān)接口的時候,就去Eureka調(diào)用猾编,而且還不影響生產(chǎn)者本身使用瘤睹。
Feign+hystrix
Feign是Netflix開發(fā)的聲明式、模板化的HTTP客戶端答倡,其實就是相當(dāng)于httpClient轰传。當(dāng)然它的功能更加強大,
Spring Cloud Feign是基于Netflix feign實現(xiàn)瘪撇,整合了Spring Cloud Ribbon和Spring Cloud Hystrix获茬,不僅可以負(fù)載均衡(如果相同的生產(chǎn)者有N個,比如有127.0.0.1倔既,127.0.0.2兩個相同的生產(chǎn)者恕曲,他的Application名字要相同,F(xiàn)eign會用相應(yīng)的算法去請求生產(chǎn)者渤涌,調(diào)用其中一個佩谣,達到負(fù)載均衡的目的),而且可以提供斷路機制(簡單的說就是處理服務(wù)異常实蓬,當(dāng)然斷路器功能比這個更加豐富)
先給大家看下消費者的項目:
項目結(jié)構(gòu)很簡單茸俭,筆者也沒有分層吊履。這里的yml也貼出來了:
server:
port: 8082
spring:
application:
name: eurka-feign
eureka:
client:
service-url:
defaultZone: http://localhost:8080/eureka/
feign:
hystrix:
enabled: true
feign: hystrix: enabled: true 表示開啟斷路器
給大家看下消費者的pom:
<?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>
<parent>
<groupId>com.yzl.cloud</groupId>
<artifactId>com.yzl.cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.yzl.Feign</groupId>
<artifactId>com.yzl.Feign</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>com.yzl.Feign Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
啟動類:
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
@EnableHystrix 表示開啟斷路器
@EnableFeignClients 表示開啟Feign
我們用Feign調(diào)用Eureka接口的時候我們只需要聲明一個接口:
package com.yzl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "EURKA-CLIENT",fallback = HelloHystrix.class)
public interface HelloService {
@RequestMapping(value = "/getData",method = RequestMethod.GET)
String getHello(@RequestParam(value = "name") String name);
@RequestMapping(value = "/postData",method = RequestMethod.POST)
String postHello(@RequestParam(value = "name") String name);
}
這里要注意幾點:
1.@FeignClient(value = "EURKA-CLIENT",fallback = HelloHystrix.class)這里@FeignClient里面的參數(shù)value表示要調(diào)用的Eureka地址(也就是Eureka上的Application下面的名字),一定要相同调鬓;后面的fallback表示異常處理艇炎,就是如果調(diào)用生產(chǎn)者者接口,但是生產(chǎn)者掛了腾窝,或者超時等等情況缀踪,可以用HelloHystrix這個類來處理,這個類筆者后面說虹脯。
2.@RequestParam(value = "name") 這個參數(shù)一定要加驴娃,筆者沒加就出現(xiàn)異常 ,類似這樣的異常(當(dāng)然如果你加了fallback,異常處理機制就不一樣了)归形;
feign.FeignException: status 405 reading HelloService#getHello(String); content:
{"timestamp":"2019-09-28T12:13:55.971+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/getData"}
at feign.FeignException.errorStatus(FeignException.java:62) ~[feign-core-9.5.1.jar:na]
at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:91) ~[feign-core-9.5.1.jar:na]
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138) ~[feign-core-9.5.1.jar:na]
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:76) ~[feign-core-9.5.1.jar:na]
at feign.hystrix.HystrixInvocationHandler$1.run(HystrixInvocationHandler.java:108) ~[feign-hystrix-9.5.1.jar:na]
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:302) ~[hystrix-core-1.5.12.jar:1.5.12]
at com.netflix.hystrix.HystrixCommand$2.call(HystrixCommand.java:298) ~[hystrix-core-1.5.12.jar:1.5.12]
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Observable.unsafeSubscribe(Observable.java:10327) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:51) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Observable.unsafeSubscribe(Observable.java:10327) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:41) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeDoOnEach.call(OnSubscribeDoOnEach.java:30) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:48) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OnSubscribeLift.call(OnSubscribeLift.java:30) ~[rxjava-1.3.8.jar:1.3.8]
at rx.Observable.unsafeSubscribe(Observable.java:10327) ~[rxjava-1.3.8.jar:1.3.8]
at rx.internal.operators.OperatorSubscribeOn$SubscribeOnSubscriber.call(OperatorSubscribeOn.java:100) ~[rxjava-1.3.8.jar:1.3.8]
at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:56) ~[hystrix-core-1.5.12.jar:1.5.12]
at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction$1.call(HystrixContexSchedulerAction.java:47) ~[hystrix-core-1.5.12.jar:1.5.12]
at com.netflix.hystrix.strategy.concurrency.HystrixContexSchedulerAction.call(HystrixContexSchedulerAction.java:69) ~[hystrix-core-1.5.12.jar:1.5.12]
at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) ~[rxjava-1.3.8.jar:1.3.8]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_161]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_161]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_161]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_161]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_161]
接著我們看下HelloHystrix托慨,也就是斷路器處理:
package com.yzl;
import org.springframework.stereotype.Component;
@Component
public class HelloHystrix implements HelloService {
@Override
public String getHello(String name) {
return "get ex";
}
@Override
public String postHello(String name) {
return "post ex";
}
}
這邊沒什么好說的就是實現(xiàn)了生產(chǎn)者的接口鼻由,重寫他的方法暇榴,返回你需要的返回值,讓你判斷蕉世。這里筆者就簡單的寫了個返回值蔼紧,但是@Component這個注解是一定要加的,他要注入到spring的容器中去狠轻。
controller調(diào)用和平時沒太多區(qū)別:
package com.yzl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class HelloController {
@Autowired
private HelloService service;
@GetMapping("/hello")
public String hello(){
return "hello";
}
@GetMapping("/getData")
public String getData(@RequestParam("name") String name){
return service.getHello(name);
}
@GetMapping("/postData")
public String postData(@RequestParam("name") String name){
return service.postHello(name);
}
}
就是調(diào)用生產(chǎn)者的接口即可奸例。
我們依次開啟Eureka——client——Feign,觀察Eureka的情況:
隨便請求一個路徑向楼,如果成功查吊,返回json到頁面
這里說一下,8080是Eureka服務(wù)端湖蜕,8081是生產(chǎn)者逻卖,8082是消費者
如果失敗(我們可以關(guān)閉8081),它會根據(jù)Hystrix處理返回HelloHystrix的json數(shù)據(jù)
現(xiàn)在我們可以看下Eureka服務(wù)器情況:
就一個消費者還是在線的昭抒。
好了评也,這是一個最簡單的springcloud搭建,但could的功能遠(yuǎn)遠(yuǎn)不止這些灭返,相關(guān)代碼已上傳git
github地址:https://github.com/weiess/springCloud.git
謝謝盗迟!