#### 注冊中心-客戶端
- 首先引入pom依賴
```
<!-- Eureka客戶端,向注冊中心注冊服務-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
```
- 啟動類
```
~~~
package com.dg.sc.client;
import brave.sampler.Sampler;
import com.dg.sc.utils.PortUtil;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import java.util.Scanner;
//2.向注冊中心注冊服務,改變端口可注冊多次
@SpringBootApplication
@EnableEurekaClient
//@EnableDiscoveryClient//非Eureka注冊中心推薦用這個
public class EurekaClientApplication {
public static void main(String[] args) {
System.out.println("請輸入端口號, 推薦 2222 、 2223 、 2224");
Scanner scanner = new Scanner(System.in);
int port = scanner.nextInt();
scanner.close();
if (PortUtil.isUsed(port)) {
System.err.println(port + "端口已被占用");
System.exit(1);
}
new SpringApplicationBuilder(EurekaClientApplication.class).properties("server.port=" + port).run(args);
}
}
~~~
```
- 配置文件
```
~~~
#應用名稱
spring.application.name=sc-eureka-client
#Eureka 注冊中心服務器主機名稱
eureka.instance.hostname=eureka-client
#Eureka 注冊中心服務器主機端口
eureka.server.port=1111
#Eureka 注冊中心服務器默認連接地址
eureka.client.serviceUrl.defaultZone=http://192.168.0.114:8001/eureka/,http://192.168.0.114:8002/eureka/,http://192.168.0.114:8003/eureka/
~~~
```
- 啟動運行,向注冊中心查看是否注冊到了Eureka
> 這個服務注冊到了三個注冊中心上了,這樣就實現了服務的高可用

- 測試高可用,ProductController.java
```
package com.dg.sc.client.controller;
import com.dg.sc.client.service.ProductService;
import com.dg.sc.pojo.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class ProductController {
@Autowired
ProductService productService;
@RequestMapping("/products")
public Object products() {
List<Product> ps = productService.listProducts();
return ps;
}
}
```
- postman測試結果

- 現在注冊中心都在運行中

- 手動kill掉其中一個p1注冊中心進程,那么只要有一個在,就不會出現死機的情況


- 測試運行,接口依然可以運行,實現其高可用
