Spring Cloud(一) — 服務註冊中心-註冊服務
-
SpringClod簡介
首先,pring Cloud是基於Spring Boot的, 最適合用於管理Spring Boot建立的各個微服務應用。
Spring提供了一系列工具,可以幫助開發人員迅速搭建分散式系統中的公共元件(比如:配置管理,服務發現,斷路器,智慧路由,微代理,控制匯流排,一次性令牌,全域性鎖,主節點選舉, 分散式session, 叢集狀態)。協調分散式環境中各個系統,為各類服務提供模板性配置。使用Spring Cloud, 開發人員可以搭建實現了這些樣板的應用,並且在任何分散式環境下都能工作得非常好,小到膝上型電腦, 大到資料中心和雲平臺。
Spring Cloud使用erureka server, 然後所有需要訪問配置檔案的應用都作為一個erureka client註冊上去。eureka是一個高可用的元件,它沒有後端快取,每一個例項註冊之後需要向註冊中心傳送心跳,在預設情況下erureka server也是一個eureka client ,必須要指定一個 server。 -
建立一個pom型別的maven工程 作為父目錄(com-springcloud-demo-all):
pom.xml檔案:
<?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>com-springcloud-demo-all</artifactId> <version>1.0-SNAPSHOT</version> <!--<modules>--> <!--<module>springcloud-eurekaService</module>--> <!--<module>springcloud-eurekaclient-A</module>--> <!--<module>spring-cloud-config-server</module>--> <!--<module>spring-cloud-config-client</module>--> <!--<module>spring-cloud-service-ribbon</module>--> <!--<module>springcloud-eurekaclient-b</module>--> <!--<module>spring-cloud-service-feign</module>--> <!--<module>spring-cloud-monitor-turbine</module>--> <!--<module>spring-cloud-service-zuul</module>--> <!--<module>spring-cloud-config-client-two</module>--> <!--</modules>--> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR5</version> <type>pom</type> <scope>import</scope> <exclusions> </exclusions> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
- 建立Eureka Server
1.在父目錄下建立一個eureka server的maven的專案 springcloud-eurekaService pom.xml配置檔案如下:
<?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>com-springcloud-demo-all</artifactId> <groupId>com.springcloud.demo</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-eurekaService</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.在resources文加下建立配置檔案application.yml:
server:
port: 8761
spring:
profiles:
active: dev
eureka:
server:
enable-self-preservation: false
instance:
preferIpAddress: true
hostname: ${spring.cloud.client.ipAddress}
instanceId: ${spring.cloud.client.ipAddress}:${server.port}
client:
registerWithEureka: false # 其中registerWithEureka:false和fetchRegistry:false 表明自己是一個eureka server
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
建立啟動類ServerApplication:
@SpringBootApplication
@EnableEurekaServer
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
}
4.專案介面目錄如下:
5.啟動eureka server,然後訪問http://localhost:8761, 介面如下, “No instances available” 表示無client註冊
- 建立Eureka Client
1.建立一個Maven工程springcloud-eurekaclient-a, pom.xml內容如下:
<?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>com-springcloud-demo-all</artifactId>
<groupId>com.springcloud.demo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-eurekaclient-a</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.建立主類EurekaClientApplication
使用@EnableEurekaClient註解表明是client
package com.eurekaservice.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
contrller類:
package com.eurekaservice.demo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${server.port}")
private int port;
@RequestMapping(value = "/index")
public String index(){
return "Hello World!,埠:"+port;
}
}
3.eureka client的配置檔案appication.yml
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
instance:
lease-renewal-interval-in-seconds: 1
appname: eurekaclient-helloworld
# leaseRenewalIntervalInSeconds: 5 #客戶端每隔n秒向服務端傳送資料包
# leaseExpirationDurationInSeconds: 10 #客戶端告知服務端:若在n秒內沒有向伺服器傳送資訊,則服務端將其從服務列表中刪除
server:
port: 8762
spring:
application:
name: eurekaclient-helloworld
#配置日誌級別
logging:
level:
com.netflix: DEBUG
4.Client啟動後, 可以訪問http://localhost:8762
5.再次訪問伺服器埠, 可以看到Service Helloworld已經自動註冊到之前的server中
這樣就完成了 服務向服務中心的註冊。