当前位置:网站首页>Nacosclient client is built, and micro services are registered into Nacos

Nacosclient client is built, and micro services are registered into Nacos

2022-07-19 16:43:00 Hu Shang

Import the following coordinates in the project :

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

In the global profile application.yml File for the corresponding configuration

server:
  port: 8083

#  Specify the application name ,Nacos This name will be used as the service name 
spring:
  application:
    name: order-service
  cloud:
    nacos:
      #  Appoint Nacos Address of service , The default value is localhost:8848
      server-addr: 127.0.0.1:8848
      discovery:
        #  Appoint Nacos  Login user name and password of management interface , The default value is nacos
        username: nacos
        password: nacos
        #  Specify the namespace 
        #  The function of namespace is to isolate different service instances , The default name is public
        namespace: public

from Spring Cloud Edgware Version start , There is no need to add @EnableDiscoveryClient Note the , If it is a previous low version, you need to add @EnableDiscoveryClient or @EnableEurekaClient, You can go to maven In the central warehouse of spring-cloud-dependencies What are the versions of .

And then start the service , Log in to Nacos You can find that the service has been registered

 Insert picture description here



Next, let's test and pass Nacos The registry communicates Services

@RestController
@RequestMapping("/order")
public class OrderController {
    

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/add")
    public String add(){
    
		//  Use RestTemplate call stock-service Interfaces in services 
        String message = restTemplate.getForObject("http://stock-service/stock/reduce", String.class);
        return " checkout success !" + message;
    }
}

When you start the service, you will find an error
 Insert picture description here
This is because , Service from Nacos After getting the list of services in the registry , You also need a load balancer to make service calls , As shown in the figure below
 Insert picture description here
So here we are RestTemplate Here is an annotation of load balancer @LoadBalanced,Nacos In fact, it is integrated Ribbon,

@Configuration
public class RestConfig {
    

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(RestTemplateBuilder builder){
    
        RestTemplate restTemplate = builder.build();
        return restTemplate;
    }
}

[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Y1eulPsF-1658022301252)(E:\Java note \image\springcloud_alibaba\image-20220606123538401.png)]

Then it can be called and passed , This is because Nacos There is no way to resolve the address corresponding to the service name , We need to rely on the load balancer to resolve the address according to the service name , Then call .

原网站

版权声明
本文为[Hu Shang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207180243244756.html