Spring Cloud Alibaba:Nacosを使用してサービスの登録と発見を実現する

添付:Nacosのドキュメント

添付:[github] nacosダウンロードアドレス

ps:nacosインストールパッケージをgithubから直接ダウンロードすると、特に時間がかかります。オンラインのホスト変更方法が使用されており、効果がありません。VPNを使用するか、誰かがダウンロードしたものを使用することをお勧めします。

添付:[csdn] nacos-server-1.1.0.tar.gz

tar -zxvf nacos-server-1.1.0.tar.gz  解压
cd nacos
cd bin
sh startup.sh -m standalone 启动

次の図は、正常な起動を示しています。 

ログインhttp://192.168.101.16:8848/nacos /index.htmlがして、アカウントのパスワードを入力し、両方のナコス次のインターフェイスが表示されます。 

注:バージョンは以下に対応します。

プロジェクトの構造:

1.モジュール:alibaba-nacos-discovery-server:

コントロール層:

package com.lucifer.demo.controller;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
 * @author: lucifer
 * @date: 2019/8/6
 * @description:
 */
@Slf4j
@RestController
public class TestController {

    @GetMapping("/hello/{name}")
    public String hello(@PathVariable(name = "name") String name) {
        log.info("invoked name = " + name);
        return "hello " + name;
    }


}

springbootスタートアップクラス:(ps:@EnableDiscoveryClient オープンサービス登録検出機能)

package com.lucifer.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author Lucifer
 */
@EnableDiscoveryClient
@SpringBootApplication
public class AlibabaNacosDiscoveryServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(AlibabaNacosDiscoveryServerApplication.class, args);
    }

}

springboot構成ファイル: 

spring:
  application:
    name: service-provider
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.101.16:8848

server:
  port: 8001

pom.xml:(メインjarパッケージ) 


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
   

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

開始:

2.モジュール:alibaba-nacos-discovery-client

1》 springboot起動クラス:alibaba-nacos-discovery-server起動クラスと同じ、および@EnableDiscoveryClient;

2》設定ファイル:

spring:
  application:
    name: service-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.101.16:8848

server:
  port: 8080

3》 pom.xml同上

 コントロール層:

package com.lucifer.alibabanacosdiscoveryclient.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author: lucifer
 * @date: 2019/8/6
 * @description:
 */
@EnableDiscoveryClient
@RestController
public class TestController {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    @Autowired
    private RestTemplate restTemplate;


    @GetMapping(value = "/hello/{str}")
    public String echo(@PathVariable(name = "str") String str) {
        return restTemplate.getForObject("http://service-provider/hello/" + str, String.class);
    }


}

開始:

もう一度http://192.168.101.16:8848/nacos/にアクセスしてください。 

郵便配達員のテスト:

1.localhost:8080 / hello / zhangsan

 2.localhost:8001 / hello / zhangsan

PS:

図に示すように、サービスの詳細を入力し、オフラインをクリックします。

インターフェースに再度移動します。サービスプロバイダーには使用可能なインスタンスがありません

もう一度オンラインをクリックして、数秒から数十秒待ってから、インターフェースに再度アクセスします。

 
187件の元の記事を公開 146のような 490,000以上にアクセス

おすすめ

転載: blog.csdn.net/qq_37495786/article/details/98626432