Nacos服務(wù)注冊(cè)與發(fā)現(xiàn)

安裝Nacos

  • 下載二進(jìn)制包

    下載地址

  • 解壓

    unzip nacos-server-1.0.0.zip
    cd nacos/bin 
    
  • 啟動(dòng)

    sh startup.sh -m standalone
    
  • 打開登陸地址

    http://localhost:8848/nacos/
    

添加Spring Cloud Alibaba依賴

<dependencyManagement>
        <dependencies>
            <!--spring-boot-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-cloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-cloud-alibaba -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--nacos注冊(cè)中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
    </dependencies>

編寫服務(wù)提供者

@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class NacosDiscoveryProviderApplication {

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


    @GetMapping("/hello")
    public String hello(){
        return "hello nacos-discovery-provider";
    }
}

配置注冊(cè)到Nacos

server:
  port: 8085
spring:
  application:
    name: nacos-discovery-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

看到日志

2020-10-10 23:14:10.152  INFO 54121 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, nacos-discovery-provider 192.168.0.105:8085 register finished

表示已經(jīng)成功注冊(cè)到nacos,如圖:

nacos注冊(cè)服務(wù)者

編寫消費(fèi)者代碼

@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class NacosDiscoveryConsumerApplication {

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

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

使用Ribbon做客戶端負(fù)載均衡調(diào)用服務(wù)者

@RestController
public class HelloController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    LoadBalancerClient loadBalancerClient;

    @GetMapping("/hello")
    public String HelloClient(String name){
        ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-discovery-provider");
        URI uri = serviceInstance.getUri();
        return restTemplate.getForObject(uri + "hello?name=" + name, String.class);
    }
}

配置文件與服務(wù)端類似

server:
  port: 18085
spring:
  application:
    name: nacos-discovery-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848

兩者都成功注冊(cè)到Nacos里

image-20201011102305466

調(diào)用消費(fèi)者/hello服務(wù),可以調(diào)用Provider提供的服務(wù)

image-20201011102401045
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容