一 nacos
1.1 nacos概念
Nacos是服务注册发现中心+配置中心的组合。比eurka实现的功能更加强大。
nacos默认均有负载均衡的功能,集成了netflix的ribbon代码包。
1.2 nacos与其他进行对别
1.3 nacos的配置
1.4 namespace和group和dataid之间的关系
二 nacos的安装搭建
2.1 软件包下载
1.软件包下载地址 :home
2.找到对应版本,进行下载
2.2 nacos的安装
2.2.1 附件数据库
1.加压软件包后,进入到目录 nacos/conf/nacos-mysql.sql 找到这个sql文件进行附件。
2.先创建一个数据库,名字为nacos,将sql脚本进行附件数据库
2.2.2 修改启动脚本
进入bin目录下,修改 startup.cmd文件,在本地运行是需要改成standalone模式
2.2.3 修改数据库连接配置文件
2.2.4 启动服务
2.3 nacos页面访问
页面进行访问: http://localhost:8848/nacos
用户名和密码为: nacos/nacos
进入页面
1.添加一条数据
2.在mysql数据库中查看
三 nacos的生产消费案例
3.1 架构
3.2 生产提供服务模块7001
3.2.1 生产提供模块结构
在主工程新建一个模块,如下图
3.2.2 pom文件
配置pom文件的依赖
-
<!--SpringCloud ailibaba nacos -->
-
<dependency>
-
<groupId>com.alibaba.cloud
</groupId>
-
<artifactId>spring-cloud-starter-alibaba-nacos-discovery
</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>
-
<!--日常通用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>
3.2.3 配置文件
-
server:
-
port:
7001
-
#服务名称
-
spring:
-
application:
-
name: nacos-payment-provider
-
cloud:
-
nacos:
-
discovery:
-
server-addr: localhost:
8848
#配置Nacos地址
-
#暴露所有监控信息为HTTP
-
management:
-
endpoints:
-
web:
-
exposure:
-
include:
'*'
3.2.4 启动类
-
@EnableDiscoveryClient
-
@SpringBootApplication
-
public class App
-
{
-
public
static
void
main( String[] args )
-
{
-
//System.out.println( "Hello World!" );
-
SpringApplication
.run(App.class, args);
-
System
.out
.println(
"================nacos7001服务启动成功!!!!");
-
}
-
}
3.2.5 业务类
-
/**
-
* @auther zzyy
-
* @create 2020-02-23 14:13
-
*/
-
@RestController
-
public
class
PaymentController
-
{
-
@Value("${server.port}")
-
private String serverPort;
-
-
@GetMapping(value = "/payment/nacos/{id}")
-
public String getPayment(
@PathVariable("id") Integer id)
-
{
-
return
"nacos registry, serverPort: "+ serverPort+
"\t id"+id;
-
}
-
}
3.2.6 启动查看结果
1.服务启动
2.nacos页面
3.3 生产提供服务模块7002
3.3.1 复制一份服务
将服务模块7001复制一份变为7001,如下
2.需要将此7002服务添加到主服务中
3.主工程的pom文件中添加此模块的声明
3.3.2 修改一些配置
1.端口号
2.配置文件中端口
3.3.3 将服务均启动
可以看到两个服务提供者:7001和7002
3.4 消费模块7000
3.4.1 新建消费模块
3.4.2 pom文件修改
-
<dependency>
-
<groupId>junit
</groupId>
-
<artifactId>junit
</artifactId>
-
<version>4.13
</version>
-
<scope>test
</scope>
-
</dependency>
-
<!--SpringCloud ailibaba nacos -->
-
<dependency>
-
<groupId>com.alibaba.cloud
</groupId>
-
<artifactId>spring-cloud-starter-alibaba-nacos-discovery
</artifactId>
-
</dependency>
-
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
-
-
<!-- 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>
-
<!--日常通用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>
3.4.3 配置文件的修改
-
server:
-
port: 7000
-
-
-
spring:
-
application:
-
name: nacos-
order-consumer
-
cloud:
-
nacos:
-
discovery:
-
server-addr:
localhost:
8848
-
-
-
#消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
-
service-url:
-
nacos-user-service:
http:
//nacos-payment-provider
3.4.4 controller的业务
-
@RestController
-
@Slf4j
-
public class OrderController {
-
@Resource
-
private RestTemplate restTemplate;
-
-
@Value(
"${service-url.nacos-user-service}")
-
private String serverURL;
-
-
@GetMapping(value =
"/consumer/payment/nacos/{id}")
-
public String
paymentInfo(
@PathVariable(
"id") Long id)
-
{
-
return
restTemplate
.getForObject(serverURL+
"/payment/nacos/"+id,String.class);
-
}
-
}
3.4.5 负载均衡
-
@Configuration
-
public class ApplicationContextConfig
-
{
-
@Bean
-
@LoadBalanced
-
public RestTemplate
getRestTemplate()
-
{
-
return
new
RestTemplate();
-
}
-
}
3.4.6 启动服务查看
首先确保nacos启动,7001提供者服务启动,7002服务提供者启动;最后启动7000消费者
页面进行访问
再次刷新:
转载:https://blog.csdn.net/u011066470/article/details/128633299
查看评论