、春のクラウド導入
フィンチリーへの更新の春の雲の現在のバージョンは、上記のバージョンspringbott2.0をサポート。特定のバージョンは、次の表を参照することができます。
クラウドコード | ブートバージョン(電車) | ブートバージョン(テスト) | ライフサイクル |
---|---|---|---|
角度 | 1.2.xの | 1.3と互換性がありません | 2017年7月でEOL |
ブリクストン | 1.3.xの | 1.4.xの | 2017-07卒 |
カムデン | 1.4.xの | 1.5.xの | - |
Dalston | 1.5.xの | 期待できない2.xの | - |
エッジウェア | 1.5.xの | 期待できない2.xの | - |
フィンチリー | 2.xの | 期待できない1.5.xの | - |
公式文書リンクのフィンチリーバージョン:
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html
第二に、レジストリサービスを作成し
、ここで、私はまだ領事記事の詳細の後になるように、サービスの登録と発見としてコンポーネントユーレカを使用しています。
2.1は、まずMavenのメインプロジェクトを作成します。
まず、そのポンポンファイルの導入に依存マスタMavenプロジェクト、春のブートバージョン2.0.3.RELEASE、春のクラウドバージョンFinchley.RELEASEを作成します。ファイルとして親ポンポンポンポンファイル、依存バージョン管理の役割を果たし、他のモジュールの作品は、POMを継承します。記事のこのシリーズは、すべてこのモデルを採用し、ポンポンポンポンこのような他の記事。もう一度説明し、導入を繰り返されていません。コードは以下の通りであります:
1の<?xml version = "1.0"エンコード= "UTF-8"?> 2 <プロジェクトのxmlns = "http://maven.apache.org/POM/4.0.0" 3 のxmlns:XSI = "のhttp:// www.w3.org/2001/XMLSchema-instance " 4 のxsi:schemaLocationの=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0 .xsd "> 5 <modelVersion> 4.0.0 </ modelVersion> 6 7 <のgroupId> com.liu </ groupIdを> 8 <たartifactId> SpringCloudLearn01 </たartifactId> 9 <バージョン> 1.0-SNAPSHOT </バージョン> 10 11 <名前> SC-F-第1章</名前> 12 <説明>デモプロジェクトのための春ブーツ</記述> 13 14 <親> 15 <のgroupId> org.springframework.boot </ groupIdを> 16 <たartifactId>ばねブートスタータ親</たartifactId> 17 <バージョン> 2.0.3.RELEASE </バージョン> 18 <relativePath / > 19 </親> 20 21 <モジュール> 22 <モジュール>ユーレカサーバ</モジュール> 23 <モジュール>サービスクライアント</モジュール> 24 <モジュール> serice-ふり</モジュール> 25 </モジュール> 26 27 <プロパティ> 28 <project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding> 29 <project.reporting.outputEncoding> UTF-8 </project.reporting.outputEncoding> 30 <のjava.version> 1.8 </java.version> 31 <バネcloud.version> Finchley.RELEASE </spring-cloud.version> 32 </プロパティ> 33 34 <依存性> 35 <依存性> 36 <のgroupId> org.springframework.boot </ groupIdを> 37 <たartifactId>ばねブートスタータ試験</たartifactId> 38 <スコープ>テスト</スコープ> 39 </依存> 40 </依存関係> 41 42 <dependencyManagement> 43 <依存性> 44 <依存性> 45 <のgroupId> org.springframework.cloud </ groupIdを> 46 <たartifactId>ばねクラウド依存性</たartifactId> 47 <バージョン> $ {スプリング-cloud.version} </バージョン> 48 <タイプ> POM </タイプ> 49 <スコープ> インポート </範囲> 50 </依存> 51 </依存関係> 52 < / dependencyManagement> 53 54 <ビルド> 55 <プラグイン> 56 <プラグイン> 57 <のgroupId> org.springframework.boot </ groupIdを> 58 <たartifactId>ばねブート達人-プラグイン</たartifactId> 59 </プラグイン> 60 </プラグイン> 61 </ビルド> 62 63 </プロジェクト>
ユーレカクライアントとしてサービスレジストリなど**モデル事業、すなわちユーレカサーバー、およびその他:2.2は、次に2つのモデルプロジェクトを作成します。
以下、一例として詳細な説明を作成するプロセスをサーバーを作成します。
右プロジェクト - >以下initialir>セレクトスプリングモデル - を作成します。
次のステップ - >を選択し、クラウドディスカバリー>ユーレカサーバ、その後、次のリストになって。
プロジェクトが作成されたら、それは親ポンポンファイルの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> <parent> <groupId>com.liu</groupId> <artifactId>SpringCloudLearn01</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.liu.example</groupId> <artifactId>config-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>config-client</name> <description>Demo project for Spring Boot</description> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
2.3 启动一个服务注册中心,只需要一个注解@EnableEurekaServer,这个注解需要在springboot工程的启动application类上加:
package com.liu.example.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
**2.4 **eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml:
server: port: 8761 #通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server. eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ spring: application: name: eurka-server
2.5 eureka server 是有界面的,启动工程,打开浏览器访问:
http://localhost:8761 ,界面如下:
No application available 没有服务被发现 ……_
因为没有注册服务当然不可能有服务被发现了。
三、创建一个服务提供者 (eureka client)
当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。
创建过程同server类似,创建完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> <parent> <groupId>com.liu</groupId> <artifactId>SpringCloudLearn01</artifactId> <version>1.0-SNAPSHOT</version> </parent> <groupId>com.liu.example</groupId> <artifactId>service-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>service-client</name> <description>Demo project for Spring Boot</description> <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> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
通过注解@EnableEurekaClient 表明自己是一个eurekaclient.
package com.liu.example.serviceclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class ServiceClientApplication { public static void main(String[] args) { SpringApplication.run(ServiceClientApplication.class, args); } }
新建service类型写上一个方法:
package com.liu.example.serviceclient.service; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.Mapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class helloSevice { @Value("${server.port}") String port; @RequestMapping("/hi") public String getHello(){ return "hello! 我的端口号是:" + port; } }
仅仅@EnableEurekaClient是不够的,还需要在配置文件中注明自己的服务注册中心的地址,application.yml配置文件如下:
server: port: 8762 spring: application: name: service-client eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
需要指明spring.application.name,这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 。
启动工程,打开http://localhost:8761 ,即eureka server 的网址:
你会发现一个服务已经注册在服务中了,服务名为SERVICE-HI ,端口为7862
这时打开 http://localhost:8762/hi?name=forezp ,你会在浏览器上看到 :