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 Nacos
quick entry to the registration center. If you are Nacos
a beginner, or want to quickly understand Nacos
the 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
Details are as follows:
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 RestTemplate
instantiate and add @LoadBalanced
annotations, as follows:
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
Then in , controller
we 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 Nacos
management background and adjust the weight of the service, as shown in the figure:
4.2 Modify the default policy of the Ribbon
user-provider:
ribbon:
NFLoadBalancerRuleClassName: com.alibaba.cloud.nacos.ribbon.NacosRule
summary
This article introduces Nacos
the entry-level use of the registration center, and introduces the passing Feign
and Ribbon
completion of inter-service calls, Nacos
and also implements a weight-based load balancing strategy, which can be used together Ribbon
.