Nacos服务注册与发现

1/启动Nacos服务

下载nacos服务端,有多种方式,这里以windows平台1.2.0.beta.1编译后的压缩包单机模式为例,更多方式参考:https://nacos.io/zh-cn/docs/quick-start.html

下载解压之后到nacos->bin->startup.cmd,直接双击运行即可。

浏览器访问localhost:8848可以看到以下界面

Nacos服务注册与发现

 2 创建Maven父工程(相关依赖的版本可以到中央仓库查询https://mvnrepository.com)

pom.xml

<modules>    <module>nacos-spring-cloud-provider-example</module>    <module>nacos-spring-cloud-consumer-example</module></modules>    <dependencies>        <!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->        <dependency>            <groupId>com.alibaba.cloud</groupId>            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>            <version>2.2.0.RELEASE</version>        </dependency>        <!-- https://mvnrepository.com/artifact/com.alibaba.nacos/nacos-client -->        <dependency>            <groupId>com.alibaba.nacos</groupId>            <artifactId>nacos-client</artifactId>            <version>1.2.0-beta.1</version>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>            <version>2.2.4.RELEASE</version>        </dependency>        <dependency>            <groupId>org.apache.httpcomponents</groupId>            <artifactId>httpclient</artifactId>            <version>4.5.11</version>        </dependency>    </dependencies>

3服务提供者

NacosProviderApplication.java
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {

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

    @RestController
    class EchoController {
        @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET)
        public String echo(@PathVariable String string) {
            return "Hello Nacos Discovery " + string;
        }
    }
}

application.yml

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

4消费者

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {

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

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

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

        @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {

            return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
        }
    }
}

application.yml

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

5服务者消费者服务均运行起来,可以看到在nacos里看到这两个服务已经注册成功

Nacos服务注册与发现

 通过浏览器访问localhost:8871/echo/hellonacos

Nacos服务注册与发现

相关推荐