一、沒有註冊中心的模擬微服務
這裏有四個項目,其中三個是服務提供者,這三個服務提供者通過@RestController暴露rest接口,供消費者去調用。消費者可以用java代碼編寫get或者post請求,我們通常用的是RestTmplate,這個spring爲我們提供好的類,特別好用!正常情況下,我們就可以很順利的調用這些服務了。這個是沒有問題的!
小夥伴們可能要問,這能調用。我們直接搞就行了,爲啥還用註冊中心呢?多此一舉?
我們來考慮這樣幾個問題:(1)在我們調用提供者的接口的時候,是不是要將他們的url編寫到消費者代碼中呢,比如
restTemplate.getForObject("http://192.128.0.1:9002/product/1", Product.class);
如果服務換ip了,換端口了,腫麼辦?
(2)爲了實現系統的高可用,我們部署同一服務的多個服務提供者怎麼辦?也就是說提供服務A的有兩個系統,你又怎麼達到負載均衡?
這兩個問題沒有很好的辦法解決吧?
二、註冊中心
當服務提供者啓動的時候,會像註冊中心註冊一個信息,“hello 註冊中心,幫我註冊一下好嗎,我的ip是...,我要註冊的服務是...”。這個時候註冊中心就有了服務提供者的信息,當消費者啓動的時候也會向註冊中心註冊信息,指定註冊的要調用的服務。同時拿到服務的信息,消費者拿到對應的信息之後,存了一個小小的備份,當調用的時候,直接調用服務。就是圖中最黑的那根線。
三、Eureka註冊中心
四、搭建註冊中心
1、創建一個父工程
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.springcloud.demo</groupId>
<artifactId>SpringCloudDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>order_service</module>
<module>product_service</module>
<module>eureka_server</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、創建子工程eureka_server子工程
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>SpringCloudDemo</artifactId>
<groupId>com.springcloud.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka_server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
配置文件
#模擬兩個EurekaServer
#端口9000 , 8000
#兩個server需要相互註冊
spring:
application:
name: eureka-server
server:
port: 9000 #端口
#配置eureka server
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false #是否將自己註冊到註冊中心
fetch-registry: false #是否從eureka中獲取註冊信息
service-url: #配置暴露給Eureka Client的請求地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
啓動類
package com.springcloud.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @author Shubo Dai
* @description 註冊中心啓動類
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
啓動註冊中心!
藥藥切克鬧,是不是特別簡單?
五、註冊服務到註冊中心
再新創建一個 項目,叫做product_service。這裏我之前是用的jpa查詢的數據庫,這塊的具體的信息我就不再展示了。可以去了解一下jpa好吧。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>SpringCloudDemo</artifactId>
<groupId>com.springcloud.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>product_service</artifactId>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--引入EurekaClient-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
server:
port: 9011 #端口
spring:
application:
name: service-product #服務名稱
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
username: root
password:
jpa:
database: MySQL
show-sql: true
open-in-view: true
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/ #多個eurekaserver之間用,隔開
instance:
prefer-ip-address: true #使用ip地址註冊
eureka配置信息,這裏面指定註冊中心的地址和使用什麼進行註冊。
package com.springcloud.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* @author Shubo Dai
* @description 提供者服務類,@EnableEurekaClient、@EnableDiscoveryClient標識激活eureka客戶端
*/
@SpringBootApplication
@EntityScan("com.springcloud.demo.entity")
@EnableEurekaClient
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class,args);
}
}
啓動之後,顯示註冊成功
這裏實際上還有dao、service、controller,可以自己想怎麼寫怎麼寫,這裏不展開了!!!!
六、消費者通過註冊中心調用提供者
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>SpringCloudDemo</artifactId>
<groupId>com.springcloud.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order_service</artifactId>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--引入EurekaClient-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
server:
port: 9002 #端口
spring:
cloud:
loadbalancer:
retry:
enabled: true # 開啓Spring Cloud的重試功能
application:
name: service-order #服務名稱
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
username: root
password: 111111
jpa:
database: MySQL
show-sql: true
open-in-view: true
#配置Eureka
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/
instance:
prefer-ip-address: true #使用ip地址註冊
package com.springcloud.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @ClassName OrderApplication
* @Description
* @Author 戴書博
* @Date 2020/5/21 10:23
* @Version 1.0
**/
@SpringBootApplication
@EntityScan("com.springcloud.demo.entity")
public class OrderApplication {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
package com.springcloud.demo.controller;
import com.springcloud.demo.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
public Product findById(@PathVariable Long id) {
List<ServiceInstance> list = discoveryClient.getInstances("service-product");
ServiceInstance serviceInstance = list.get(0);
Product product = restTemplate.getForObject("http://"+serviceInstance.getHost()+":"
+serviceInstance.getPort()
+"/product/1",Product.class);
return product;
}
}
啓動服務試一下。
註冊中心
可以調用成功!!!
七、註冊中心的高可用
如果我們只有一個註冊中心的話,那麼這個註冊中心宕機了,怎麼辦?爲了保證高可用,那麼我們可以搞成一個eureka集羣。
在註冊中心一塊我們只需要修改配置文件就可以了,比如兩個註冊中心的端口:8000、9000。需要相互獲取信息、將自己註冊到對應的註冊中心中。
8000端口的配置文件:
#模擬兩個EurekaServer
#端口9000 , 8000
#兩個server需要相互註冊
spring:
application:
name: eureka-server
server:
port: 9000 #端口
#配置eureka server
eureka:
client:
service-url: #配置暴露給Eureka Client的請求地址
defaultZone: http://127.0.0.1:8000/eureka/
9000端口的配置文件:
#模擬兩個EurekaServer
#端口9000 , 8000
#兩個server需要相互註冊
spring:
application:
name: eureka-server
server:
port: 8000 #端口
#配置eureka server
eureka:
client:
service-url: #配置暴露給Eureka Client的請求地址
defaultZone: http://127.0.0.1:9000/eureka/
服務註冊到多個註冊中心
實際上由於兩個註冊中心之間相互都是交換信息的,所以在一個註冊上,另一個也會同步。當然我們也可以配置兩個都註冊。
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/,http://localhost:8000/eureka/
完善配置
eureka:
client:
service-url:
defaultZone: http://localhost:9000/eureka/ #多個eurekaserver之間用,隔開
instance:
prefer-ip-address: true #使用ip地址註冊
instance-id: ${spring.cloud.client.ip-address}:${server.port} #向註冊中心中註冊服務id
lease-renewal-interval-in-seconds: 5 #發送心跳的間隔
lease-expiration-duration-in-seconds: 10 #續約到期的時間
我們在服務提供者設置下面三行配置。
第一個是在註冊中心配置一個名稱ip+端口號。
第二個是發送心跳的時間默認是30s。
第三個是默認的宕機時間,到了這個時間提供者未向註冊中心發送心跳就會認爲提供者宕機。
關閉自我保護機制
在eureka內部存在一個自我保護機制,這個機制會統計其他提供者發送心跳的次數和認爲宕機的次數。如果發現打部門提供者的心跳都結束了,那麼就不再剔除服務。一般測試階段關閉這個自我保護機制。
eureka:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enable-self-preservation: false #關閉自我保護
eviction-interval-timer-in-ms: 4000 #剔除服務間隔
最後一個配置是4s執行一次定時任務剔除已經宕機的提供者服務。