Spring Cloud Eureka 是Spring Cloud Netfix 微服務套件的一部分,基於 Netfix Eureka做了二次封裝,主要負責實現微服務架構中的服務治理功能 。
一、服務治理
1. 服務註冊:
在服務治理框架中,通常都會構建一個註冊中心,每個服務但願向註冊中心登記自己提供的服務,將主機與端口號、版本號、通信協議等一些附加信息告知註冊中心,註冊中心按服務名分類組織服務清單。另外,服務註冊中心還需要以心跳的方式去檢測清單的服務是否可用,若不可用需要從服務清單中剔除,達到排除故障服務的效果。
2. 服務發現:
由於在服務治理框架下運作,服務間的調用不在通過指定具體的實例地址來實現, 而是通過向服務名發起請求調用實現。所以,服務調用房在調用提供方的藉口的時候,並不知道具體的服務實例的位置。因此,調用方需要向服務註冊中心諮詢服務,冰獲取所有服務的實例清單,以實現對具體服務實例的訪問。
二、編寫註冊中心服務
1. 添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
注意:spring-cloud-dependencies 的版本一定要和spring-boot的版本一致,我使用的spring-boot的版本是 2.3.0.RELEASE
詳細的版本對應在這個鏈接中 https://start.spring.io/actuator/info 。
2. 在啓動類上添加 @EnableEurekaServer 註解
3. 在配置文件中添加如下配置:
#設置端口號
server.port=8761
#由於該應用爲註冊中心,所以設置爲false,代表該不向註冊中心註冊自己
eureka.client.register-with-eureka=false
#由於註冊中心的職責是就是維護實例,它並不需要去檢索服務,所以也要是這隻爲false
eureka.client.fetch-registry=false
注意:eureka.client.register-with-eureka 一定要設置爲false,不然啓動的時候會 把自己當做客戶端向自己註冊,會報錯。
4. 啓動服務會看到下面的界面:
看到上面的界面就代表了成功了。
如果看到下面這個界面,並報“org.springframework.web.servlet.NoHandlerFoundException: No handler found for GET /eureka/css/wro.css”這個錯誤,需要進行下面的排查:
a. 是否在啓動類中加了 @EnableConfigServer 註解,如果加了,在配置文件中添加這個配置 spring.cloud.config.server.prefix=/config。
b. 如果沒有添加 @EnableConfigServer 註解,請在配置文件中檢查是否配置了這個 spring.resources.add-mappings=false,如果有配置註釋掉就好了。
二、編寫服務提供者
1. 添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2. 在啓動類中添加 @EnableDiscoveryClient 註解。
3. 在配置文件中添加下面的配置
spring.application.name=eureka-client-user-service
server.port=8081
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
eureka.client.serviceUrl.defaultZone 的地址就是我們之前啓動的 Eureka 服務的地址,在程序啓動的時候需要將自身的信息註冊到 Eureka 中。
4. 編寫提供接口
@GetMapping("/user/hello")
public String sayHello(){
return "hello everyOne";
}
5. 啓動應用,在 Eureka 的控制檯中看到下面的信息就證明成功啦。
注意:一定要引入下面的這個依賴 不然程序啓動後會自動停止。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
三、編寫服務消費者
1.引入的依賴和配置
我們需要編寫一個服務消費者,消費我們剛剛服務提供者的"/user/hello"的接口,其中引入的依賴和啓動類添加的註解都和服務提供者的一樣。唯一不同的就是配置文件中的配置:
spring.application.name=eureka-client-article-service
server.port=8082
eureka.client.serviceUrl.defaultZone=http://ocalhost:8761/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
2. 編寫消費的接口:
RestTemplate 是Spring提供的用於訪問Rest服務的客戶端。RestTemplate 提供了多種便捷訪問遠程Http 服務的方法,能夠大大的提高客戶端的編寫效率。我們可以通過配置 RestTemplate 來調用接口。
a. RestTemplate 配置
@Configuration
public class BeanConfiguration {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
b. 調用接口
@GetMapping("/article/callhello")
public String callHello(){
return restTemplate.getForObject("http://localhost:8081/user/hello",String.class);
}
啓動應用後通過訪問 即可 http://localhost:8082/article/callhello 即可
通過 Eureka 來消費接口
a. RestTemplate 配置
@Configuration
public class BeanConfiguration {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
b. 調用接口
@GetMapping("/article/callhello2")
public String callHello2(){
return restTemplate.getForObject("http://eureka-client-user-service/user/hello",String.class);
}
啓動應用後通過訪問 即可 http://localhost:8082/article/callhello2 即可
到此爲止服務消費也搭建成功了
四、開啓 Eureka 認證
1. 添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
2. 配置文件中加入如下的配置
spring.security.user.name=用戶名
spring.security.user.password=密碼
3. 添加Security配置類
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.authorizeRequests().
anyRequest().authenticated().and().httpBasic();
}
}
重新啓動後,訪問localhost:8761 ,看到下圖中的頁面代表成功了
同時客戶端的註冊也要加上用戶名和密碼的信息例如:
eureka.client.serviceUrl.defaultZone=http://用戶名:密碼@localhost:8761/eureka/
五、關閉自我保護:
在配置文件中添加下面的配置即可:
eureka.server.enable-self-preservation=false
六、自定義實例id
在客戶端註冊的配置文件中加入下面的配置:
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
七、自定義實例跳轉鏈接
在客戶端註冊的配置文件中加入下面的配置:
eureka.instance.status-page-url=http://localhost:8081/user/hello
在下圖所示的地方可以點擊進入: