SpringCloud Alibaba
Nacos
What is Nacos
Nacos is the first two letters of Naming and Configuration, and the last S is Service
A dynamic service discovery, configuration management and service management platform that is easier to build cloud native applications
Nacos is the combination of registration center + configuration center Eureka+Config+Bus
Download and launch Nacos
Official website address https://github.com/alibaba/nacos/releases/tag/1.4.1
start-up
- Run startup.exe in bin directory CMD file
- visit http://localhost:8848/nacos
- The default password and account number are nacos
- All versions above 1.3.2 are cluster startup by default. When there is no configuration database, change the startup. In the bin directory The content set MODE="cluster" in CMD file is changed to set MODE = "standalone"
Using Nacos
Official teaching https://spring.io/projects/spring-cloud-alibaba#learn
Registration Center
Pom
Parent project
<dependencyManagement> <dependencies> <!--spring boot 2.2.2--> <!--Manage all about spring boot jar Package version--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.2.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--spring cloud alibaba 2.1.0.RELEASE--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies> </dependencyManagement>
Provider
<dependencies> <!--boot web actuator--> <!-- SpringBoot integration Web assembly --> <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> <!--mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies>
yml
server: port: 9002 spring: application: name: nacos-provider cloud: nacos: discovery: server-addr: localhost:8848 datasource: driver-class-name: com.mysql.jdbc.Driver # mysql driver package url: jdbc:mysql://localhost:3306/test_student?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: zhangzhen management: endpoints: web: exposure: include: '*'
Startup class
@EnableDiscoveryClient @SpringBootApplication public class studentMain9002 { public static void main(String[] args) { SpringApplication.run(studentMain9002.class,args); } }
consumer
Load balancing class
@Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
Controller
@Autowired private RestTemplate restTemplate; @Value("${service-url.nacos-user-service}") String SERVICE_URL; @GetMapping(value = "/consumer/fallback/{id}") public String fallback(@PathVariable("id") Long id) { //Registration name + producer path + return value type return restTemplate.getForObject(SERVICE_URL + "/querystudent/"+id,String.class); }
Configuration center
Naming rules for nacos configuration file names
Registry name - development environment (dev/test/info) file type
p r e f i x − {prefix}- prefix−{spring.profile.active}.${file-extension}
pom
<!--nacos-config--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
Namespace (namespsace)
bootStrap. YML (priority)
# nacos configuration server: port: 83 spring: application: name: nacos-config-client cloud: nacos: discovery: server-addr: localhost:8848 #Nacos service registry address config: server-addr: localhost:8848 #Nacos as configuration center address file-extension: yaml #Specifies the configuration of yaml format
application.yml
spring: profiles: active: dev # Represents the development environment #active: test # Represents the test environment #active: info
Configure group
# nacos configuration server: port: 3377 spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848 #Nacos service registry address config: server-addr: localhost:8848 #Nacos as configuration center address file-extension: yaml #Specifies the configuration of yaml format group: DEV_GROUP
Configuration set id(Dala ID)
# nacos configuration server: port: 3377 spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848 #Nacos service registry address config: server-addr: localhost:8848 #Nacos as configuration center address file-extension: yaml #Specifies the configuration of yaml format group: DEV_GROUP namespace: b483a3ec-6573-468f-904c-5ed1ab3e9740
Nacos persistence
By default, nacos uses Derby database to store data. Every time a nacos is created, a derby database will be generated to store data. There is a problem with data unification when clustering. At present, nacos only supports mysql and Derby, and establishes mysql to store all the established nacos data to solve the problem of data inconsistency
-
set up a database
Run Nacos MySQL in the directory of nacos-server-1.4.1\nacos\conf SQL file
-
Change the nacos profile
Change the application in the directory of nacos-server-1.4.1\nacos\conf Properties file
spring.datasource.platform=mysql #The number of databases can be matched with the number of database clusters built by yourself db.num=1 #Server address local default 127.0.0.1 + database port number: 3306 + data name There may be a time zone problem. Just add & servertimezone = UTC after saying hello db.url.0=jdbc:mysql://127.0.0.1:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&serverTimezone=UTC db.user=root db.password=zhangzhen
-
Run test
Nacos cluster
-
Download the Linux version of nocas
Official website address https://github.com/alibaba/nacos/releases/tag/1.4.1
-
Transferring files to a Linux server
- Upload to opt folder
- decompression
- Create a new folder and copy the extracted files
-
nacos persistence
- Install mysql
- navcat connects to the database and runs the sql file in nacos
- Change Nacos / conf / application The properties configuration file is consistent with the stand-alone version
-
Modify cluster configuration file
-
Configure cluster
-
Use the hostname -i command to get the local ip address
-
Modify / Nacos / conf / cluster Conf file
-
Delete the original content
-
Add your own ip: port number
-
-
-
There is no economic strength to choose to simulate three servers. Modify the script startup information. If there are three servers, it does not need to be configured
Modify Nacos / bin / startup SH file
-
Start the nacos cluster
nacos/bin/./startup.sh -p 3333
nacos/bin/./startup.sh -p 4444
nacos/bin/./startup.sh -p 5555
View startup details
ps -ef |grep nacos
It can be seen from this that only one was started
When a server starts up and starts multiple nacos at the same time, it may run out of memory
Change the startup file to reduce the memory temporarily used at startup
Just restart
Stop command: Nacos / bin // shutdown. sh
Startup is the same as above
Find out
Congratulations on your successful startup
-
Configure nginx
vim nginx/conf/nginx.conf
Run nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
There may be no sbin folder obtained by decompressing the file
Enter nginx folder to execute
make && make install
Run nginx again
Query whether nginx is running
ps -ef | grep nginx
Indicates successful operation
test
Access the server ip in the local browser: port number / nginx
Why is the node state DOWN? I don't know for the time being
It's a success!