foreword
This article mainly introduces that SpringCloud uses Nacos as the configuration center and registration center, learns the basic configuration of Naocs through a simple demo, and completes interface calls between different microservices through RestTemplate and openfeign.
Nacos installation configuration
Nacos learning resources mainly include the following two
- Nacos pipe network Nacos pipe network
- Nacos GitHub NacosGitHub
Download Nacos
Download the corresponding version of Nacos directly from GitHub https://github.com/alibaba/nacos/releases . This article uses 2.1.0. If there is no GitHub acceleration, you can download it directly from CSDN. I uploaded it, completely free, and no points are required https: //download.csdn.net/download/whzhaochao/88074308
Start Naocs
If startup.cmd
the configuration in the Windows modification set MODE="standalone"
is configured as a stand-alone mode, then click startup.cmd
Run to enable Nacos, or use to startup.sh -m standalone
specify the startup mode.
When we see 8848, it means that the startup is successful. Enter http://127.0.0.1:8848/nacos/ in the browser , username nacos, password nacos to log in to Nacos.
configure nacos
After entering Nacos, there are several concepts that need to be briefly explained
Namespaces
It can be understood as different environments. For example, dev/test/prod corresponds to development/test/official. We can create these three environments separately. Pay attention to the namespace ID (it will be automatically generated if it is not filled in). We will not fill it in and let it be automatically generated. up.
configuration list
The configuration list is to replace the yml or properties file in our previous project. We choose the dev environment to create a DataID as user-service and Group as Shop. Here, DataID can be understood as corresponding to one of our microservices, and Group can be understood as one of our projects. We make a mall, there will be a user center and an order center. Here, the mall project can be understood as a Group, and the user center can be understood as a Data ID.
service management
The service management here corresponds to one of our microservices. When our microservice starts, it will be automatically registered here. There is no data yet
Spring Cloud environment construction
We make a very simple Demo, the basic logic is to enable a user center, which pulls user configuration from the nacos configuration center, and then provides an interface to obtain user information through ID, creates an interface in the order center, and calls the interface of the user center Obtain user information, the basic project structure is as follows:
Here we refer to the POM with a note: different SpingBoot versions may not be compatible with cloud-alibaba and nacos, we can download from https://github.com/spring-cloud- incubator/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E Check the corresponding compatible version
<properties>
<spring-boot-start-web.version>2.3.11.RELEASE</spring-boot-start-web.version>
<spring-cloud-alibaba.version>2.2.5.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot-start-web.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
The bootstrap.yml configuration of the user center
Here we mainly look at two key configurations, nacos.config and nacos.discovery. config is mainly configured to pull configuration information from nacos. Here, our prefix applies to our nacos background user-service, namespace corresponds to our development environment, and group is Corresponding to our project shop.
server:
port: 82
spring:
profiles:
active: dev
application:
name: user-service
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
group: shop
enabled: true
prefix: user-service
namespace: 6835622b-e819-4a78-a4d0-3fdc12b29ae9
file-extension: yaml
discovery:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
namespace: 6835622b-e819-4a78-a4d0-3fdc12b29ae9
group: shop
Dependency configuration
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
Let's take a look at UserConfig
this class, through which we can pull the users.list data configured in nacos.
@Configuration
@ConfigurationProperties("users")
@Data
public class UserConfig {
private List<User> list;
}
Let's take a look. UserController
This class provides a way /user/{id}
to get the user information of the nacos configuration center through the id.
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Value("${server.port}")
private Integer port;
@Resource
private UserConfig userConfig;
@GetMapping("/{id}")
public User info(@PathVariable Long id){
return userConfig.getList().stream().filter(x->x.getId().equals(id)).peek(x-> x.setPort(port)).findFirst().get();
}
}
Enable the nacos-user project, when we start the nacos-user project, you can see the user-service in the nacos background, the port is 82
We can successfully obtain the configuration information of nacos through the IP and port http://192.168.1.103:82/user/1 displayed on nacos
Order center
Order center configuration
server:
port: 81
spring:
profiles:
active: dev
application:
name: order-service
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
group: shop
enabled: true
prefix: order-service
namespace: 6835622b-e819-4a78-a4d0-3fdc12b29ae9
file-extension: yaml
discovery:
server-addr: 127.0.0.1:8848
username: nacos
password: nacos
namespace: 6835622b-e819-4a78-a4d0-3fdc12b29ae9
group: shop
Dependency configuration
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
Order center, in the order center we wrote two interfaces /order/{id}
by restTempate
calling the interface of the user center and /order/feign/{id}
by openFeign
calling the interface of the user center. It should be noted here that it RestTemplate
needs to be added in the configuration @LoadBalanced
, otherwise the interface address will not be found, and it will be used after adding it ribon
Load balancing ribon
will find the corresponding service address from the registration center through user-service.
@RestController
@RequestMapping("/order")
public class OrderController {
@Resource
private RestTemplate restTemplate;
@Resource
private UserFeign userFeign;
@GetMapping("/{id}")
public User info(@PathVariable Long id){
return restTemplate.getForObject("http://user-service/user/".concat(id.toString()), User.class);
}
@GetMapping("/feign/{id}")
public User feign(@PathVariable Long id){
return userFeign.getUser(id);
}
}
@Configuration
public class RestTempleteConfig {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
@FeignClient(value = "user-service")
@Component
public interface UserFeign {
@GetMapping("user/{id}")
User getUser(@PathVariable Long id);
}
Start nacos-order
, there will be one of the nacos services order-service
, we access the corresponding interface through the IP and port, and complete the call to the user center service
Enter http://192.168.1.103:81/order/1 Call the user center interface through restTempate Enter
http : //192.168.1.103:81/order/feign/2 calls the user center interface through openFeign
Here we copy the user-service startup configuration and overwrite and service.port
start two user-services
You can see that there are two instances of nacos background user-service
When order-service is called, it is provided by different user-services to achieve load balancing.
at last
This article is nacos
an introduction to learning, and more nacos
related content will be learned later
Project source code: https://gitee.com/whzhaochao/nacos-study