Spring Cloud學習01--Eureka基本使用
Eureka的功能:雲端服務發現,一個基於 REST 的服務,用於定位服務,以實現雲端中間層服務發現和故障轉移。
在微服務框架中,是一個用於“服務註冊與發現”的工具。在Spring Cloud的H版(Hoxton)已經不再推薦使用此工具,而是建議使用Nacos或者Consul來代替。
因此本文屬於@Deprecated,已經過時的知識。當然官方的推薦只是建議而已,實際專案仍然可以繼續使用這個工具。下面通過一個專案演示此工具的基本使用。
1.建立主體專案:
使用IDEA建立一個Spring Boot的專案,取名為springclouddemo。修改pom檔案,新增如下節點:
1 <properties> 2 <java.version>1.8</java.version> 3 <spring.cloud-version>Hoxton.SR8</spring.cloud-version> 4 </properties> 5 6 <dependencyManagement> 7 <dependencies> 8 <dependency> 9 <groupId>org.springframework.cloud</groupId> 10 <artifactId>spring-cloud-dependencies</artifactId> 11 <version>${spring.cloud-version}</version> 12 <type>pom</type> 13 <scope>import</scope> 14 </dependency> 15 </dependencies> 16 </dependencyManagement>
2.建立Eureka服務子專案一:
在第1步建立的專案中,新建一個Module,建立一個Maven專案,取名為01-eureka。
並在pom檔案中新增以下引用:
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-security</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.cloud</groupId> 8 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 9 </dependency> 10 <dependency> 11 <groupId>org.springframework.boot</groupId> 12 <artifactId>spring-boot-starter-web</artifactId> 13 </dependency> 14 </dependencies>
第4行配置是引入spring boot的安全控制。
第8行配置是引入Eureka的服務端。
第12行配置是引入spring mvc開啟web站點功能。
新建一個SpringBoot啟動類,並新增相關注解:
1 package com.yangasen; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 @SpringBootApplication 8 @EnableEurekaServer 9 public class EurekaApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(EurekaApplication.class,args); 13 } 14 }
新增配置檔案,application.yml:
1 server: 2 port: 8761 3 4 eureka: 5 instance: 6 hostname: localhost 7 client: 8 registerWithEureka: true 9 fetchRegistry: true 10 serviceUrl: 11 defaultZone: http://root:root@localhost:8762/eureka/ 12 13 spring: 14 security: 15 user: 16 name: root 17 password: root 18 application: 19 name: EUREKA
新增配置類,WebSecurityConfig,開啟安全控制:
1 package com.yangasen.config; 2 3 import org.springframework.security.config.annotation.web.builders.HttpSecurity; 4 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; 5 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 6 7 @EnableWebSecurity 8 public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 9 @Override 10 protected void configure(HttpSecurity http) throws Exception { 11 http.csrf().ignoringAntMatchers("/eureka/**"); 12 super.configure(http); 13 } 14 }
3.建立Eureka服務子專案二:
在第1步建立的專案中,新建一個Module,建立一個Maven專案,取名為04-eureka。
配置均與第2步相同,只是application.yml配置修改如下:
1 server: 2 port: 8762 3 4 eureka: 5 instance: 6 hostname: localhost 7 client: 8 registerWithEureka: true 9 fetchRegistry: true 10 serviceUrl: 11 defaultZone: http://root:root@localhost:8761/eureka/ 12 13 spring: 14 security: 15 user: 16 name: root 17 password: root 18 application: 19 name: EUREKA
4.建立Eureka客戶端子專案,用於呼叫服務:
在第1步建立的專案中,新建一個Module,建立一個Maven專案,取名為02-customer。
pom檔案配置如下:
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-web</artifactId> 9 </dependency> 10 </dependencies>
新增一個spirngboot啟動類:
1 package com.yangasen; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 import org.springframework.context.annotation.Bean; 7 import org.springframework.web.client.RestTemplate; 8 9 10 @SpringBootApplication 11 @EnableEurekaClient 12 public class CustomerApplication { 13 public static void main(String[] args) { 14 SpringApplication.run(CustomerApplication.class,args); 15 } 16 @Bean 17 public RestTemplate restTemplate(){ 18 return new RestTemplate(); 19 } 20 }
新增配置檔案application.yml:
1 eureka: 2 client: 3 serviceUrl: 4 defaultZone: http://root:root@localhost:8761/eureka,http://root:root@localhost:8762/eureka 5 6 spring: 7 application: 8 name: CUSTOMER
新增用於測試的Controller:
1 package com.yangasen.controller; 2 3 import com.netflix.appinfo.InstanceInfo; 4 import com.netflix.discovery.EurekaClient; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.GetMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 import org.springframework.web.client.RestTemplate; 9 10 @RestController 11 public class CustomerController { 12 13 @Autowired 14 private RestTemplate restTemplate; 15 16 @Autowired 17 private EurekaClient eurekaClient; 18 19 @GetMapping("/customer") 20 public String customer(){ 21 InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("SEARCH",false); 22 String url = instanceInfo.getHomePageUrl(); 23 System.out.println(url); 24 String result = restTemplate.getForObject(url+"/search",String.class); 25 return result; 26 } 27 }
5.建立Eureka客戶端子專案,用於提供服務:
在第1步建立的專案中,新建一個Module,建立一個Maven專案,取名為03-search。
pom檔案配置如下:與02專案相同。
1 <dependencies> 2 <dependency> 3 <groupId>org.springframework.cloud</groupId> 4 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 5 </dependency> 6 <dependency> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-web</artifactId> 9 </dependency> 10 </dependencies>
新增springboot啟動類,與03專案相同。
1 package com.yangasen; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.EnableEurekaClient; 6 7 @SpringBootApplication 8 @EnableEurekaClient 9 public class SearchApplication { 10 public static void main(String[] args) { 11 SpringApplication.run(SearchApplication.class,args); 12 } 13 }
新增配置檔案application.yml,注意埠與02專案不同:
1 eureka: 2 client: 3 serviceUrl: 4 defaultZone: http://root:root@localhost:8761/eureka,http://root:root@localhost:8762/eureka 5 6 spring: 7 application: 8 name: SEARCH 9 10 server: 11 port: 8081
新增用於測試的Controller檔案:
1 package com.yangasen.controller; 2 3 import org.springframework.web.bind.annotation.GetMapping; 4 import org.springframework.web.bind.annotation.RestController; 5 6 @RestController 7 public class SearchController { 8 9 @GetMapping("/search") 10 public String search(){ 11 return "search"; 12 } 13 }
6.測試效果:
將以上四個模組都啟動,在瀏覽器中訪問如下地址:http://localhost:8080/customer
瀏覽器中會顯示“search”字樣,測表示Eureka服務執行正常。
同時,可以通過http://localhost:8761以及http://localhost:8762這兩個地址(賬戶:root,密碼:root)登入,檢視Eureka服務的資訊。如圖所示:
登入介面
8761埠管理介面:
8762埠管理介面: