nacos learning

First, installation services:

1. Download nacos package: https://github.com/alibaba/nacos/releases ;

2. After the download is complete, extract. Depending on the platform, execute different commands to start the stand-alone version Nacos services:

(1) Linux/Unix/Macsh startup.sh -m standalone

(2) Windowscmd startup.cmd -m standalone

3, open a browser: http: // localhost: 8848 / nacos; Username and password are: nacos;

Second, the realization Provider :

1, the new Spring boot project provider;

2, the package is introduced in a POM file:

1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-web</artifactId>
4 </dependency>
5 <dependency>
6     <groupId>org.springframework.cloud</groupId>
7     <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
8     <version>0.9.0.RELEASE</version>
9 </dependency>

3, in the configuration file application.yml filled out: 

1 server:
2   port: 5005
3 spring:
4   application:
5     name: provider
6   cloud:
7     nacos:
8       discovery:
9         server-addr: 127.0.0.1:8848

4, the inlet class annotate @EnableDiscoveryClient: 

 1 @EnableDiscoveryClient
 2 @SpringBootApplication
 3 public class NoApplication {
 4     public static void main(String[] args) {
 5         SpringApplication.run(NoApplication.class,args);
 6     }
 7 }
 8 
 9 @RestController
10 public class NacosProducerController {
11     @RequestMapping("/hello")
12     public String hello(@RequestParam("name")String name)
13     {
14         return "hello::"+name;
15     }
16 }

5, start the application, you can http: // localhost: list of services on 8848 / nacos see the service provider; 

Third, the realization Consumer :

1, the new Spring boot project;

2, the package is introduced in the POM:

1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-web</artifactId>
4 </dependency>
5 <dependency>
6     <groupId>org.springframework.cloud</groupId>
7     <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
8     <version>0.9.0.RELEASE</version>
9 </dependency>

3, in the configuration file application.yml filled out: 

1 server:
2   port: 6060
3 spring:
4   application:
5     name: consumer
6   cloud:
7     nacos:
8       discovery:
9         server-addr: 127.0.0.1:8848

4, the inlet class implements: 

 1 @RestController
 2 public class ConController {
 3     @Autowired
 4     RestTemplate restTemplate;
 5 
 6     @RequestMapping("/callSayHello")
 7     public String services(@RequestParam("name") String name) {
 8 
 9         String callServiceResult = restTemplate.getForEntity("http://provider/hello?name="+name,String.class).getBody();
10         System.out.println(callServiceResult);
11         return callServiceResult;
12     }
13 }

5, start the application, you can find a new consumer service in the list of services nacos; then enter in the browser http: // localhost:? 6060 / callSayHello name = 123456 to see the return of the data; 

Fourth, the pit:

Spring cloud with nacos achieve more pit, according to the steps of the knock-line code, not being given the run up to find that service is not registered with the nacos, and finally found the problem with the version;

nacos version 0.2.x.RELEASE corresponds Spring Boot 2.x version, nacos version 0.1.x.RELEASE corresponds Spring Boot 1.x versions.

I finally debugging code by using the corresponding version is: nacos: 0.9.0.RELEASE , the Spring-the Boot-Starter-parent : 2.1.7.RELEASE ; nacos Server Version: 1.1.3;

 

V. References:

https://blog.51cto.com/9332743/2423285

https://www.cnblogs.com/zgwjava/p/10562775.html

http://blog.didispace.com/spring-cloud-learning/

https://nacos.io/zh-cn/docs/quick-start-spring.html

Guess you like

Origin www.cnblogs.com/laoxia/p/11456565.html