概述
用一個簡單的例子演示spring cloud中eureka和ribbon的基本用法。
版本和環境
- idea
- spring boot 1.5.·0
- jdk 1.8
- maven 3
構建eureka server
在spring cloud,可以使用eureka來管理微服務,微服務可以注冊到eureka中。
首先可以用idea的spring initialzr
來創建eureka server注冊中心。
修改application.properties文件,添加如下內容
1
2
3
4
5
|
spring.application.name=eureka-server eureka.instance.hostname=localhost eureka.client.register-with-eureka= false eureka.client.fetch-registry= false server.port= 8881 |
在spring boot為我們生成的啟動類serverapplication
中加入@enableeurekaserver
注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.springcloud.eureka; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.netflix.eureka.server.enableeurekaserver; @enableeurekaserver @springbootapplication public class serverapplication { public static void main(string[] args) { springapplication.run(serverapplication. class , args); } } |
在瀏覽器中輸入http://localhost:8881/
可以看到如下界面:
可以看到暫時還沒有服務注冊上來。到此,一個簡單的微服務注冊中心就構建完了。
編寫微服務userservice
接下來用rest構建一個微服務接口,并注冊到注冊中心上去。依然使用spring initialzr
來構建一個新的工程。使用方式跟上面的一樣。
注意這次要勾選eureka discovery
組件。而不是eureka server
。
修改application.properties文件,添加如下內容:
1
2
3
|
spring.application.name=user server.port= 8882 eureka.client.service-url.defaultzone=http: //localhost:8881/eureka/ |
在spring boot
為我們生成的userapplication
類中使用@enablediscoveryclient
注解。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
package com.springcloud; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; @enablediscoveryclient @springbootapplication public class userapplication { public static void main(string[] args) { springapplication.run(userapplication. class , args); } } |
創建一個rest full的微服務接口。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.springcloud; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.restcontroller; @restcontroller public class usercontroller { @getmapping ( "/getuser" ) public string getuser() { return "i am user list." ; } } |
運行userapplication后,再次訪問http://localhost:8881/
會發現user這個服務已經注冊上來了。
編寫微服務order
接下來,我們再構建一個訂單的微服務,并訪問user這個微服務中的接口。
依然使用spring initialzr
來構建一個新工程。user這個微服務是可以部署到多臺機器上的。客戶端在訪問這個服務的時候,請求可以路由到任何一臺部署了user服務的機器。因此客戶端需要使用一個路由算法來調度user這個服務。在spring cloud中,可以使用ribbon組件來做客戶端路由。ribbon內部會去服務注冊中心獲取服務列表的,以便調用對應的服務。
這次除了勾選eureka discovery
組件。還需要勾選ribbon
。
修改application.properties文件,添加如下內容:
1
2
3
|
spring.application.name=order server.port= 8883 eureka.client.service-url.defaultzone=http: //localhost:8881/eureka/ |
在spring boot
為我們生成的orderapplication
類中增加如下配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.springboot; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.cloud.client.discovery.enablediscoveryclient; import org.springframework.cloud.client.loadbalancer.loadbalanced; import org.springframework.context.annotation.bean; import org.springframework.web.client.resttemplate; @enablediscoveryclient @springbootapplication public class orderapplication { @bean @loadbalanced resttemplate resttemplate() { return new resttemplate(); } public static void main(string[] args) { springapplication.run(orderapplication. class , args); } } |
由于使用了ribbon,這里需要使用@loadbalanced
注解。
編寫ordercontroller
。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package com.springboot; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.restcontroller; import org.springframework.web.client.resttemplate; @restcontroller public class ordercontroller { @autowired private resttemplate resttemplate; @getmapping ( "/getorderuser" ) public string getorderuser() { return resttemplate.getforentity( "http://user/getuser" ,string. class ).getbody(); } } |
運行orderapplication
后,訪問http://localhost:8881/
會發現order這個服務也被注冊到注冊中心了。
接下來我們訪問ordercontroller
中的getorderuser
方法,觸發調用usercontroller
的getuser
方法。
在瀏覽器中輸入:http://localhost:8883/getorderuser
可以看到返回了:i am user list.
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/linsongbin1/article/details/79361268