The process of NACOS multi-environment configuration
This article mainly introduces "the process of NACOS multi-environment configuration". In daily operation, I believe many people have doubts about the process of NACOS multi-environment configuration. I have consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "the process of NACOS multi-environment configuration"! Next, please follow the small series to learn together!
Linux/Unix/Mac
Start command (standalone stands for standalone mode, non-cluster mode):
sh startup.sh -m standalone
If you are using ubuntu, or if you run the script with an error [symbol not found], try running as follows:
bash startup.sh -m standalone
Windows
Start command:
cmd startup.cmd
Or double-click startup.cmd to run the file.
Visit: 127.0.0.1:8848/nacos/index.html Login interface appears, startup successful. (User name and secret are nacos)
Services Register with NACOS
First Nacos specifies that a bootstrap profile must be available for injection. I use yml in my project, for example:
server: port: 8060spring: application: name: power-match #project name profiles: active: local cloud: nacos: config: server-addr: 127.0.0.1:8848 #Registry address discovery: server-addr: 127.0.0.1:8848
Next, an example of a startup class is as follows (I used feign):
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;import org.springframework.cloud.openfeign.EnableFeignClients;@EnableFeignClients@EnableDiscoveryClient@SpringBootApplicationpublic class PowerMatchApplication { public static void main(String[] args) { SpringApplication.run(PowerMatchApplication.class, args); }}
After running, go to the Nacos page to view the effect as follows:
表示注册成功。
分环境注册
在这我只举例官方推荐的方法,别的就不再介绍了。还是同一个nacos,登录--找到命名空间--新建命名空间,输入内容后就会生成命名空间ID
以application-local.yml配置为例:
spring: cloud: nacos: config: namespace: e503611c-9c54-4669-baff-e12770b3e948 discovery: namespace: e503611c-9c54-4669-baff-e12770b3e948ribbon: ReadTimeout: 60000 ConnectTimeout: 60000
启动后,去看服务列表的test下面就有注册的服务了,只会服务调用就会只在local中调用。
他支持多种服务消费方式WebClient、Feign、RestTemplate。
WebClient@EnableDiscoveryClient@SpringBootApplicationpublic class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Slf4j @RestController static class TestController { @Autowired private WebClient.Builder webClientBuilder; @GetMapping("/test") public Mono test() { Mono result = webClientBuilder.build() .get() .uri("http://alibaba-nacos-discovery-server/hello?name=didi") .retrieve() .bodyToMono(String.class); return result; } } @Bean @LoadBalanced public WebClient.Builder loadBalancedWebClientBuilder() { return WebClient.builder(); }}使用Feign
上面介绍的RestTemplate和WebClient都是Spring自己封装的工具,下面介绍一个Netflix OSS中的成员,通过它可以更方便的定义和使用服务消费客户端。下面也举一个具体的例子,其实现内容与上面两种方式结果一致:
第一步:在pom.xml中增加openfeign的依赖:
org.springframework.cloud spring-cloud-starter-openfeign
第二步:定义Feign客户端和使用Feign客户端:
@EnableDiscoveryClient@SpringBootApplication@EnableFeignClientspublic class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Slf4j @RestController static class TestController { @Autowired Client client; @GetMapping("/test") public String test() { String result = client.hello("word"); return "Return : " + result; } } @FeignClient("alibaba-nacos-discovery-server") interface Client { @GetMapping("/hello") String hello(@RequestParam String name); }}使用RestTemplate@EnableDiscoveryClient@SpringBootApplicationpublic class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } @Slf4j @RestController static class TestController { @Autowired RestTemplate restTemplate; @GetMapping("/test") public String test() { String result = restTemplate.getForObject("http://alibaba-nacos-discovery-server/hello?name=word", String.class); return result; } } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); }}
总的来说,还说和以前没的什么区别。
到此,关于"NACOS多环境配置的过程"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!