赞
踩
Nacos是服务注册发现中心+配置中心的组合。比eurka实现的功能更加强大。
nacos默认均有负载均衡的功能,集成了netflix的ribbon代码包。
代码地址:
https://gitee.com/jurf-liu/mscloud-2022.git
1.软件包下载地址 :home
2.找到对应版本,进行下载
1.加压软件包后,进入到目录 nacos/conf/nacos-mysql.sql 找到这个sql文件进行附件。
2.先创建一个数据库,名字为nacos,将sql脚本进行附件数据库
进入bin目录下,修改 startup.cmd文件,在本地运行是需要改成standalone模式
在config目录下,修改application配置文件中数据库的连接信息
页面进行访问: http://localhost:8848/nacos
用户名和密码为: nacos/nacos
进入页面
1.添加一条数据
2.在mysql数据库中查看
在主工程新建一个模块,如下图
配置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>
- server:
- port: 7001
- #服务名称
- spring:
- application:
- name: nacos-payment-provider
- cloud:
- nacos:
- discovery:
- server-addr: localhost:8848 #配置Nacos地址
- #暴露所有监控信息为HTTP
- management:
- endpoints:
- web:
- exposure:
- include: '*'
- @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服务启动成功!!!!");
- }
- }
- /**
- * @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;
- }
- }
1.服务启动
2.nacos页面
将服务模块7001复制一份变为7001,如下
2.需要将此7002服务添加到主服务中
3.主工程的pom文件中添加此模块的声明
1.端口号
2.配置文件中端口
可以看到两个服务提供者:7001和7002
1.服务名称
2.服务名称对应的集群中具体ip和端口:
- <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>
- 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
- @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);
- }
- }
- @Configuration
- public class ApplicationContextConfig
- {
- @Bean
- @LoadBalanced
- public RestTemplate getRestTemplate()
- {
- return new RestTemplate();
- }
- }
首先确保nacos启动,7001提供者服务启动,7002服务提供者启动;最后启动7000消费者
页面进行访问:http://localhost:7000/consumer/payment/nacos/555
再次刷新:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。