Nacos Registration Center Quick Start

As a member of the company's infrastructure team, I made a technology selection for the company's infrastructure registration center at the end of last year. I accidentally looked through last year's selection materials, and saw this Nacosquick entry to the registration center. If you are Nacosa beginner, or want to quickly understand Nacosthe difficulty of getting started, I hope this article can help you.

1. Quick start

1.1 Introducing dependencies

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

1.2 Application configuration

spring:
  cloud:
    nacos:
      discovery:
        server-addr: nacos-host:80
        namespace: e5aebd28-1c15-4991-a36e-0865bb5af930
        group: ${
    
    spring.profiles.active}

1.3 Start the application

Annotations added in the startup class of the project @EnableDiscoveryClient.

@SpringBootApplication
@EnableDiscoveryClient
public class UserProviderApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(UserProviderApplication.class, args);
    }
}

1.4 View examples

28c89f43531c4620a96b83ed35123f58

Details are as follows:

image-20221108140422655

2. Use Feign to complete the service call

2.1 Introducing dependencies

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>2.2.2.RELEASE</version>
</dependency>

2.2 Startup class

The annotation added on the startup class @EnableFeignClients.

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class UserConsumerApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(UserConsumerApplication.class, args);
    }
}

2.3 Application configuration

feign:
  hystrix:
    enabled: true

2.4 Example of use

@FeignClient(name = "user-provider",fallback = UserServiceFallback.class)
public interface UserService {
    
    
    @RequestMapping("/user/config")
    String config();
}
 
@Service
public class UserServiceFallback implements UserService {
    
    
    @Override
    public String config() {
    
    
        return "user-fallback";
    }
}

The controller call is as follows.

@Autowired
private UserService userService;
 
@RequestMapping("consumer-feign")
public String userService() {
    
    
    return userService.config();
}

3. Use the Ribbon to complete the service call

We just need to RestTemplateinstantiate and add @LoadBalancedannotations, as follows:

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

Then in , controllerwe use this instantiation RestTemplate, and that’s it. The specific implementation is as follows:

@Autowired
private RestTemplate restTemplate;
 
@RequestMapping("consumer-ribbon")
public String consumerribbon() {
    
    
    String url = "http://user-provider/user/config";
    return restTemplate.getForObject(url, String.class);
}

4. Use Nacos weight load balancing

4.1 Modify weight

The calling methods of the three services have been introduced to you. However, their load balancing strategy is polling, which does not meet our requirements. We enter the Nacosmanagement background and adjust the weight of the service, as shown in the figure:

bb0c162c8c274df8a29f199fa3c238c6

4.2 Modify the default policy of the Ribbon

user-provider:
  ribbon:
    NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule

summary

This article introduces Nacosthe entry-level use of the registration center, and introduces the passing Feignand Ribboncompletion of inter-service calls, Nacosand also implements a weight-based load balancing strategy, which can be used together Ribbon.

Guess you like

Origin blog.csdn.net/yang237061644/article/details/127767554