Table of contents
2. Modify the configuration and start nacos
3. Configure nacos in springcloud
1. springcloud configuration file pom
2. Configuration of order-nacos order module
3. Configuration of stock-service inventory module
4. Project start and operation
1. Download nacos
Select the nacos download that matches the springcloud version
Release Notes · alibaba/spring-cloud-alibaba Wiki · GitHub
Select the zip package in the windows environment. Unzip it after downloading.
2. Modify the configuration and start nacos
1. Direct boot
Enter the decompressed bin directory, edit startup.cmd, modify the set MODE line to set MODE="standalone", and save.
Double-click startup.cmd or run it as an administrator.
You can check the log in bin/logs to see if the startup is successful.
2. Background start
Use the nssm tool to install nacos as a background service. nssm use refer to the link below.
After starting, enter the address to access. Both username and password are nacos.
http://192.168.1.15:8848/nacos/index.html
3. Configure nacos in springcloud
Code directory structure
1. springcloud configuration file pom
The overall configuration of the project determines the version of springcloud and the version of each component.
Just focus on <dependencyManagement>, which replaces <parent>.
The modules under <modules> are automatically added when the project adds modules.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <modules> <module>stock</module> <module>order</module> <module>stock-nacos</module> <module>order-nacos</module> </modules> <groupId>com.wind.springcloud</groupId> <artifactId>alibaba</artifactId> <version>0.0.1-SNAPSHOT</version> <name>alibaba</name> <description>springcloudalibaba</description> <packaging>pom</packaging> <properties> <java.version>1.8</java.version> <spring.cloud.alibaba.version>2.2.5.RELEASE</spring.cloud.alibaba.version> <spring.boot.version>2.3.11.RELEASE</spring.boot.version> <spring.cloud.version> Hoxton.SR8</spring.cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <!-- alibaba版本管理--> <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> <!-- springboot版本管理--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- springcloud版本管理--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2. Configuration of order-nacos order module
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>alibaba</artifactId> <groupId>com.wind.springcloud</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>order-nacos</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- nacos服务注册发现 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> </dependencies> </project>
application.yml
Service registration to nacos uses the value of application name by default
server: port: 8020 spring: application: name: order-service cloud: nacos: server-addr: 127.0.0.1:8848 discovery: username: nacos password: nacos # namespace: public #Service classification management default public # ephemeral: false # Permanent instance defaults to true Temporary instance # service: default ${spring.application.name} # group: finer classification management # weight: #load balancing weight # metadata: metadata can be extended according to the source code
3. Configuration of stock-service inventory module
The pom.xml is consistent with the order module, and the application.yml only needs to change the port to 8021.
4. Code
OrderApplication.java
Where @LoadBalanced is the default load balancing call of nacos. Analyze the service name to obtain ip, port
package com.wind.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @author dongguanghui
* @date 2023/5/15 17:51
*/
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(RestTemplateBuilder builder){
RestTemplate restTemplate = builder.build();
return restTemplate;
}
}
OrderController.java
package com.wind.order.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author dongguanghui
* @date 2023/5/15 17:47
*/
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/add")
public String add(){
System.out.println("下单成功");
String msg = restTemplate.getForObject("http://stock-service/stock/reduct", String.class);
return "order success!" + msg;
}
}
StockApplication.java
package com.wind.stock;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @author dongguanghui
* @date 2023/5/15 17:51
*/
@SpringBootApplication
public class StockApplication {
public static void main(String[] args) {
SpringApplication.run(StockApplication.class,args);
}
}
StockController.java
package com.wind.stock.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @author dongguanghui
* @date 2023/5/15 17:47
*/
@RestController
@RequestMapping("/stock")
public class StockController {
@Value("${server.port}")
private String port;
@RequestMapping("/reduct")
public String reduct(){
System.out.println("下单成功");
return "reduct success!:"+port;
}
}
4. Project start and operation
After the project starts, the service is registered to nacos.
The browser directly calls the interface. You can see that the background calls another service successfully through the service name