使用 Spring Cloud 實現微服務系統
準備工作:
為了方便創建項目,以及各版本以來關系,此次創建項目使用 Spring Assistant
插件。
創建單體服務中心項目
啟用服務端的服務注冊,發現功能
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerDemoApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerDemoApplication.class, args);}}
配置“服務中心”地址,端口號,應用名稱
# 端口
server.port=8080
# 服務名
spring.application.name=Eureka Server Demo
# 是否注冊到 Eureka Server,默認為true
eureka.client.register-with-eureka=true
# 是否從 Eureka Server 獲取注冊信息,默認為true
eureka.client.fetch-registry=true
# 設置與 Eureka Server 進行交互的地址,查詢服務和注冊服務都需要使用他。多個地址使用 "," 分割。
eureka.client.service-url.default-zone=http://localhost:8080/eureka/
gitee:https://gitee.com/Jacob-gitee/eureka/tree/master/eureka1/
實現服務集群
配置虛擬地址
# 節點1
127.0.0.1 node1
# 節點2
127.0.0.1 node2
# 節點3
127.0.0.1 node3
多環境配置
創建節點 node1 配置文件 application-node1.pproperties,并指向 node2 和 node3。
# 端口
server.port=8081
# 服務名
spring.application.name=Eureka Server Demo
# 節點名
eureka.instance.hostname=node1
# 是否注冊到 Eureka Server,默認為true
eureka.client.register-with-eureka=true
# 是否從 Eureka Server 獲取注冊信息,默認為true
eureka.client.fetch-registry=true
# 設置與 Eureka Server 進行交互的地址,查詢服務和注冊服務都需要使用他。多個地址使用 "," 分割。
eureka.client.service-url.defaultZone=http://node2:8082/eureka/,http://node3:8083/eureka/
創建節點 node2 配置文件 application-node2.pproperties,并指向 node1 和 node3。
# 端口
server.port=8082
# 服務名
spring.application.name=Eureka Server Demo
# 節點名
eureka.instance.hostname=node2
# 是否注冊到 Eureka Server,默認為true
eureka.client.register-with-eureka=true
# 是否從 Eureka Server 獲取注冊信息,默認為true
eureka.client.fetch-registry=true
# 設置與 Eureka Server 進行交互的地址,查詢服務和注冊服務都需要使用他。多個地址使用 "," 分割。
eureka.client.service-url.defaultZone=http://node1:8081/eureka/,http://node3:8083/eureka/
創建節點 node3 配置文件 application-node3.pproperties,并指向 node1 和 node2。
# 端口
server.port=8083
# 服務名
spring.application.name=Eureka Server Demo
# 節點名
eureka.instance.hostname=node3
# 是否注冊到 Eureka Server,默認為true
eureka.client.register-with-eureka=true
# 是否從 Eureka Server 獲取注冊信息,默認為true
eureka.client.fetch-registry=true
# 設置與 Eureka Server 進行交互的地址,查詢服務和注冊服務都需要使用他。多個地址使用 "," 分割。
eureka.client.service-url.defaultZone=http://node1:8081/eureka/,http://node2:8082/eureka/
打包和部署
打包
部署 服務中心 集群
在 3個DOS命令窗口分別暑促一下命令啟動服務中心:
# node1
java -jar eureka-0.0.1.jar --spring.profiles.active=node1
# node2
java -jar eureka-0.0.1.jar --spring.profiles.active=node2
# node3
java -jar eureka-0.0.1.jar --spring.profiles.active=node3
gitee:https://gitee.com/Jacob-gitee/eureka/tree/master/eureka
實現單體"服務提供者"客戶端
使用插件創建 客戶端,最后使用選擇 Eureka Discovery Client
:
添加依賴
<!-- 不加此依賴會:registration status: 204 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>
添加配置
# 服務名
spring.application.name=provider
# 應用的端口號
server.port=8000
provider.name=provider0
# "服務中心" 地址
eureka.client.service-url.defaultZone=http://node1:8081/eureka/
啟用客戶端的服務注冊,發現功能:
@EnableDiscoveryClient
@SpringBootApplication
public class EurekaServerClientApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerClientApplication.class, args);}}
使用 Eureka 啟用客戶端服務的注冊和發現功能,可以使用注解 @EnableEurekaClient 和 @EnableDiscoveryClient 來實現。如果使用其他"服務中心"(zookeeper,Consul),則使用 @EnableDiscoveryClient 來實現。@EnableEurekaClient 是 Eureka 的專用注解。
實現"服務提供者"接口
@RestController
public class HellController {@Value("${server.port}")private String port;@Value("${provider.name}")private String name;@GetMapping("hello")public String hello(){return "provider:"+ name+" port:"+port;}
}
實現"服務提供者"集群
多環境配置
創建配置文件 application-provider1.properties,并做如下配置:
# 服務名
spring.application.name=provider
# 應用的端口號
server.port=8001
# 自定義配置項
provider.name=provider1
# "服務中心" 地址
eureka.client.service-url.defaultZone=http://node1:8081/eureka/,http://node2:8082/eureka/,http://node3:8083/eureka/
創建配置文件 application-provider2.properties,并做如下配置:
# 服務名
spring.application.name=provider
# 應用的端口號
server.port=8002
# 自定義配置項
provider.name=provider2
# "服務中心" 地址
eureka.client.service-url.defaultZone=http://node1:8081/eureka/,http://node2:8082/eureka/,http://node3:8083/eureka/
打包啟動
# 打包
mvn clean package
# 啟動
java -jar eureka-clien.0.0.1.jar --spring.profiles.active=provider1
java -jar eureka-clien.0.0.1.jar --spring.profiles.active=provider2
用 Feign 實現"服務消費者"
創建 Eureka 的客戶端
添加依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency>
添加配置
# 服務名
spring.application.name=consumer
# 應用的端口號
server.port=9000
# 不注冊到"服務中心"
eureka.client.register-with-eureka=false
# "服務中心" 地址
eureka.client.service-url.defaultZone=http://node1:8081/eureka/,http://node2:8082/eureka/,http://node3:8083/eureka/
啟用客戶端的發現和遠程調用
// 啟動 feign 遠程調用服務
@EnableFeignClients
// 啟用客戶端的服務注冊,發現功能
@EnableDiscoveryClient
@SpringBootApplication
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}}
調用"服務提供者"接口
// 遠程服務名
@FeignClient(name = "provider")
public interface MyFeignClient {// 此接口需要和遠程調用接口方法名,參數保持一致@GetMapping("/hello")public String hello();}
實現"客戶端"接口
@RestController
public class FeignController {@Autowiredprivate MyFeignClient myFeignClient;@GetMapping("hello")public String hello(){return myFeignClient.hello();}}