1、父工程pom文件
<?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>order</module>
<module>stock</module>
</modules>
<groupId>com.wanqi</groupId>
<artifactId>springCloudAlibaba</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springCloudAlibaba</name>
<description>springCloudAlibaba</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
<spring-cloud.alibaba.version>2021.1</spring-cloud.alibaba.version>
<spring-cloud.alibaba.nacos.version>2021.1</spring-cloud.alibaba.nacos.version>
<spring-boot.version>2.7.2</spring-boot.version>
<spring-cloud.loadbalancer.version>3.1.3</spring-cloud.loadbalancer.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 考虑到企业可能有自己的父工程,spring-boot父工程引用改到dependencyManagement -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${spring-cloud.alibaba.nacos.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 高版本移除了Ribbon,改用loadbalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>${spring-cloud.loadbalancer.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${spring-cloud.alibaba.nacos.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>
1.1、nacos服务本地部署启动
1.1.1、github下载nacos服务
1.1.2、解压后修改启动文件改为单机nacos/bin/startup.sh
解压命令
tar -xvf nacos-server-2.1.0.tar.gz
# export MODE="cluster"
export MODE="standalone"
1.1.3、修改启动端口nacos/conf/application.properties
1.1.4、启动nacos通过nacos/bin/startup.sh
1.1.5、根据启动提示找到日志nacos/logs/start.out
#看到一下提示表示启动成功
INFO Nacos started successfully in stand alone mode. use embedded storage
1.1.6、找到日志中启动logo的Console: http://192.168.110.57:8848/nacos/index.html使用浏览器打开,输入默认用户名密码,均为:nacos
2、服务提供方pom文件
<?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>springCloudAlibaba</artifactId>
<groupId>com.wanqi</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>stock</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
2.1、application.yml配置文件
server:
port: 8082
---
spring:
cloud:
nacos:
server-addr: 39.105.101.111:12205
discovery:
ephemeral: true #是否临时实例,false表示永久实例,掉线也不会消失
application:
name: stock-service
2.2、 编写测试Controller
@RestController
@RequestMapping("/stock")
public class StockController {
@Value("${server.port}")
private String port;
@RequestMapping("/add")
public String add(){
return "ok---" + port;
}
}
3、 服务调用方pom文件
<?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>springCloudAlibaba</artifactId>
<groupId>com.wanqi</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 高版本移除了Ribbon,改用loadbalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>
3.1、application.yml配置文件
server:
port: 8081
spring:
cloud:
nacos:
server-addr: 39.105.101.111:12205
discovery:
ephemeral: true #是否临时实例,false表示永久实例,掉线也不会消失
application:
name: order-service
3.2、配置类,调用方注册RestTemplate
@Configuration
public class AppConfig {
@Bean
@LoadBalanced //解决 java.net.UnknownHostException
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
3.3、controller使用RestTemplate
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/add")
public String add(){
return restTemplate.getForObject("http://stock-service/stock/add",String.class);
}
}
1、父工程pom文件
<?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>order</module>
<module>stock</module>
</modules>
<groupId>com.wanqi</groupId>
<artifactId>springCloudAlibaba</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springCloudAlibaba</name>
<description>springCloudAlibaba</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.3</spring-cloud.version>
<spring-cloud.alibaba.version>2021.1</spring-cloud.alibaba.version>
<spring-cloud.alibaba.nacos.version>2021.1</spring-cloud.alibaba.nacos.version>
<spring-boot.version>2.7.2</spring-boot.version>
<spring-cloud.loadbalancer.version>3.1.3</spring-cloud.loadbalancer.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 考虑到企业可能有自己的父工程,spring-boot父工程引用改到dependencyManagement -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.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>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>${spring-cloud.alibaba.nacos.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 高版本移除了Ribbon,改用loadbalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>${spring-cloud.loadbalancer.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>${spring-cloud.alibaba.nacos.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>
1.1、nacos服务本地部署启动
1.1.1、github下载nacos服务
1.1.2、解压后修改启动文件改为单机nacos/bin/startup.sh
解压命令
tar -xvf nacos-server-2.1.0.tar.gz
# export MODE="cluster"
export MODE="standalone"
1.1.3、修改启动端口nacos/conf/application.properties
1.1.4、启动nacos通过nacos/bin/startup.sh
1.1.5、根据启动提示找到日志nacos/logs/start.out
#看到一下提示表示启动成功
INFO Nacos started successfully in stand alone mode. use embedded storage
1.1.6、找到日志中启动logo的Console: http://192.168.110.57:8848/nacos/index.html使用浏览器打开,输入默认用户名密码,均为:nacos
2、服务提供方pom文件
<?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>springCloudAlibaba</artifactId>
<groupId>com.wanqi</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>stock</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
2.1、application.yml配置文件
server:
port: 8082
---
spring:
cloud:
nacos:
server-addr: 39.105.101.111:12205
discovery:
ephemeral: true #是否临时实例,false表示永久实例,掉线也不会消失
application:
name: stock-service
2.2、 编写测试Controller
@RestController
@RequestMapping("/stock")
public class StockController {
@Value("${server.port}")
private String port;
@RequestMapping("/add")
public String add(){
return "ok---" + port;
}
}
3、 服务调用方pom文件
<?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>springCloudAlibaba</artifactId>
<groupId>com.wanqi</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>order</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 高版本移除了Ribbon,改用loadbalancer -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>
3.1、application.yml配置文件
server:
port: 8081
spring:
cloud:
nacos:
server-addr: 39.105.101.111:12205
discovery:
ephemeral: true #是否临时实例,false表示永久实例,掉线也不会消失
application:
name: order-service
3.2、配置类,调用方注册RestTemplate
@Configuration
public class AppConfig {
@Bean
@LoadBalanced //解决 java.net.UnknownHostException
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
3.3、controller使用RestTemplate
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/add")
public String add(){
return restTemplate.getForObject("http://stock-service/stock/add",String.class);
}
}
4、Nacos集群部署,并使用nginx实现负载均衡
- 虚拟机准备
- mysql准备,安装并创建nacos库
- nginx安装
4.1、github下载nacos服务
4.1.1、解压nacos并修改nacos/conf/application.properties文件
- 修改端口号,配置放开并修改数据库相关数据
server.port=8849
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=mysql
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://39.105.101.111:3306/nacos?characterEncoding=utf-8&useSSL=true&serverTimezone=Asia/Shanghai
db.user.0=wq
db.password.0=123456
4.2、数据库表相关sql,nacos/conf/nacos-mysql.sql
4.3、修改启动脚本nacos/bin/startup.sh
# *根据服务器实际内存情况酌情修改*
JAVA_OPT="${JAVA_OPT} -server -Xms256m -Xmx256m -Xmn128m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
#JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"
# *新增以下参数设置本机ip地址,云服务器如果不修改会默认多一个使用网卡ip注册的节点*
JAVA_OPT="${JAVA_OPT} -Dnacos.server.ip=114.67.111.165"
4.5、在nacos/conf下新建cluster.conf文件配置集群
- 一台机器模拟端口最好分散一点不容易翻车,多台的话默认8848即可
#单台机器模拟
114.67.111.165:8849
114.67.111.165:8851
114.67.111.165:8853
#多台虚拟机
172.16.156.134:8848
172.16.156.135:8848
172.16.156.136:8848
4.5、启动并验证是否成功参考单机模式
4.6、启动成功后把这个nacos再复制2个修改端口然后启动即可
4.7、nginx配置新增在http下
upstream nacosServer{
#nacos集群ip与端口
server 114.67.111.165:8849;
server 114.67.111.165:8851;
server 114.67.111.165:8853;
}
server {
#监听端口
listen 12205;
#nginx服务器ip
server_name 39.105.101.111;
location /nacos{
proxy_pass http://nacosServer/nacos;
}
# 静态资源处理
location ~ .* {
proxy_pass http://nacosServer;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Fonwarded-For $proxy_add_x_forwarded_for;
}
}
4.8、访问nginx配置的监听端口与请求
http://39.105.101.111:12205/nacos
后续介绍:
集成openfeign、Nacos-config配置使用、集成sentinel、Spring Cloud Gateway、seata使用、SkyWalking链路追踪