Spring Cloud Alibaba
Spring Cloud Netflix项⽬进⼊维护模式
Spring Cloud Alibaba 能够提供微服务开发的⼀站式解决⽅案,⽽且经过阿⾥业务考验,⾜够优秀
参考⽂档 https://spring.io/blog/2018/12/12/spring-cloud-greenwich-rc1-available-now
- Spring CloudNetFlix项⽬进⼊维护模式
- 维护模式意味着Spring Cloud团队将不再向改团队添加新功能,修复block级别的Bug以及安全问题,会考虑审查社区的⼩型pull request
简介
Spring Cloud Alibaba 致⼒于提供微服务开发的⼀站式解决⽅案。此项⽬包含开发分布式应⽤微服务的必需组件,⽅便开发者通过 Spring Cloud 编程模型轻松使⽤这些组件来开发分布式应⽤服务。
依托 Spring Cloud Alibaba,您只需要添加⼀些注解和少量配置,就可以将 Spring Cloud 应⽤接⼊阿⾥微服务解决⽅案,通过阿⾥中间件来迅速搭建分布式应⽤系统。
官⽹地址:https://spring.io/projects/spring-cloud-alibaba
github项⽬地址:https://github.com/alibaba/spring-cloud-alibaba
主要功能
服务限流降级:默认⽀持 WebServlet、WebFlux, OpenFeign、RestTemplate、Spring Cloud Gateway, Zuul, Dubbo 和 RocketMQ 限流降级功能的接⼊,可以在运⾏时通过控制台实时修改限流降级规则,还⽀持查看限流降级 Metrics 监控。
服务注册与发现:适配 Spring Cloud 服务注册与发现标准,默认集成了 Ribbon 的⽀持。
分布式配置管理:⽀持分布式系统中的外部化配置,配置更改时⾃动刷新。
消息驱动能⼒:基于 Spring Cloud Stream 为微服务应⽤构建消息驱动能⼒。
分布式事务:使⽤ @GlobalTransactional 注解, ⾼效并且对业务零侵⼊地解决分布式事务问题。
阿⾥云对象存储:阿⾥云提供的海量、安全、低成本、⾼可靠的云存储服务。⽀持在任何应⽤、任何时间、任何地点存储和访问任意类型的数据。
分布式任务调度:提供秒级、精准、⾼可靠、⾼可⽤的定时(基于 Cron 表达式)任务调度服务。同时提供分布式的任务执⾏模型,如⽹格任务。⽹格任务⽀持海量⼦任务均匀分配到所有 Worker(schedulerx-client)上执⾏。
阿⾥云短信服务:覆盖全球的短信服务,友好、⾼效、智能的互联化通讯能⼒,帮助企业迅速搭建客户触达通道。
主要组件
Sentinel:把流量作为切⼊点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。
Nacos:⼀个更易于构建云原⽣应⽤的动态服务发现、配置管理和服务管理平台。
Seata:阿⾥巴巴开源产品,⼀个易于使⽤的⾼性能微服务分布式事务解决⽅案。
RocketMQ:⼀款开源的分布式消息系统,基于⾼可⽤分布式集群技术,提供低延时的、⾼可靠的消息发布与订阅服务。
Dubbo:Apache Dubbo™ 是⼀款⾼性能 Java RPC 框架。
Alibaba Cloud OSS: 阿⾥云对象存储服务(Object Storage Service,简称 OSS),是阿⾥云提供的海量、安全、低成本、⾼可靠的云存储服务。您可以在任何应⽤、任何时间、任何地点存储和访问任意类型的数据。
Alibaba Cloud SchedulerX: 阿⾥中间件团队开发的⼀款分布式任务调度产品,提供秒级、精准、⾼可靠、⾼可⽤的定时(基于 Cron 表达式)任务调度服务。
Alibaba Cloud SMS: 覆盖全球的短信服务,友好、⾼效、智能的互联化通讯能⼒,帮助企业迅速搭建客户触达通道。
版本说明
组件版本关系
依赖版本关系
Nacos
简介
为什么叫nacos
前四个字⺟分别为Naming和Configuration的前两个字⺟,最后的s为Service
简介
Nacos 致⼒于帮助您发现、配置和管理微服务。Nacos 提供了⼀组简单易⽤的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。 Nacos 是构建以“服务”为中⼼的现代应⽤架构 (例如微服务范式、云原⽣范式) 的服务基础设施。
下载地址
https://github.com/alibaba/Nacos 下载地址
https://nacos.io/zh-cn/docs/what-is-nacos.html 官⽅⽂档
总结
Nacos = Eureka + Config + Bus
Nacos代替Eureka作为注册中⼼,Nacos代替Config作为配置中⼼
安装
下载nacos-server-1.4.1.zip
解压进⼊bin⽬录执⾏startup.cmd启动
修改startup.cmd⽂件中 set MODE=“standalone”
访问http://localhost:8848/nacos,默认⽤户名密码都是"nacos"
入门案例
⼊⻔案例模拟订单微服务调⽤⽀付微服务,订单和⽀付都注册到Nacos,以及后续从nacos读取配置属性
⽀付微服务:服务提供者
订单微服务:服务消费者
⽗⼯程
⽗⼯程统⼀管理spring boot,spring cloud和spring cloud alibaba版本号,参考上⾯版本对应关系
Spring Cloud Hoxton.SR8
Spring Cloud Alibaba 2.2.5.RELEASE
Spring Boot 2.3.2.RELEASE
pom
<properties>
<java.version>1.8</java.version>
<alibaba-cloud.version>2.2.5.RELEASE</alibaba-cloud.version>
<springcloud.version>Hoxton.SR8</springcloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${springcloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${alibaba-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
公共⼯程
提供订单和⽀付业务都需要的公共实体类
pom.xml
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
注意:设置maven-plugin插件 true 可以避免spring boot应该⽤没有main class的错误
实体类
CommonResult
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
private Integer code; //返回状态码
private String message; //返回信息
private T data; //返回数据
public CommonResult(Integer code, String message) {
this.code = code;
this.message = message;
}
}
Payment
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Payment implements Serializable {
private Long id;
private String serial;
}
提供者-⽀付微服务
pom.xml
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--nacos config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--sentinel持久化-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency><!-- 引⼊⾃⼰定义的api通⽤包,可以使⽤Payment⽀付Entity -->
<groupId>org.example</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<!--⽇常通⽤jar包配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 后续做Sentinel的持久化会⽤到的依赖 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!-- sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
</dependencies>
application.yml
server:
port: ${port:9001}
spring:
application:
name: payment-service
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置Nacos地址
PaymentApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentApplication{
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class, args);
}
}
PaymentController
import com.lxs.springcloud.entity.CommonResult;
import com.lxs.springcloud.entity.Payment;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
@RestController
public class PaymentController {
@Value("${server.port}")
private String serverPort;
public static HashMap<Long, Payment> hashMap = new HashMap<>();
static {
hashMap.put(1L, new Payment(1L, "28a8c1e3bc2742d8848569891fb42181"));
hashMap.put(2L, new Payment(2L, "bba8c1e3bc2742d8848569891ac32182"));
hashMap.put(3L, new Payment(3L, "6ua8c1e3bc2742d8848569891xt92183"));
}
@GetMapping(value = "/paymentSQL/{id}")
public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id) {
Payment payment = hashMap.get(id);
CommonResult<Payment> result = new CommonResult(200, "from
mysql,serverPort: " + serverPort, payment);
return result;
}
}
5) 启动⽀付服务
启动两个服务实例端⼝号分别为9001和9002,注册到nacos中
配置⽂件port: ${port:9001}表示,没有port参数,使⽤9001端⼝,有port参数则使⽤port参数指定的端⼝
访问nacos查看服务列表
消费者-订单微服务
pom.xml
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--nacos config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--sentinel持久化配置-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<!--SpringCloud openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--SpringCloud ailibaba nacos -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--SpringCloud ailibaba sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- 引⼊⾃⼰定义的api通⽤包,可以使⽤Payment⽀付Entity -->
<dependency>
<groupId>org.example</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
server:
port: 84
spring:
application:
name: order-service
cloud:
nacos:
discovery:
server-addr: localhost:8848
启动器
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
@EnableDiscoveryClient
@EnableFeignClients
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
ApplicationContextConfig
//nacos底层也是ribbon,注⼊RestTemplate
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
5)FeignClient
PaymentFallbackService
@Component
public class PaymentFallbackService implements PaymentService {
@Override
public CommonResult<Payment> paymentSQL(Long id) {
return new CommonResult<>(44444, "服务降级返回,---
PaymentFallbackService", new Payment(id, "errorSerial"));
}
}
PaymentService
import com.springcloud.entity.CommonResult;
import com.springcloud.entity.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "payment-service", fallback = PaymentFallbackService.class)
public interface PaymentService {
@GetMapping(value = "/paymentSQL/{id}")
public CommonResult<Payment> paymentSQL(@PathVariable("id") Long id);
}
OrderController
@RestController
public class OrderController {
public static final String SERVICE_URL = "http://payment-service";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/ribbon/{id}")
public CommonResult consumer_ribbon(@PathVariable("id") Integer id){
CommonResult<Payment> result = restTemplate.getForObject(SERVICE_URL +
"/paymentSQL/" + id, CommonResult.class, id);
return result
}
//OpenFeign
@Resource
private PaymentService paymentService;
@GetMapping(value = "/consumer/feign/{id}")
public CommonResult<Payment> consumer_feign(@PathVariable("id") Long id) {
return paymentService.paymentSQL(id);
}
}
启动
访问http://localhost:84//consumer/ribbon/2地址,可以看到9001和9002端⼝交替执⾏,因为nacos底层也是采⽤ribbon进⾏负载均衡处理
通过feign调用,也是有负载均衡的。
配置中心:
基本配置
nacos可以作为配置中⼼使⽤,在payment⼯程中如下步骤,启动nacos配置中⼼
引⼊依赖
<!--nacos config-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
配置⽂件
bootstrap.yml
spring:
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
application.yml
server:
port: ${port:9001}
spring:
application:
name: payment-service
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置Nacos地址
profiles:
active: dev
注意:spring.cloud.nacos.config配置必须放到bootstrap.yml配置⽂件中,保证在优先读取配置⽂件再启动,否则配置⽆效
点击发布:
nacos中的配置
访问nacos在配置列表中增加如下配置
在Nacos配置中心就可以看到了一个配置
说明:之所以需要配置 spring.application.name ,是因为它是构成 Nacos 配置管理 dataId 字段的⼀部分。
在 Nacos Spring Cloud 中, dataId 的完整格式如下:
p
r
e
f
i
x
−
{prefix}-
prefix−{spring.profiles.active}.${file-extension}
对应的关系图
业务中读取配置属性
@RestController
@RequestScope
public class PaymentController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo() {
return configInfo;
}
}
测试
http://localhost:9001/config/info
修改以后页面直接刷新,可以看到已经是修改后的配置了
当修改配置值,会结果已经改变,Nacos⾃带⾃动刷新功能,也就是Nacos=Spring Cloud Config +Spring Cloud Bus
配置隔离
通常,企业研发的流程是这样的:先在开发测试环境开发和测试功能,然后灰度,最后发布到⽣产环境。并且,为了⽣产环境的稳定,需要将测试环境和⽣产环境进⾏隔离,此时,必然会遇到问题是多环境(dev,test,prod)问题,即:
多个环境的数据如何隔离?
如何优雅的隔离?
nacos采⽤如下⽅案进⾏服务的隔离,如下图:
1)创建dev,test,prod命名空间
2)在不同命名空间下创建如下配置
其他命名空间下,克隆修改相应内容
3)配置读取相应的配置
bootstrap.yml
spring:
cloud:
nacos:
config:
server-addr: localhost:8848
file-extension: yaml
namespace: f521b45b-e0bb-4c4d-b97c-3d0158970001
group: MY_GROUP
注意:如果不配置namespace默认为public,不配置group默认为DEFAULT_GROUP
4) 在注册service可以指定namespace隔离注册到nacos的服务
server:
port: ${port:9001}
spring:
application:
name: payment-service
cloud:
nacos:
discovery:
server-addr: localhost:8848 #配置Nacos地址
namespace: f521b45b-e0bb-4c4d-b97c-3d0158970001
profiles:
active: dev
服务已经被部署到Dev下了;
此时用订单服务去访问服务,就访问不到,因为他们不在同一个命名空间下了。
然后部署到同一个命名空间下,就又可以访问了
配置拆分⽅案
项⽬中会有很多的微服务,必然会存在很多具体配置,和重复配置,可以采⽤如下⽅案管理配置
根据上⾯分析在nacos中配置如下:
配置⽂件
bootstrap.yml
注:extension-configs配置属性和shared-configs配置属性功能⼀致,都是读取配置⽂件,这⾥我们把重复配置放到common.yaml中,这样在不同的⼯程中就可以重复使⽤[n]的值越⼤,优先级越⾼
application.yml
spring:
application:
name: payment-service
profiles:
active: dev
order微服务bootstrap.yml
注意:这⾥两个⼯程使⽤重⽤的配置⽂件common.yaml
交给Nacos管理的配置不能包括关于服务本身的名称的相关配置,如本例中的服务的名称。
订单微服务的配置文件注释掉
然后访问
可以发现已经成功了,说明配置到Nacos成功了
Nacos⾼可⽤
官⽹nacos集群部署⽂档:https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html
我们采⽤的Nacos⾼可⽤架构
在本地创建centos 7镜像,路径提前选好
复制好配置文件以后执行安装:
Vagrant.configure("2") do |config|
# 定义应⽤服务器
config.vm.define :s2 do |s2|
s2.vm.provider "virtualbox" do |v|
v.customize ["modifyvm", :id, "--name", "s2", "--memory", "4096", "--
cpus", "2"]
end
s2.vm.box = "centos/7"
s2.vm.hostname = "s2"
s2.vm.network :private_network, ip: "192.168.56.111"
end
end
安装sz,rz命令
yum -y install lrzsz
安装vim
yum -y install vim*
安装net-tool 包
yum install net-tools
开启远程登陆,修改“/etc/ssh/sshd_config”
PermitRootLogin yes
PasswordAuthentication yes
然后重启SSHD
systemctl restart sshd
使⽤Xshell或SecureCRT进⾏远程连接即可
安装MySQL
1)下载Mysql
下载地址 https://downloads.mysql.com/archives/community/
2)安装
拷⻉mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz到/root⽬录下
tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
解压完成后重命名
mv mysql-5.7.28-linux-glibc2.12-x86_64 mysql-5.7.28
mv mysql-5.7.28 /usr/local/
检查mysql组和⽤户是否存在,如果没有则创建
创建用户
安装数据库
创建data⽬录
cd /usr/local/mysql-5.7.28
mkdir data
将/usr/local/mysql-5.7.28的所有者及所属组改为mysql
chown -R mysql.mysql /usr/local/mysql-5.7.28
在/usr/local/mysql-5.7.28/support-files⽬录下创建my_default.cnf
cd /usr/local/mysql-5.7.28/support-files
vim my_default.cnf
[mysqld]
#设置mysql的安装⽬录
basedir =/usr/local/mysql-5.7.28
#设置mysql数据库的数据存放⽬录
datadir = /usr/local/mysql-5.7.28/data
#设置端⼝
port = 3306
socket = /tmp/mysql.sock
#设置字符集
character-set-server=utf8
#⽇志存放⽬录
log-error = /usr/local/mysql-5.7.28/data/mysqld.log
pid-file = /usr/local/mysql-5.7.28/data/mysqld.pid
#允许时间类型的数据为零(去掉NO_ZERO_IN_DATE,NO_ZERO_DATE)
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
拷⻉配置⽂件,是否覆盖,是
cp my_default.cnf /etc/my.cnf
初始化Mysql
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql-5.7.28/ --datadir=/usr/local/mysql-5.7.28/data/
如果报错:./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or 就安装libaio,如果没有则跳过
yum install libaio
初始化完成之后查看⽇志
cat /usr/local/mysql-5.7.28/data/mysqld.log
把启动脚本放到开机初始化⽬录
cp support-files/mysql.server /etc/init.d/mysql
启动mysql
service mysql start
进⼊mysql并更改密码
cd /usr/local/mysql-5.7.28
./bin/mysql -u root -p 上⾯⽣成的初始化密码
mysql> set password=password('123456');
mysql> grant all privileges on *.* to root@'%' identified by '123456';
mysql> flush privileges;
添加远程访问权限
mysql> use mysql;
mysql> update user set host='%' where user = 'root';
mysql> flush privileges;
如果更改时报错:ERROR 1062 (23000): Duplicate entry ‘%-root’ for key ‘PRIMARY’,就先查询⼀下是否已更改,最后执⾏刷新。
重启mysql⽣效
service mysql restart
3 安装JDK
yum安装很⽅便,实在是懒得⼀直拷⻉安装包了。
安装⽅法
yum install java-1.8.0-openjdk.x86_64
查找jdk安装的位置
[root@localhost]# which java
/bin/java
[root@localhost]# ls -l /bin/java
lrwxrwxrwx. 1 root root 22 Mar 22 01:01 /bin/java -> /etc/alternatives/java
[root@localhost]# ls -l /etc/alternatives/java
lrwxrwxrwx. 1 root root 73 Mar 22 01:01 /etc/alternatives/java ->
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.el7_7.x86_64/jre/bin/java
配置JAVA_HOME
vi /etc/profile
最后⼀⾏加⼊
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-
0.el7_7.x86_64/jre
export PATH=$PATH:$JAVA_HOME/bin
配置⽣效
source /etc/profile
4 安装Nginx
安装所需环境
Nginx 是 C语⾔ 开发,先安装相关依赖包
⼀. gcc 安装
安装 nginx 需要先将官⽹下载的源码进⾏编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
yum install gcc-c++
⼆. PCRE pcre-devel 安装
PCRE(Perl Compatible Regular Expressions) 是⼀个Perl库,包括 perl 兼容的正则表达式库。nginx 的http 模块使⽤ pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使⽤ pcre 开发的⼀个⼆次开发库。nginx也需要此库。命令:
yum install -y pcre pcre-devel
三. zlib 安装
zlib 库提供了很多种压缩和解压缩的⽅式, nginx 使⽤ zlib 对 http 包的内容进⾏ gzip ,所以需要在Centos 上安装 zlib 库。
yum install -y zlib zlib-devel
四. OpenSSL 安装
OpenSSL 是⼀个强⼤的安全套接字层密码库,囊括主要的密码算法、常⽤的密钥和证书封装管理功能及SSL 协议,并提供丰富的应⽤程序供测试或其它⽬的使⽤。
nginx 不仅⽀持 http 协议,还⽀持 https(即在ssl协议上传输http),所以需要在 Centos 安装OpenSSL 库
yum install -y openssl openssl-devel
官⽹下载
1.直接下载 .tar.gz 安装包,地址:https://nginx.org/en/download.html
解压
依然是直接命令:
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
配置
其实在 nginx-1.18.0 版本中你就不需要去配置相关东⻄,默认就可以了。当然,如果你要⾃⼰配置⽬录也是可以的。
1.使⽤默认配置
./configure
2.⾃定义配置(不推荐)
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
注:将临时⽂件⽬录指定为/var/temp/nginx,需要在/var下创建temp及nginx⽬录
编译安装
make
make install
查找安装路径:
whereis nginx
启动、停⽌nginx
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
5 Nacos持久化配置
拷⻉nacos-server-1.4.1.tar.gz,解压到/user/local/nacos下
tar -zxvf nacos-server-1.4.1.tar.gz -C /usr/local
创建nacos数据库,导⼊nacos\conf\nacos-mysql.sql脚本
修改conf\application.properties配置⽂件,如下:
#*************** 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://127.0.0.1:3306/nacos?
characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=tru
e&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=123456
### Connection pool configuration: hikariCP
db.pool.config.connectionTimeout=30000
db.pool.config.validationTimeout=10000
db.pool.config.maximumPoolSize=20
db.pool.config.minimumIdle=2
让3个Nacos节点都使⽤同⼀个数据源
修改nacos\conf\cluster⽂件:
10.0.2.15:8848
10.0.2.15:8849
10.0.2.15:8850
拷⻉2个nacos,修改端⼝号
server.port=8849
从之前Nacos导⼊配置,修改如下:
6 Nginx反向代理配置
修改/usr/local/nginx/conf配置如下:
#gzip on;
upstream nacos_cluster {
server 127.0.0.1:8848;
server 127.0.0.1:8849;
server 127.0.0.1:8850;
}
server {
listen 1111;
server_name localhost;
location / {
proxy_pass http://nacos_cluster;
proxy_set_header Host $host:$server_port;
}
}
7 配置⽂件
spring:
cloud:
nacos:
config:
server-addr: 192.168.56.110:1111
file-extension: yaml
extension-configs[0]:
data-id: common.yaml
refresh: true
运⾏测试,关闭⼀个Nacos节点,测试⾼可⽤,Nacos其他两个节点依然可⽤