上記の記事では、登録センターの構築方法を既に書いています。登録センターを持つことは十分ではないため、登録センターに登録してサービスを提供する必要があります。これは登録サービスプロバイダーと呼ばれます。
前提
上記を読んで、環境を変更せずに登録センターを正常に構築する
プロジェクト構築
ここでは、新しいMavenプロジェクトを作成する必要があります。プロジェクト名は以前に提起されていません。ここでそれを参照させてください。私はSpringClouddemoです、これらの詳細を気にしないでください!
POMファイルを変更するには、次を参照してください。
注:これらのJARパッケージのバージョン番号をご覧ください。記事の最後に、以前に作成した2つの単純なデモのgithubパスを投稿します。
<?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 <http://www.w3.org/2001 XSI:Schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.0.sdsd"> <modeleversion> 4.0.0 </modelversion> <グループcom.hellxz </groupid> <artifactid> springclouddemo </artifactid> <バージョン> 0.0.1-snapshot </version> <packaging> jar </packaging> <name> springclouddemo </name> <説明> spring boot </description> <parent> <parent> groupid> org.springframework <artifactid> spring-boot-starter-parent </artifactid> <バージョン> 1.5.9.release </version> <relativepath/> <! - lookup parent from repository-> </parent> <dependencymanagement> <dependencies> <バージョン> camden.sr3 </version> <type> pom </type> <scope>インポート</scope> </dependency> </dependency> </dependency> </dependencymanagement> <properties> <properties> <propert.build.sourceencoding <project.reporting.outputencoding> utf-8 </project.reporting.outputencoding> <java.version> 1.8 </java.version> </properties> <dependencies> <shipencies> <dependency> <Dependency> groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter-test </artifactid> <scope>テスト</scope> </dependency> <! - プロジェクトの監視とステータス情報を提供するために使用される - > <依存関係> <groupid> <artifactid> spring-boot-starter-actuator </artifactid> </dependency> <! - junit test-> <dependency> <groupid> junit </groupid> <artifactid> junit </artifactid> <バージョン> 4.8.2 </バージョン> </依存関係> < <artifactid> spring-cloud-starter-eureka </artifactid> </dependency> <dependency> sgroupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-config-server </artifactid> </dependency> </dependency> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-maven-plugin </artifactid> </plugin> <plugin> <groupid> org.apache.maven.plugins </groupId> <artifactid> maven-compiler-plugin </artifactid> </artifactid> <Target> 1.8 </Target> </configuration> </plugin> </plugins> </build> </project>
バージョン番号はeurekaserver登録センタープロジェクトとは異なりますが、通常は実践を通じて使用できます。安心してください
新しいスタートアップクラスを作成します(すべてのスプリングブートプロジェクトで)
パッケージcom.hellxz.springcloudhelloworld; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplication; Import org.springframework.cloud.clud.discovery.discoveryclicclient;/ * * * * * * * * * * * * * * * * * * * * * * * @dedcoveryclient。 eurekaclient * @date:2018/4/13 16:57 */@enablediscoveryclient@springbootapplicice class springclouddemoapplication {public static void main(string [] args){springclouddemoapplication.class、args); }}新しいコントローラークラスを作成し、後でテストするために残します
パッケージcom.hellxz.springcloudhelloworld; Import org.apache.log4j.logger; import org.springframework.beans.Annotation.Autowired; import org.springframework.cloud.client.serviceinstance; Import org.springframework.cloud.cloud.cloud.cloud.cloud.discobery.discobery springframework.web.bind.annotation.requestmapping; Import org.springframework.web.bind.annotation.requestmethod; import org.springframework.web.bind.annotation.restcontroller;/*** @author:hellxz* @descripation:@descriput 11:36 */ @strestcontrollerpublicクラスSpringBootController {@Autowired Private DiscoveryClientクライアント; //ディスカバリークライアントプライベート最終ロガー= logger.getLogger(springbootcontroller.class); @RequestMapping(value = "/hello"、method = requestmethod.get)public string hello(){//サービスインスタンスを取得します。関数は、コンソールserviceinstance instance intance = client.getLocalserviceInstance()の効果を表示することです。 logger.info( "/hello host:"+serviceinstance.gethost()+"service_id:"+serviceinstance.getServiceid()); 「こんにちは」を返します。 }}SRC/ResourcesフォルダーにApplication.ymlを作成します。今回は、構成にYAMLを使用します。プロパティファイルメソッドを試してみたい場合は、上記を参照してください。ここで構成されているサービスアドレスの登録センターの構成を参照してください。
サーバー:ポート:8080Spring:アプリケーション:名前:hello-serviceeureka:client:serviceurl:defaultzone:http:// localhost:1111/eureka/
OK、このプロジェクトをポート8080で実行し、登録センターにアクセスしてサービスに登録できます。
最初に登録センターでプロジェクトを開始し、開始後、プロジェクトを開始します。
テスト
表示するには登録センターのURLを入力してください:localhost:1111
構成したばかりのコントローラーパスにアクセス:http:// localhost:8080/hello
右側の写真に示されているように、登録は成功しています。
現時点では、このプロジェクトを使用してサービスを提供できます
デモの例:
https://github.com/hellxz/eurekaserver
https://github.com/hellxz/eurekaclient
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。