最新Java程式效能優化,讓你的Java程式更快、更穩定
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配置檔案
1 <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,編寫服務控制器類程式碼
1 @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!!!