javascript
SpringCloud 入门教程(一): 服务注册
1. ?什么是Spring Cloud?
Spring提供了一系列工具,可以幫助開(kāi)發(fā)人員迅速搭建分布式系統(tǒng)中的公共組件(比如:配置管理,服務(wù)發(fā)現(xiàn),斷路器,智能路由,微代理,控制總線,一次性令牌,全局鎖,主節(jié)點(diǎn)選舉, 分布式session, 集群狀態(tài))。協(xié)調(diào)分布式環(huán)境中各個(gè)系統(tǒng),為各類服務(wù)提供模板性配置。使用Spring Cloud, 開(kāi)發(fā)人員可以搭建實(shí)現(xiàn)了這些樣板的應(yīng)用,并且在任何分布式環(huán)境下都能工作得非常好,小到筆記本電腦, 大到數(shù)據(jù)中心和云平臺(tái)。
Spring Cloud官網(wǎng)的定義比較抽象,我們可以從簡(jiǎn)單的東西開(kāi)始。Spring Cloud是基于Spring Boot的, 最適合用于管理Spring Boot創(chuàng)建的各個(gè)微服務(wù)應(yīng)用。要管理分布式環(huán)境下的各個(gè)Spring Boot微服務(wù),必然存在服務(wù)的注冊(cè)問(wèn)題。所以我們先從服務(wù)的注冊(cè)談起。既然是注冊(cè),必然有個(gè)管理注冊(cè)中心的服務(wù)器,各個(gè)在Spring Cloud管理下的Spring Boot應(yīng)用就是需要注冊(cè)的client
Spring Cloud使用erureka server, ?然后所有需要訪問(wèn)配置文件的應(yīng)用都作為一個(gè)erureka client注冊(cè)上去。eureka是一個(gè)高可用的組件,它沒(méi)有后端緩存,每一個(gè)實(shí)例注冊(cè)之后需要向注冊(cè)中心發(fā)送心跳,在默認(rèn)情況下erureka server也是一個(gè)eureka client ,必須要指定一個(gè) server。
2. ?創(chuàng)建Eureka Server
1).創(chuàng)建一個(gè)Maven工程helloworld.eureka.server, pom.xml內(nèi)容如下:
<?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.chry</groupId><artifactId>springcloud.helloworld.eureka.server</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>springcloud.helloworld.Eureka.server</name><description>Demo Spring Eureka Server</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></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><!--eureka server --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency><!-- <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency>--><!-- spring boot test--><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>Dalston.RC1</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><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>?
?2). 用Spring Boot創(chuàng)建一個(gè)服務(wù)類EurekaServerApplication,需要一個(gè)注解@EnableEurekaServer加在springboot工程的啟動(dòng)類上
package springcloud.helloworld.eureka.server;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}}?
3).eureka server的配置文件appication.yml,其中registerWithEureka:false和fetchRegistry:false表明自己是一個(gè)eureka server
server:port: 8761eureka:instance:hostname: localhostclient:registerWithEureka: falsefetchRegistry: falseserviceUrl:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/?
4) eureka server的工程結(jié)構(gòu)如下
5)啟動(dòng)eureka server,然后訪問(wèn)http://localhost:8761, 界面如下, "No instances available" 表示無(wú)client注冊(cè)
?
3. ?創(chuàng)建Eureka Client
1).?創(chuàng)建一個(gè)Maven工程helloworld.eureka.client, pom.xml內(nèi)容如下:
<?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.chry</groupId><artifactId>springcloud.helloworld.eureka.client</artifactId><version>0.0.1-SNAPSHOT</version><name>springcloud.helloworld.eureka.client</name><packaging>jar</packaging><description>Demo Spring Boot Client</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></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.cloud</groupId><artifactId>spring-cloud-starter-eureka</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>Dalston.RC1</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><repositories><repository><id>spring-milestones</id><name>Spring Milestones</name><url>https://repo.spring.io/milestone</url><snapshots><enabled>false</enabled></snapshots></repository></repositories></project>2). ?創(chuàng)建主類EurekaClientApplication
使用@EnableEurekaClient注解表明是client
1 package springcloud.helloworld.eureka.client;2 3 import org.springframework.beans.factory.annotation.Value;4 import org.springframework.boot.SpringApplication;5 import org.springframework.boot.autoconfigure.SpringBootApplication;6 import org.springframework.cloud.netflix.eureka.EnableEurekaClient;7 import org.springframework.web.bind.annotation.RequestMapping;8 import org.springframework.web.bind.annotation.RequestParam;9 import org.springframework.web.bind.annotation.RestController; 10 11 @SpringBootApplication 12 @EnableEurekaClient 13 @RestController 14 15 public class EurekaClientApplication { 16 17 public static void main(String[] args) { 18 SpringApplication.run(EurekaClientApplication.class, args); 19 } 20 21 @Value("${server.port}") 22 String port; 23 @RequestMapping("/") 24 public String home() { 25 return "hello world from port " + port;26 } 27 28 }?
3) eureka client的配置文件appication.yml
1 eureka: 2 client: 3 serviceUrl: 4 defaultZone: http://localhost:8761/eureka/ 5 server: 6 port: 8762 7 spring: 8 application: 9 name: service-helloworld?
4). Client啟動(dòng)后, 可以訪問(wèn)http://localhost:8762
?
5). 再次訪問(wèn)服務(wù)器端口, 可以看到Service Helloworld已經(jīng)自動(dòng)注冊(cè)到之前的server中
?
?
- 參考資料:
- Spring Cloud 官網(wǎng)
- 博客: http://blog.csdn.net/forezp/article/details/70148833
- 博客:https://www.cnblogs.com/chry/p/7248947.html
?
總結(jié)
以上是生活随笔為你收集整理的SpringCloud 入门教程(一): 服务注册的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: hdu4857
- 下一篇: Spring 3 MVC深入研究