Spring Cloud爲開發人員提供了快速構建分佈式系統的工具(例如:配置管理,服務發現,斷路器,智能路由,微代理,控制總線,一次性令牌,全局鎖,領導選舉,分佈式會話,集羣狀態等等)。 開發人員能夠用Spring Cloud快速搭建具備以上功能的應用程序。 它們能夠在任何分佈式環境中正常工做,包括開發人員本身的筆記本電腦,裸機數據中心以及Cloud Foundry等託管平臺。java
咱們建立一個工程來做爲服務註冊中心,下面詳細說一下建立過程:git
在IDEA新建一個項目->選擇spring initialir 以下圖: web
點擊next->填寫項目metadata信息:spring
點擊next->選擇Spring Cloud Discovery->右側選擇Eureka Server, 而後點擊下一步知道項目建立成功:apache
建立完成後,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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.noodles.mars</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
依賴spring-cloud-starter-netflix-eureka-server
包後,啓用一個服務註冊中心很簡單,只要在啓動類上註解@EnableEurekaServer
就能夠了:瀏覽器
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
Eureka是一個高可用的組件,它是沒有後端緩存組件的,每個實例註冊後須要向服務註冊中心發送心跳,默認狀況下,一個Eureka server同時也是一個client, 必須指定一個server。 服務註冊中心的配置文件:緩存
server: port: 9090 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:9090/eureka
啓動註冊中心,打開瀏覽器訪問: http://localhost:9090, 會看到以下界面,此時註冊列表是空的:app
項目建立步驟和建立註冊中心同樣,只是在選擇Spring Cloud Discovery時選擇Eureka Discovery Client:maven
服務提供者項目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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.noodles.mars</groupId> <artifactId>eureka-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-client</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> <spring-cloud.version>Greenwich.SR3</spring-cloud.version> </properties> <dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
開啓註冊服務提供也很簡單,首先要在Spring boot啓動類上註解@EnableEurekaClient
,代表本身是Eureka Client:
@EnableEurekaClient @SpringBootApplication public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
而後配置服務註冊中心信息:
server: port: 8040 eureka: client: service-url: defaultZone: http://localhost:9090/eureka/ spring: application: name: hello-erueka-client
注意:這裏須要指明spring.application.name
, 應用名稱將在隨後的服務間調用中用到。
在這裏提供一個接口,隨後測試調用:
@RestController public class HelloController { @Value("${server.port}") int port; @GetMapping("/hello") public String hello(@RequestParam("name") String name) { return String.format("Hello, My name is %s, I'm from port: %d", name, port); } }
啓動項目,打開瀏覽器輸入http://localhost:9090, 打開註冊中心的網址:
打開網頁後你會發現你的服務提供者已經註冊在服務註冊中心了。
使用Postman或直接在瀏覽器中訪問服務提供者的接口,你會看到迴應:
Hello, My name is Mars, I'm from port: 8040
關注公衆號:JAVA九點半課堂,回覆【資料】獲取1T最新技術資料!