SpringCloud簡單案例

一、服務注冊與發(fā)現(xiàn)

這里會用到Spring Cloud Netflix涎跨,該項目是Spring Cloud的子項目之一隅很,主要內(nèi)容是對Netflix公司一系列開源產(chǎn)品的包裝叔营,它為Spring Boot應用提供了自配置的Netflix OSS整合绒尊。通過一些簡單的注解婴谱,開發(fā)者就可以快速的在應用中配置一下常用模塊并構(gòu)建龐大的分布式系統(tǒng)躯泰。它主要提供的模塊包括:服務發(fā)現(xiàn)(Eureka)斟冕,斷路器(Hystrix),智能路由(Zuul)景描,客戶端負載均衡(Ribbon)等超棺。

這里的核心內(nèi)容是服務發(fā)現(xiàn)模塊:Eureka

創(chuàng)建“服務注冊中心”

1.創(chuàng)建基于web的Maven項目(springcloud)

2.創(chuàng)建服務注冊中心棠绘。在springcloud項目中創(chuàng)建SpringBoot項目(springboot):

image

image.png

image

image.png

勾選Cloud Discovery–>Eureka server氧苍。以方便導包

image

image.png

3編寫springboot項目

3.1查看pom.xml文件

<?xml version="1.0"encoding="UTF-8"?>4.0.0org.springframework.bootspring-boot-starter-parent2.1.3.RELEASE<!-- lookup parent from repository -->com.handspringclouddemo0.0.1-SNAPSHOTspringclouddemoDemo project for Spring Boot1.8Greenwich.RELEASEorg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-pluginspring-milestonesSpring Milestoneshttps://repo.spring.io/milestone

4在啟動類上加上注解 如下

通過@EnableEurekaServer注解啟動一個服務注冊中心提供給其他應用進行對話。這一步非常的簡單罢荡,只需要在一個普通的Spring Boot應用中添加這個注解就能開啟此功能,比如下面的例子:

packagecom.hand;
import org.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublicclassSpringclouddemoApplication{publicstaticvoidmain(String[] args){        SpringApplication.run(SpringclouddemoApplication.class, args);    }}

5 修改application.yml文件

yml文件的好處浪南,天然的樹狀結(jié)構(gòu)络凿,一目了然

---#端口號server: port: 8760eureka: instance: hostname: localhost client:# eureka.client.registerWithEureka :表示是否將自己注冊到Eureka Server喷众,默認為true到千。# 由于當前這個應用就是Eureka Server憔四,故而設為falseregister-with-eureka:false# eureka.client.fetchRegistry :表示是否從Eureka Server獲取注冊信息了赵,默認為true甸赃。因為這是一個單點的Eureka Server络断,# 不需要同步其他的Eureka Server節(jié)點的數(shù)據(jù)貌笨,故而設為false襟沮。fetch-registry:falseservice-url:# eureka.client.serviceUrl.defaultZone :設置與Eureka Server交互的地址开伏,#查詢服務和注冊服務都需要依賴這個地址硅则。默認是defaultZone: http://{eureka.instance.hostname}:{server.port}/eureka/

6 啟動項目后訪問

http://localhost:8760

可以看到下面的頁面怎虫,其中還沒有發(fā)現(xiàn)任何服務:

image

image.png

7.搭建服務端(生產(chǎn)者)

創(chuàng)建springBoot項目同上

查看pom.xml文件

<?xml version="1.0"encoding="UTF-8"?>4.0.0org.springframework.bootspring-boot-starter-parent2.1.3.RELEASEcom.handproducer0.0.1-SNAPSHOTproducerDemo project for Spring Boot1.8Greenwich.RELEASEorg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-pluginspring-milestonesSpring Milestoneshttps://repo.spring.io/milestone

8修改application.yml文件

注:端口不能與上面的相同蘸际。這里的服務name:service-hi 可以根據(jù)自己情況定義粮彤。

---server: port:8762eureka: client: service-url: defaultZone: http://localhost:8760/eureka/spring: application: name: service-producer

9 編寫啟動類

packagecom.hand.producer;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.netflix.eureka.EnableEurekaClient;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;@SpringBootApplication@EnableEurekaClient@RestControllerpublicclassProducerApplication{publicstaticvoidmain(String[] args){ SpringApplication.run(ProducerApplication.class, args); }@Value("${server.port}") String port;@RequestMapping("/hi")publicStringhome(@RequestParam String name){return"hi "+ name +",i am from port:"+ port; }}

運行服務

http://localhost:8761/hi?name=xiong.zhang@hand-china.com

image

image.png

然后查看http://localhost:8760

可以看到,我們定義的服務被注冊了圈澈。如下圖所示:

image

image.png

9.創(chuàng)建消費者

9.1 創(chuàng)建消費者modul递递,流程如上述工程創(chuàng)建流程登舞。

9.2查看pom.xml文件

<?xml version="1.0"encoding="UTF-8"?>4.0.0org.springframework.bootspring-boot-starter-parent2.1.3.RELEASEcom.handcustomer0.0.1-SNAPSHOTcustomerDemo project for Spring Boot1.8Greenwich.RELEASEorg.springframework.cloudspring-cloud-starter-netflix-eureka-serverorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-pluginspring-milestonesSpring Milestoneshttps://repo.spring.io/milestone

10 yml配置

---server: port:8763eureka: client: service-url: defaultZone: http://localhost:8760/eureka/spring: application: name: service-customerfeign: hystrix: enabled :true

11 編寫啟動類

@EnableDiscoveryClient表明標注類是消費者菠秒,加入restTemplate以消費相關(guān)的服務

packagecom.hand.customer;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;importorg.springframework.cloud.client.loadbalancer.LoadBalanced;importorg.springframework.context.annotation.Bean;importorg.springframework.web.client.RestTemplate;@SpringBootApplication@EnableDiscoveryClientpublicclassCustomerApplication{publicstaticvoidmain(String[] args){ SpringApplication.run(CustomerApplication.class, args); }@Bean@LoadBalancedRestTemplaterestTemplate(){returnnewRestTemplate(); }}

12 .創(chuàng)建service和controller

12.1 service層

packagecom.hand.customer.controller;importcom.hand.customer.service.HelloService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;packagecom.hand.customer.service;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.web.client.RestTemplate;/**

  • Created with IntelliJ IDEA.

  • Author: Patrick

  • Date: 2019/2/25

  • Time: 23:12

  • Description:

*/@ServicepublicclassHelloService{@AutowiredRestTemplate restTemplate;publicStringhiService(String name){returnrestTemplate.getForObject("http://SERVICE-PRODUCER/hi?name="+ name, String.class); }}

12.2 controller層

packagecom.hand.customer.controller;importcom.hand.customer.service.HelloService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;/**

  • Created with IntelliJ IDEA.

  • Author: Patrick

  • Date: 2019/2/25

  • Time: 23:38

  • Description:

*/@RestControllerpublic class HelloControler { @AutowiredHelloService helloService; @RequestMapping(value="/hi") public String hi(@RequestParam String name) {returnhelloService.hiService(name); }}

再次查看服務

image

image.png

在瀏覽器中輸入http://localhost:8763/hi?name=admin

image

作者:佛祖0

鏈接:

簡書著作權(quán)歸作者所有践叠,任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權(quán)并注明出處。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末囚戚,一起剝皮案震驚了整個濱河市酵熙,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌驰坊,老刑警劉巖匾二,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異拳芙,居然都是意外死亡察藐,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門舟扎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來譬猫,“玉大人,你說我怎么就攤上這事挖垛。” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長闽铐,這世上最難降的妖魔是什么澳叉? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任瓶殃,我火速辦了婚禮淆储,結(jié)果婚禮上蓝谨,老公的妹妹穿的比我還像新娘。我一直安慰自己芦昔,他們只是感情好料扰,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布拯钻。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上致份,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天击儡,我揣著相機與錄音矫夯,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼慢显,長吁一口氣:“原來是場噩夢啊……” “哼应狱!你這毒婦竟也來了写半?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體内狗,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡找爱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鹏漆。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出庭呜,到底是詐尸還是另有隱情搀庶,我是刑警寧澤,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布别渔,位于F島的核電站,受9級特大地震影響箩朴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜绎橘,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一涮较、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧苫亦,春花似錦润匙、人聲如沸厂财。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽堪澎。三九已至,卻和暖如春便脊,著一層夾襖步出監(jiān)牢的瞬間蚂四,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工哪痰, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留遂赠,地道東北人。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓晌杰,卻偏偏與公主長得像跷睦,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子肋演,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

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