目錄
1. 單機 Eureka 構建
1.1 創建 EurekaServer 服務註冊中心
a. 建 moudle
b. 改 pom
<dependencies>
<!--eureka server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--自定義通用包-->
<dependency>
<groupId>com.zth.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--boot web actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--一般通用配置-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
c. 寫 YML
server:
port: 7001
eureka:
instance:
hostname: localhost # eureka 服務端的示例名稱
client:
# false :不向註冊中心註冊自己
register-with-eureka: false
# false:表示自己就是註冊中心,職責是維護服務實例,不需要去檢索服務
fetch-registry: false
# 設置 eureka 服務地址
service-url:
defaultZoon: http://${eureka.instance.hostname}:${server.port}/eureka/
d. 主啓動
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args){
SpringApplication.run(EurekaMain7001.class,args);
}
}
e. 測試
1.2 Eureka 註冊服務提供者
a. 改 pom
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
b. 寫 YML
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
c. 主啓動
添加 @EnableEurekaClient
@EnableEurekaClient
@SpringBootApplication
public class PaymentMain8001 {
public static void main(String[] args){
SpringApplication.run(PaymentMain8001.class,args);
}
}
d. 測試
先啓動 EurekaServer
【注】服務提供者和服務消費者入駐 Eureka 一致。
2. Eureka 集羣
2.1 創建兩個 EurekaServer
cloud-eureka-server7001
cloud-eureka-server7002
2.2 修改映射文件
127.0.0.1 eureka7001.com
127.0.0.1 eureka7002.com
2.3 寫 YML
cloud-eureka-server7001:
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com # eureka 服務端的示例名稱
client:
# false :不向註冊中心註冊自己
register-with-eureka: false
# false:表示自己就是註冊中心,職責是維護服務實例,不需要去檢索服務
fetch-registry: false
# 設置 eureka 服務地址
service-url:
defaultZone: http://eureka7002.com:7002/eureka/
cloud-eureka-server7002:
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com # eureka 服務端的示例名稱
client:
# false :不向註冊中心註冊自己
register-with-eureka: false
# false:表示自己就是註冊中心,職責是維護服務實例,不需要去檢索服務
fetch-registry: false
# 設置 eureka 服務地址
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
2.4 服務提供者發佈到集羣
服務提供者 cloud-payment-server8001 YML:
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
2.5 測試
3. 服務提供者集羣
兩個服務提供者:
cloud-provider-payment8001
cloud-provider-payment8002
YML 配置:
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 當前數據源操作類型
driver-class-name: org.gjt.mm.mysql.Driver # mysql 驅動包
url: jdbc:mysql://localhost:3306/mycloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: mysql
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
兩個服務提供者的 application.name 一致,則認爲是同一個服務提供者。
即:將提供相同服務的不同的服務提供者 application.name 保持一致,並註冊到 Erueka,就完成了服務提供者集羣。
4. 服務消費者獲取提供者服務
4.1 修改服務地址
@RestController
@Slf4j
public class OrderController {
/**
* public static final String PAYMENT_URL = "http://localhost:8001";
*/
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult<Payment> getPayment(@PathVariable("id") long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
}
}
4.2 RestTemplate 添加負載均衡
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
5. 完善 actuator 微服務信息
5.1 修改服務名稱
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001
prefer-ip-address: true
instance-id: payment8001:修改服務名稱
prefer-ip-address: true:訪問信息是否提示 ip
5.2 添加微服務 info
a. 添加 pom
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
b. 配置 YML
info:
groupId: "@groupId@"
artifactId: "@artifactId@"
version: "@version@"
principal: 張三
phone: 110
c. 查看 info
6. 服務發現 Discovery
可以通過服務發現獲得註冊進 eureka 裏面微服務的信息。
6.1 修改 controller
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping(value = "/payment/discovery")
public Object discovery(){
List<String> services = discoveryClient.getServices();
for (String element : services) {
log.info("element:"+element);
}
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info(instance.getServiceId()+"\t"+instance.getHost()+"\t"+instance.getPort()+"\t"+instance.getUri());
}
return this.discoveryClient;
}
【注】import org.springframework.cloud.client.discovery.DiscoveryClient;
6.2 修改啓動類
啓動類添加 @EnableDiscoveryClient
6.3 測試結果
7. Erueka 自我保護
7.1 自我保護機制
爲了防止 EurekaClient 可以正常運行但與 EurekaServer 網絡不通情況下,EurekaServer 立刻剔除 EurekaClient。
默認情況下,如果 EurekaServer 在一 定時間內沒有接收到某個微服務實例的心跳,EurekaServer 將會註銷該實例 (默認90秒)
7.2 禁止自我保護
EurekaServer 端 YML:
eureka.server.enable-self-preservation=false 關閉自我保護
eureka:
instance:
hostname: eureka7001.com # eureka 服務端的示例名稱
client:
# false :不向註冊中心註冊自己
register-with-eureka: false
# false:表示自己就是註冊中心,職責是維護服務實例,不需要去檢索服務
fetch-registry: false
# 設置 eureka 服務地址
service-url:
defaultZone: http://eureka7002.com:7002/eureka/
server:
enable-self-preservation: false
EurekaClient 端:
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001
prefer-ip-address: true
# Eureka 客戶端向服務端發送心跳的時間間隔,單位爲秒(默認是30秒)
lease-expiration-duration-in-seconds: 1
# Eureka 服務端在收到最後一次心跳後等待時間上限, 單位爲秒(默認是90秒),超時將剔除服務
lease-renewal-interval-in-seconds: 3