Eureka
Eureka(原來以爲是縮寫,原來就是一個單詞,翻譯爲:我發現了,我找到了!0.0)是Netflix開源的一款提供服務註冊和發現的產品,它提供了完整的Service Registry和Service Discovery實現。也是springcloud體系中最重要最核心的組件之一。
這個東西通俗的理解就像是一個淘寶,你是賣家也好,還是買家也好,你要交易,你得在我這先註冊一個賬號。
1,先新建一個maven工程
2,在pom文件中引入相關jar包
學習大佬的教程,結果用大佬的demo直接報錯,啓動程序一直提示:
Caused by: java.lang.ClassNotFoundException: com.sun.jersey.api.core.DefaultResourceConfig
鬱悶,查看spring-cloud-starter-eureka-server jar包
發現其中引入的jersey的jar是1.19.1,然後自己研究,發現1.19可以使用,遂在pom文件中引入,按照我的理解1.19.1肯定比1.19版本高的,怎麼反而不行了?
再啓動,然後這個錯誤是消失了,結果後面又報錯,又出來一個servo 包下的類找不到,mmp~又是版本問題,再引入 servo包,ok了~
最終形成如下的pom配置文件
<parent>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-parent</artifactId>
4 <version>1.5.8.RELEASE</version>
5 </parent>
6
7 <dependencies>
8 <dependency>
9 <groupId>org.springframework.cloud</groupId>
10 <artifactId>spring-cloud-starter</artifactId>
11 </dependency>
12 <dependency>
13 <groupId>com.sun.jersey</groupId>
14 <artifactId>jersey-bundle</artifactId>
15 <version>1.19</version>
16 </dependency>
17
18 <dependency>
19 <groupId>com.netflix.servo</groupId>
20 <artifactId>servo-core</artifactId>
21 <version>0.12.7</version>
22 </dependency>
23 <dependency>
24 <groupId>org.springframework.cloud</groupId>
25 <artifactId>spring-cloud-starter-eureka-server</artifactId>
26 </dependency>
27 </dependencies>
28 <dependencyManagement>
29 <dependencies>
30 <dependency>
31 <groupId>org.springframework.cloud</groupId>
32 <artifactId>spring-cloud-dependencies</artifactId>
33 <version>Dalston.RC1</version>
34 <type>pom</type>
35 <scope>import</scope>
36 </dependency>
37 </dependencies>
38 </dependencyManagement>
39 <repositories>
40 <repository>
41 <id>spring-milestones</id>
42 <name>Spring Milestones</name>
43 <url>https://repo.spring.io/milestone</url>
44 <snapshots>
45 <enabled>false</enabled>
46 </snapshots>
47 </repository>
48 </repositories>
3,編寫啓動類代碼
1 @SpringBootApplication
2 @EnableEurekaServer
3 public class App {
4
5 public static void main(String[] args) {
6 SpringApplication.run(App.class, args);
7 }
8 }
注意添加EnableEurekaServer註解
4,添加配置文件
對於這個配置文件的添加有2種格式,一種是application.properties 另外一種是 application.yaml。對於2種格式的區別,我們不做比較。但是對於這個文件的位置,我還是納悶了一會,最後經過嘗試,如圖所示位置
並且需要注意文件名稱一個字母都不能少0.0,我就是由於沒注意少寫個字母,也報錯了。。。。
application.properties 格式,文件內容如下:
1 spring.application.name=spring-cloud-eureka
2 server.port=8000
3 eureka.client.register-with-eureka=false
4 eureka.client.fetch-registry=false
5 eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
注意第3 行默認是true也就是如果你不加上這個false,啓動就會報錯,因爲他會想把自己註冊到自己上面!!!第4行默認也是true,意思是他要不要獲取註冊到服務中心的信息
5,啓動註冊中心
在瀏覽器輸入 localhost:8000,查看註冊中心是否正常啓動,出現如下截圖,說明已經ok
有了註冊中心,我們在接着搞一個服務提供者,和服務消費者。
服務提供者
1,新建maven工程
2,在pom文件中引入和註冊中心服務一樣的jar包。
3,編寫application.properties
內容如下:
1 spring.application.name=spring-cloud-producer
2 server.port=9000
3 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
第一行是給自己的服務命名,第二行設置自己的訪問端口,第三行設置自己要註冊到那個註冊中心,因爲我們在上面設置了eureka註冊中心是本地的8000端口,所以就寫這個地址
4,編寫啓動類代碼
1 @SpringBootApplication
2 @EnableDiscoveryClient
3 public class App
4 {
5 public static void main( String[] args )
6 {
7 SpringApplication.run(App.class, args);
8 }
9 }
注意添加 EnableDiscoveryClient 註解
5,編寫服務控制器類代碼
@RestController
2 public class HelloController {
3
4 @RequestMapping("/hello")
5 public String hello(@RequestParam String name) {
6 return "hello "+name+",nice to meet you!";
7 }
8 }
到這裏 服務提供者完成,啓動程序,無報錯即可,刷新註冊中心的頁面,會看到Application中當前註冊的服務。
服務調用者
1,新建maven工程
2,同樣在pom文件中引入和之前一樣的內容。
3,編寫application.properties
1 spring.application.name=spring-cloud-consumer
2 server.port=9001
3 eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
第一行也是給當前服務起名字,第二行設置端口,第三行設置註冊中心url。
4,編寫啓動類代碼
1 @SpringBootApplication
2 @EnableDiscoveryClient
3 @EnableFeignClients
4 public class App
5 {
6 public static void main( String[] args )
7 {
8 SpringApplication.run(App.class, args);
9 }
10 }
注意這個啓動類,比服務提供者多了一個EnableFeignClients註解,這個註解的作用就是啓用feign進行遠程調用。
5,編寫feign調用實現
1 @FeignClient(name= "spring-cloud-producer")
2 public interface HelloRemote {
3 @RequestMapping(value = "/hello")
4 public String hello(@RequestParam(value = "name") String name);
5 }
注意這是一個接口,上面的註解參數name,就是指定你當前要調用的服務提供者名稱。另外還要注意方法中的參數name 和服務提供者中的參數保持一致
6,編寫服務調用者控制器類
1 @RestController
2 public class ConsumerController {
3
4 @Autowired
5 HelloRemote HelloRemote;
6
7 @RequestMapping("/hello/{name}")
8 public String hello(@PathVariable("name") String name) {
9 return HelloRemote.hello(name);
10 }
11
12 }
在當前類中引入HelloRemote 接口,通過調用本地hello方法,然後再調用HelloRemote 接口中的方法
啓動程序,無報錯即可。
刷新註冊中心這個時候應該可以看到2個服務已經註冊
測試驗證
打開瀏覽器輸入 : http://localhost:9001/hello/JJ
如上圖正常返回結果,說明整個服務調用和提供者ok!!!