Article directory
Series of articles:
Introduction and installation of Nacos-01-Nacos
Code address: https://gitee.com/cangyu1024/nacos-demo
1: Configuration Center
1: Basic configuration use
The nacos configuration is as follows, using the dev namespace
0: guide package
<!-- 配置中心 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
1: configuration
Configured in bootstrap.yml, it is loaded earlier than application.yml
server:
port: 18080
servlet:
context-path: /
spring:
application:
name: nacos_config_test
cloud:
nacos:
config:
server-addr: ip:8848
namespace: 55f18581-f53b-4d17-b27a-d05d182fe1a9
group: DEFAULT_GROUP
name: nacos-test.yml
file-extension: yml
2: use
package com.java1234.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/*
* @description:
* @author: wangkanglu
* @date: 2022/8/25 21:09
* @param:
* @return:
**/
@RestController
@RequestMapping("/nacos")
@RefreshScope
public class NacosConfigController {
@Value("${java.name}")
private String name;
@Value("${java.age}")
private String age;
@GetMapping("/getConfigInfo")
public String getConfigInfo(){
return name+":"+age;
}
}
2: Load multiple configuration sets
1: Configuration
Configured in bootstrap.yml, it is loaded earlier than application.yml
server:
port: 18080
servlet:
context-path: /
spring:
application:
name: nacos_config_test
cloud:
nacos:
config:
server-addr: ip:8848
namespace: 55f18581-f53b-4d17-b27a-d05d182fe1a9
group: DEFAULT_GROUP
file-extension: yml
extension-configs:
- data-id: nacos-test.yml
group: DEFAULT_GROUP
refresh: true
- data-id: nacos-pro.yml
group: DEFAULT_GROUP
refresh: true
Note:
- data-id : Data Id
- group: Customize the group where the Data Id is located. If it is not explicitly configured, the default is DEFAULT_GROUP.
- refresh: Controls whether the Data Id supports dynamic refresh in the application when the configuration changes, so as to sense the latest configuration values. Default is not supported.
2: use
package com.java1234.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/*
* @description:
* @author: wangkanglu
* @date: 2022/8/25 21:09
* @param:
* @return:
**/
@RestController
@RequestMapping("/nacos")
@RefreshScope
public class NacosConfigController {
@Value("${java.name}")
private String name;
@Value("${java.age}")
private String age;
//这个是从nacos-pro.yml中获取的
@Value("${java.sex}")
private String sex;
@GetMapping("/getConfigInfo")
public String getConfigInfo(){
return name+":"+age+":"+sex;
}
}
2: Service Discovery
1: Introduction to Service Registration and Discovery
Large distributed microservice projects will split the project into multiple business module projects according to the business, and then call each other;
how to call each other? Here, each of
our project modules needs to go to the nacos service registration center to register, register the address and port of each project itself, and then other project modules can find the address of other module projects that need to be called through Nacos;
Realize service discovery and invocation;
2: Service registration
1: guide package
<!-- 服务注册/发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
2: Configuration
Configure in bootstrap.yml
server:
port: 18081
servlet:
context-path: /
spring:
application:
name: nacos_provider
cloud:
nacos:
discovery:
#注册中心地址
server-addr: ip:8848
#注册中心命名空间
namespace: 55f18581-f53b-4d17-b27a-d05d182fe1a9
config:
#配置中心地址
server-addr: 42.193.114.46:8848
#配置中心命名空间
namespace: 55f18581-f53b-4d17-b27a-d05d182fe1a9
#配置中心分组
group: DEFAULT_GROUP
file-extension: yml
extension-configs:
- data-id: nacos-test.yml
group: DEFAULT_GROUP
refresh: true
- data-id: nacos-pro.yml
group: DEFAULT_GROUP
refresh: true
3: Test
3: Service call
1: guide package
<!-- openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2: Configuration
1: Configure bootstrap.yml
server:
port: 18082
servlet:
context-path: /
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery:
#注册中心地址
server-addr: ip:8848
#注册中心命名空间
namespace: 55f18581-f53b-4d17-b27a-d05d182fe1a9
config:
#配置中心地址
server-addr: ip:8848
#配置中心命名空间
namespace: 55f18581-f53b-4d17-b27a-d05d182fe1a9
#配置中心分组
group: DEFAULT_GROUP
file-extension: yml
extension-configs:
- data-id: nacos-test.yml
group: DEFAULT_GROUP
refresh: true
- data-id: nacos-pro.yml
group: DEFAULT_GROUP
refresh: true
2: Define the Feign interface and generate a dynamic proxy object
package com.example.nacos_consumer.feign.provider;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @author wangkanglu
* @version 1.0
* @description
* @date 2022-08-25 22:11
*/
//@FeignClient(contextId = "remoteSearchService", value = ServiceNameConstants.SEARCH_CLIENT, fallbackFactory = RemoteSearchFallbackFactory.class)
@FeignClient(name = "nacos-provider")//这个注解就是指向被调用的服务
@Component
public interface RemoteHelloService {
@RequestMapping("/provider/hello")//这个地址就是被调用服务的地址
public String hello();
}
3: Start class plus @EnableFeignClients(basePackages = "com.java1234.feign")
Enable Feign client support
package com.example.nacos_consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class NacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
}
Note: @FeignClient annotation description
value、name
value和name的作用一样,如果没有配置url那么配置的值将作为服务名称,用于服务发现。反之只是一个名称。
contextId
我们不想将所有的调用接口都定义在一个类中,有一种解决方案就是为每个Client手动指定不同的contextId,这样就不会冲突了。
url
url用于配置指定服务的地址,相当于直接请求这个服务,不经过Ribbon的服务选择。像调试等场景可以使用。
decode404
当调用请求发生404错误时,decode404的值为true,那么会执行decoder解码,否则抛出异常。
configuration
configuration是配置Feign配置类,在配置类中可以自定义Feign的Encoder、Decoder、LogLevel、Contract等。
fallback
定义容错的处理类,也就是回退逻辑,fallback的类必须实现Feign Client的接口,无法知道熔断的异常信息。
fallbackFactory
也是容错的处理,可以知道熔断的异常信息。
path
path定义当前FeignClient访问接口时的统一前缀,比如接口地址是/user/get, 如果你定义了前缀是user, 那么具体方法上的路径就只需要写/get 即可。
primary
primary对应的是@Primary注解,默认为true,官方这样设置也是有原因的。当我们的Feign实现了fallback后,也就意味着Feign Client有多个相同的Bean在Spring容器中,当我们在使用@Autowired进行注入的时候,不知道注入哪个,所以我们需要设置一个优先级高的,@Primary注解就是干这件事情的。
qualifier
qualifier对应的是@Qualifier注解,使用场景跟上面的primary关系很淡,一般场景直接@Autowired直接注入就可以了。
3: load balancing
1: Load an identical provider, as long as the spring.application.name is the same, it is regarded as the same service
2: Start two providers and consumers
3: View nacos
4: You can also go in and make a judgment on the weight of each instance and whether it is online