沒有廢話,直接上乾貨,理論部分你們能夠看其它資料。java
這裏是部分關鍵代碼,若是須要所有可運行的代碼,請給本人留言。web
在後繼,還將給出搭建高可用Eureka架構的方式。spring
在Eureka的服務器裏,包含着記錄當前全部服務列表的註冊中心,而服務提供者和調用者所在的機器均被稱爲「Eureka客戶端」。瀏覽器
服務提供者會和服務器進行以下的交互:第一,註冊自己能提供的服務,第二,定時發送心跳,以此證實本服務處於生效狀態。而服務調用者通常會從服務器查找服務,並根據找到的結果從服務提供者這端調用服務。服務器
這裏咱們將在EurekaBasicDemo-Server項目裏編寫Eureka服務器的代碼。架構
第一步,當咱們建立完Maven類型的項目後,須要在pom.xml裏編寫該項目所須要的依賴包,關鍵代碼以下。 app
1 <dependencyManagement>
2 <dependencies>
3 <dependency>
4 <groupId>org.springframework.cloud</groupId>
5 <artifactId>spring-cloud-dependencies</artifactId>
6 <version>Brixton.SR5</version>
7 <type>pom</type>
8 <scope>import</scope>
9 </dependency>
10 </dependencies>
11 </dependencyManagement>
12 <dependencies>
13 <dependency>
14 <groupId>org.springframework.cloud</groupId>
15 <artifactId>spring-cloud-starter-eureka-server</artifactId>
16 </dependency>
17 </project>
從第1到第11行,咱們引入了版本號是Brixton.SR5的Spring Cloud包,這個包裏包含着Eureka的支持包,在第13到16行的代碼裏,引入了Eureka Server端的支持包,引入後,咱們才能在項目的java文件裏使用Eureka的特性。負載均衡
第二步,在application.yml裏,須要配置Eureka服務端的信息,代碼以下。 框架
1 server: 2 port: 8888 3 eureka: 4 instance: 5 hostname: localhost 6 client: 7 register-with-eureka: false 8 fetch-registry: false 9 serviceUrl: 10 defaultZone: http://localhost:8888/eureka/
從第2和第5行裏,咱們指定了Eureka服務端使用的主機地址和端口號,這裏分別是localhost和8888,也就是說讓服務端運行在本地8888號端口,在第10行裏,咱們指定了服務端所在的url地址。ide
因爲這已是服務器端,因此咱們經過第7行的代碼,指定無需向Eureka註冊中心註冊本身,同理,服務器端的職責是維護服務列表而不是調用服務,因此經過第8行的代碼指定本端無需檢索服務。
第三步,在RegisterCenterApp.java裏編寫Eureka啓動代碼。
1 省略必要的package和import代碼 2 @EnableEurekaServer //指定本項目是Eureka服務端 3 @SpringBootApplication 4 public class RegisterCenterApp 5 { 6 public static void main( String[] args ) 7 {SpringApplication.run(RegisterCenterApp.class, args);} 8 }
在第6行的main函數裏,咱們仍是經過run方法啓動Eureka服務。
運行App.java啓動Eureka服務器端後,在瀏覽器裏輸入localhost:8888後,能夠看到以下圖所示的Eureka服務器端的信息面板,其中Instances currently registered with Eureka目前是空的,說明還沒有有服務註冊到本服務器的註冊中心。
這裏咱們將在EurekaBasicDemo-ServerProvider項目裏編寫Eureka客戶端的代碼,在這個項目裏,咱們將提供一個SayHello的服務。
第一步,建立完Maven類型的項目後,咱們須要在pom.xml裏寫入本項目的依賴包,關鍵代碼以下。本項目所用到的依賴包以前都用過,因此這裏就不展開講了。
1 <dependencyManagement>
2 <dependencies>
3 <dependency>
4 <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId>
5 <version>Brixton.SR5</version>
6 <type>pom</type>
7 <scope>import</scope>
8 </dependency>
9 </dependencies>
10 </dependencyManagement>
11 <dependencies>
12 <dependency>
13 <groupId>org.springframework.boot</groupId>
14 <artifactId>spring-boot-starter-web</artifactId>
15 <version>1.5.4.RELEASE</version>
16 </dependency>
17 <dependency>
18 <groupId>org.springframework.cloud</groupId>
19 <artifactId>spring-cloud-starter-eureka</artifactId>
20 </dependency>
21 </dependencies>
第二步,在application.yml裏編寫針對服務提供者的配置信息,代碼以下。
1 server: 2 port: 1111 3 spring: 4 application: 5 name: sayHello 6 eureka: 7 client: 8 serviceUrl: 9 defaultZone: http://localhost:8888/eureka/
從第2行裏,咱們能看到本服務將啓用1111號端口,在第5行,咱們指定了本服務的名字,叫sayHello,在第9行,咱們把本服務註冊到了Eureka服務端,也就是註冊中內心。
第三步,在Controller.java裏,編寫控制器部分的代碼,在其中實現對外的服務。
1 //省略必要的package和import代碼 2 @RestController //說明這是個控制器 3 public class Controller { 4 @Autowired //描述Eureka客戶端信息的類 5 private DiscoveryClient client; 6 @RequestMapping(value = "/hello/{username}", method = RequestMethod.GET ) 7 public String hello(@PathVariable("username") String username) { 8 ServiceInstance instance = client.getLocalServiceInstance(); 9 //輸出服務相關的信息 10 System.out.println("host is:" + instance.getHost()); 11 System.out.println("port is:" + instance.getPort()); 12 System.out.println("ServiceID is:" + instance.getServiceId() ); 13 //返回字符串 14 return "hello " + username; 15 } 16 }
咱們經過第6和第7行的代碼,指定了能觸發hello方法的url格式,在這個方法裏,咱們首先經過第10到12行的代碼,輸出了主機名、端口號和ServiceID等信息,並在第14行裏,返回了一個字符串。
第四步,編寫Spring Boot的啓動類ServiceProviderApp.java,代碼以下。
1 //省略必要的package和import代碼 2 @SpringBootApplication 3 @EnableEurekaClient 4 public class ServiceProviderApp { 5 public static void main( String[] args ) 6 {SpringApplication.run(ServiceProviderApp.class, args);} 7 }
因爲這是處於Eureka的客戶端,因此加入第3行所示的註解,在main函數裏,咱們依然是經過run方法啓動Spring Boot服務。
啓動Eureka服務器端的RegisterApp.java和服務提供者端的ServiceProviderApp.java,在瀏覽器裏輸入http://localhost:8888/後,在Eureka的信息面板裏能看到SayHello服務,以下圖所示。
若是這時咱們在瀏覽器裏輸入http://localhost:1111/hello/Mike,能直接調用服務,同時能看瀏覽器裏看到「hello Mike」的輸出。不過在大多數的場景裏,咱們通常是在程序裏調用服務,而不是簡單地經過瀏覽器調用,在下面的EurekaBasicDemo-ServiceCaller項目裏,咱們將演示在Eureka客戶端調用服務的步驟。
第一步。在這個Maven項目裏,咱們編寫以下的pom.xml配置,關鍵代碼以下。
1 <dependencyManagement> 2 <dependencies> 3 <dependency> 4 <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId> 5 <version>Brixton.SR5</version> 6 <type>pom</type> 7 <scope>import</scope> 8 </dependency> 9 </dependencies> 10 </dependencyManagement> 11 <dependencies> 12 <dependency> 13 <groupId>org.springframework.boot</groupId> 14 <artifactId>spring-boot-starter-web</artifactId> 15 <version>1.5.4.RELEASE</version> 16 </dependency> 17 <dependency> 18 <groupId>org.springframework.cloud</groupId> 19 <artifactId>spring-cloud-starter-eureka</artifactId> 20 </dependency> 21 <dependency> 22 <groupId>org.springframework.cloud</groupId> 23 <artifactId>spring-cloud-starter-ribbon</artifactId> 24 </dependency> 25 </dependencies>
請你們注意,從第21到24行,咱們須要引入 ribbon的依賴包,經過它咱們能夠實現負載均衡。而其它的依賴包咱們以前都已經見過,因此就再也不解釋了。
第二步,在application.yml裏,咱們編寫針對本項目的配置信息,代碼以下。
1 spring: 2 application: 3 name: callHello 4 server: 5 port: 8080 6 eureka: 7 client: 8 serviceUrl: 9 defaultZone: http://localhost:8888/eureka/
在第3行裏,咱們指定了本服務的名字叫callHello,在第5行裏咱們指定了本服務是運行在8080端口,在第9行裏,咱們把本服務註冊到Eureka服務器上。
第三步,編寫提供服務的控制器類,在其中調用服務提供者的提供的服務,代碼以下。
1 //省略必要的package和import代碼 2 @RestController 3 @Configuration 4 public class Controller { 5 @Bean 6 @LoadBalanced 7 public RestTemplate getRestTemplate() 8 { return new RestTemplate(); } 9 10 @RequestMapping(value = "/hello", method = RequestMethod.GET ) 11 public String hello() { 12 RestTemplate template = getRestTemplate(); 13 String retVal = template.getForEntity("http://sayHello/hello/Eureka", String.class).getBody(); 14 return "In Caller, " + retVal; 15 } 16 }
在第7行的getRestTemplate方法上,咱們啓動了@LoadBalanced(負載均衡)的註解。關於負載均衡的細節將在後面章節裏詳細描述,這裏咱們引入@LoadBalanced註解的緣由是,RestTemplate類型的對象自己不具有調用遠程服務的能力,也就是說,若是咱們去掉這個註解,程序未必能跑通。只有當咱們引入該註解,該方法所返回的對象才能具有調用遠程服務的能力。
在提供服務的第11行的hello方法裏,咱們是經過第13行的代碼,用RestTemplate類型對象的getForEntity方法,調用服務提供者sayHello提供的hello方法。
這裏咱們是經過http://sayHello/hello/Eureka這個url去發現對應的服務,在這個url裏,只包含了服務名sayHello,並無包含服務所在的主機名和端口號,換句話說,該url實際上是經過註冊中心定位到sayHello服務的物理位置的。
至於這個url和該服務物理位置的綁定關係,是在Eureka內部實現的,這也是Eureka能夠被稱做「服務發現框架」的緣由。
第四步,在ServiceCallerApp.java方法裏,咱們編寫啓動本服務的代碼,這咱們已經很熟悉了,因此就再也不講述了。
1 //省略必要的package和import代碼 2 @EnableDiscoveryClient 3 @SpringBootApplication 4 public class ServiceCallerApp 5 { 6 public static void main( String[] args ) 7 {SpringApplication.run(ServiceCallerApp.class, args); } 8 }
當咱們依次啓動Eureka服務器(也就是註冊中心)、服務提供者和服務調用者的Spring Boot啓動程序後,在瀏覽器裏輸入http://localhost:8888/後,能在信息面板裏看到有兩個服務,分別是服務提供者SayHello和服務調用者CallHello,以下圖所示。
因爲服務調用者運行在8080端口上,因此若是咱們在瀏覽器裏輸入http://localhost:8080/hello,能看到在瀏覽器裏輸出「In Caller, hello Eureka」,這就說明它確實已經調用了服務提供者SayHello裏的hello方法。
此外,咱們還能在服務提供者所在的控制檯裏看到host、port和ServiceID的輸出,以下圖所示,這能進一步驗證了服務提供者裏控制器類裏的hello方法被服務調用者調到。