SpringCloud入門

首先在idea中新建一個(gè)工程demo勤家,作為父工程犁珠。


image.png

創(chuàng)建完成后的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>

<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>demo</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.13.RELEASE</version>
</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>Finchley.RELEASE</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR4</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>
<modules>
  <module> server1</module>
    <module>client</module>
    <module>ribbon</module>
    <module>zuul</module>
</modules>

</project>

<modules> 聲明多個(gè)模塊:server陈莽,client底挫,ribbon,zuul淑际。
<dependencyManagement> 統(tǒng)一依賴的版本號(hào)畏纲。只是聲明依賴,并未被繼承春缕,只有子工程中寫了該依賴項(xiàng)盗胀,且未注明版本號(hào),才會(huì)從父項(xiàng)目中繼承此項(xiàng)锄贼。(當(dāng)子工程指定版本時(shí)票灰,使用子工程中指定的版本)。
<dependencies> 所有在 <dependencies>里的依賴都會(huì)自動(dòng)注入宅荤,默認(rèn)被所有的子工程繼承屑迂。即使子工程中不寫依賴,在父工程中<dependencies>里的依賴也都會(huì)被子工程繼承膘侮。

然后創(chuàng)建子工程eureka server1服務(wù)注冊(cè)中心屈糊,用于提供服務(wù)的注冊(cè)與發(fā)現(xiàn)。

image.png

server1 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>
<groupId>com.example</groupId>
<artifactId>server1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>server1</name>
<description>Demo project for Spring Boot</description>
<parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>



<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

修改server1 pom <parent>繼承父工程
<parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<parent>相關(guān)配置取自父工程


image.png

pom添加相應(yīng)依賴琼了,配置完成后在啟動(dòng)類中@EnableEurekaServer。
之后進(jìn)行application.yml相關(guān)配置夫晌。


server:
port: 1111
eureka:
instance:
hostname: qlocal
prefer-ip-address: true
lease-expiration-duration-in-seconds: 20
lease-renewal-interval-in-seconds: 10
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://qlocal:1110/eureka/,http://qlocal:1111/eureka/
spring:
application:
name: SERVERSEV


server.port 聲明端口號(hào)為 1111

eureka.instance.hostname 設(shè)置當(dāng)前實(shí)例主機(jī)名為 qlocal(在本地hosts中添加 127.0.0.1 qlocal)

eureka.instance.prefer-ip-address 不使用主機(jī)名定義注冊(cè)中心地址雕薪,而使用IP地址的形式。Spring會(huì)自動(dòng)為我們獲取第一個(gè)非回環(huán)IP地址(除環(huán)路IP外的第一個(gè)IP地址)晓淀。

eureka.instance.lease-expiration-duration-in-seconds 定義服務(wù)失效時(shí)間為20 秒所袁。表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超時(shí)時(shí)間凶掰,在這個(gè)時(shí)間內(nèi)若沒(méi)收到下一次心跳燥爷,則將移除該instance。
默認(rèn)為90秒
如果該值太大懦窘,則很可能將流量轉(zhuǎn)發(fā)過(guò)去的時(shí)候前翎,該instance已經(jīng)不存活了。
如果該值設(shè)置太小了畅涂,則instance則很可能因?yàn)榕R時(shí)的網(wǎng)絡(luò)抖動(dòng)而被摘除掉港华。
該值至少應(yīng)該大于lease-renewal-interval-in-seconds的值。

eureka.instance.lease-renewal-interval-in-seconds 定義服務(wù)續(xù)約任務(wù)(心跳)的調(diào)用間隔10秒午衰。 表示eureka client發(fā)送心跳給server端的頻率立宜。如果在leaseExpirationDurationInSeconds后冒萄,server端沒(méi)有收到client的心跳,則將摘除該instance橙数。除此之外尊流,如果該instance實(shí)現(xiàn)了HealthCheckCallback,并決定讓自己unavailable的話灯帮,則該instance也不會(huì)接收到流量崖技。
默認(rèn)30秒

eureka.client.fetch-registry 為true 進(jìn)行服務(wù)檢查

eureka.client.register-with-eureka 為true 向注冊(cè)中心注冊(cè)自己

eureka.client.service-url. defaultZone=http://qlocal:1110/eureka/,http://qlocal:1111/eureka/ 指定服務(wù)注冊(cè)中心的位置為http://qlocal:1110/eureka/,http://qlocal:1111/eureka/ 兩個(gè)地址。

spring.application.name=SERVERSEV 指定應(yīng)用名稱SERVERSEV

配置完成后訪問(wèn) qlocal:1111


image.png

接著創(chuàng)建eureka client 服務(wù)提供者

image.png

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>
<groupId>com.example</groupId>
<artifactId>client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


然后在啟動(dòng)類中添加@EnableEurekaClient

啟動(dòng)類

@RestController
@EnableEurekaClient
@SpringBootApplication
public class ClientApplication {

public static void main(String[] args) {
    SpringApplication.run(ClientApplication.class, args);
}
@Value("${server.port}")
String port;
@RequestMapping("/my")
public String home(@RequestParam String name){
 return "my name" +name +" 端口" +port;
}

}

application.yml配置

server:
port: 1112
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1110/eureka/,http://localhost:1111/eureka/
prefer-same-zone-eureka: true
instance:
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
spring:
application:
name: CLIENTSEV


eureka.client.prefer-same-zone-eureka 實(shí)例是否使用同一zone里的eureka服務(wù)器施流,默認(rèn)為true响疚,理想狀態(tài)下,eureka客戶端與服務(wù)端是在同一zone下

訪問(wèn) qlocal:1112/my?name=1

image.png
image.png

eureka ribbon+hystrix 消費(fèi)者+斷路器

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>

<groupId>com.example</groupId>
<artifactId>ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>ribbon</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>




<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-ribbon</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-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

想要自動(dòng)重試必須添加retry依賴
想要自動(dòng)重試必須添加retry依賴
想要自動(dòng)重試必須添加retry依賴
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>

application.yml配置

eureka:
client:
service-url:
defaultZone: http://qlocal:1110/eureka/,http://qlocal:1111/eureka/
server:
port: 1114
spring:
application:
name: RIBBONSERV
cloud:
loadbalancer:
retry:
enabled: true
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 10000
RIBBONSERV:
ribbon:
ConnectTimeout: 250
ReadTimeout: 1000
OkToRetryOnAllOperations: true
MaxAutoRetriesNextServer: 2
MaxAutoRetries: 1


spring.cloud. loadbalancer.retry.enabled=true 開(kāi)啟重連機(jī)制

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=10000 斷路器的超時(shí)時(shí)間,斷路器的超時(shí)時(shí)間需要大于ribbon的超時(shí)時(shí)間瞪醋,不然不會(huì)觸發(fā)重試忿晕。
RIBBONSERV: #服務(wù)名(spring.application.name=RIBBONSERV)
ribbon:
ConnectTimeout: 250 #ribbon請(qǐng)求連接的超時(shí)時(shí)間
ReadTimeout: 1000 #ribbon 請(qǐng)求處理的超時(shí)時(shí)間
OkToRetryOnAllOperations: true #對(duì)所有操作請(qǐng)求都進(jìn)行重試
MaxAutoRetriesNextServer: 2 #對(duì)下個(gè)實(shí)例的重試次數(shù)
MaxAutoRetries: 1 #對(duì)當(dāng)前實(shí)例的重試次數(shù)

啟動(dòng)類配置
添加@EnableEurekaClient 向服務(wù)中心進(jìn)行注冊(cè)
@Bean //向程序ioc注入bean restTemplate
@LoadBalanced //表明 restTemplate開(kāi)啟負(fù)載均衡功能


@EnableHystrix
@EnableEurekaClient
@SpringBootApplication
public class RibbonApplication {

public static void main(String[] args) {
    SpringApplication.run(RibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
    return  new RestTemplate();
}

}

新建一個(gè)Service類,通過(guò)訪問(wèn)之前Clientsev的/my接口進(jìn)行測(cè)試银受。這里使用了程序名替代了具體的url地址践盼,ribbon中它會(huì)根據(jù)服務(wù)名來(lái)選擇具體的服務(wù)實(shí)例,根據(jù)服務(wù)實(shí)例在請(qǐng)求的時(shí)候會(huì)用具體的url替換掉服務(wù)名


service

@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "error")
public String helservice(String name){
return restTemplate.getForObject("http://CLIENTSEV/my?name="+name,String.class);
}
public String error(String name){
return "my name" + name +" 斷路" ;

}

}


新建controller 類 調(diào)用service方法

@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping(value = "/my")
public String con(@RequestParam String name){
return helloService.helservice(name);
}

}


啟動(dòng) 一次開(kāi)啟 server 1110 1111 client 1112 1113 ribbon 1114(qlocal:1114/my?name=1)

image.png

zuul 網(wǎng)關(guān)

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>

<groupId>com.example</groupId>
<artifactId>zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>zuul</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>



<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.retry</groupId>
        <artifactId>spring-retry</artifactId>
    </dependency>


</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>


application.yml


eureka:
client:
service-url:
defaultZone: http://qlocal:1110/eureka/,http://qlocal:1111/eureka/
server:
port: 1115
spring:
application:
name: ZUULSERV
cloud:
loadbalancer:
retry:
enabled: true
zuul:
routes:
ribbon:
path: /ribbon/**
serviceId: RIBBONSERV
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 10000
ZUULSERV:
ribbon:
ConnectTimeout: 250
ReadTimeout: 1000
OkToRetryOnAllOperations: true
MaxAutoRetriesNextServer: 2
MaxAutoRetries: 1


啟動(dòng)類


@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
public class ZuulApplication {

public static void main(String[] args) {
    SpringApplication.run(ZuulApplication.class, args);
}

}
--------------------------------------------------------------------------------------------------------------------訪問(wèn)地址 qlocal:1115/ribbon/my?name=1

源碼地址:鏈接:https://pan.baidu.com/s/1rdNfniIgEV_9yQjHFCeibw 密碼:v2bd

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末宾巍,一起剝皮案震驚了整個(gè)濱河市咕幻,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌顶霞,老刑警劉巖肄程,帶你破解...
    沈念sama閱讀 218,607評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異选浑,居然都是意外死亡蓝厌,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,239評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門古徒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)拓提,“玉大人,你說(shuō)我怎么就攤上這事隧膘〈” “怎么了?”我有些...
    開(kāi)封第一講書人閱讀 164,960評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵疹吃,是天一觀的道長(zhǎng)蹦疑。 經(jīng)常有香客問(wèn)我,道長(zhǎng)互墓,這世上最難降的妖魔是什么必尼? 我笑而不...
    開(kāi)封第一講書人閱讀 58,750評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上判莉,老公的妹妹穿的比我還像新娘豆挽。我一直安慰自己,他們只是感情好券盅,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,764評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布帮哈。 她就那樣靜靜地躺著,像睡著了一般锰镀。 火紅的嫁衣襯著肌膚如雪娘侍。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 51,604評(píng)論 1 305
  • 那天泳炉,我揣著相機(jī)與錄音憾筏,去河邊找鬼。 笑死花鹅,一個(gè)胖子當(dāng)著我的面吹牛氧腰,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播刨肃,決...
    沈念sama閱讀 40,347評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼古拴,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了真友?” 一聲冷哼從身側(cè)響起黄痪,我...
    開(kāi)封第一講書人閱讀 39,253評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎盔然,沒(méi)想到半個(gè)月后桅打,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,702評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡愈案,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,893評(píng)論 3 336
  • 正文 我和宋清朗相戀三年油额,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片刻帚。...
    茶點(diǎn)故事閱讀 40,015評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖涩嚣,靈堂內(nèi)的尸體忽然破棺而出崇众,到底是詐尸還是另有隱情,我是刑警寧澤航厚,帶...
    沈念sama閱讀 35,734評(píng)論 5 346
  • 正文 年R本政府宣布顷歌,位于F島的核電站,受9級(jí)特大地震影響幔睬,放射性物質(zhì)發(fā)生泄漏眯漩。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,352評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望赦抖。 院中可真熱鬧舱卡,春花似錦、人聲如沸队萤。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,934評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)要尔。三九已至舍杜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間赵辕,已是汗流浹背既绩。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 33,052評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留还惠,地道東北人饲握。 一個(gè)月前我還...
    沈念sama閱讀 48,216評(píng)論 3 371
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像吸重,于是被迫代替她去往敵國(guó)和親互拾。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,969評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容