场景
SpringCloud分布式微服务项目搭建构造父子模块依赖与实现服务提供者与消费者示例:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/124618737
SpringCloud分布式微服务项目Common通用依赖模块抽离:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/124631635
在上面搭建项目结构基础上,首先是实现了单机模式下集成Eureka。
SpringCloud中集成Eureka实现服务注册(单机Eureka构建):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/124688609
在上面的基础上怎样集成Sleuth实现链路追踪。
在上面搭建项目框架的基础上,怎样集成SpringCloudAlibaba并使用Nacos作为服务注册中心实现跨服务调用。
SpringCloudAlibaba
官网:
https://spring.io/projects/spring-cloud-alibaba#overview
学习资料:
https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md
Nacos
一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
替代Eureka做服务注册中心
替代Config做服务配置中心
下载:
https://nacos.io/zh-cn/index.html
官网文档:
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。
实现
1、Nacos的搭建
Nacos简介、下载与配置持久化到Mysql:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/109356152
这里不用配置持久化到Mysql。
参考上面实现本地搭建Nacos,并修改startup.cmd,使其以单机模式启动。
2、新建服务提供者6001
为了在各个子模块下引用SpringCloudAlibaba,所以将SpringCloudAlibaba的依赖添加进父级pom文件中
<!--spring cloud alibaba 2.1.0.RELEASE--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency>
pom文件的写法可以参考其官网
然后新建子模块cloudalibaba-provider-payment6001,并修改其pom文件,添加nacos的依赖
<!--SpringCloud ailibaba nacos --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
添加web以及其他依赖之后完整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>SpringCloudDemo</artifactId> <groupId>com.badao</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloudalibaba-provider-payment9001</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!--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> </dependencies> </project>
新建并修改application.yml
server: port: 6001 spring: application: name: nacos-payment-provider cloud: nacos: discovery: server-addr: localhost:8848 #配置Nacos地址 management: endpoints: web: exposure: include: '*'
这里配置nacos的地址为上面启动的nacos
配置文件的写法可以参考官网:
https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html
新建Controller,并添加一个接口方法
package com.badao.springcloudalibabademo.controller; 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; @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; } }
新建主启动类并添加注解@EnableDiscoveryClient
package com.badao.springcloudalibabademo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class PaymentMain6001 { public static void main(String[] args) { SpringApplication.run(PaymentMain6001.class, args); } }
3、为了演示出Nacos自带负载均衡功能,参考上面新建服务提供者6001的过程
复制出一模一样的服务提供者6002,除了端口不同之外,其他都相同。
这是因为nacos中集成了ribbon。
4、新建服务消费者cloudalibaba-consumer-nacos-order83模块
修改其pom文件,同样添加nacos的依赖
<?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>SpringCloudDemo</artifactId> <groupId>com.badao</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloudalibaba-consumer-nacos-order83</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!--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> </dependencies> </project>
然后新建并修改其application.yml
server: port: 83 spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: localhost:8848 #消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者) service-url: nacos-user-service: http://nacos-payment-provider
注意这里不同的是配置了要访问的微服务的名称。
然后还是需要配置restTemplate的配置文件,所以新建配置类
package com.badao.springcloudalibabademo.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
然后新建controller,实现服务调用
package com.badao.springcloudalibabademo.controller; 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 org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController public class OrderNacosController { @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); } }
新建主启动类并添加注解@EnableDiscoveryClient
package com.badao.springcloudalibabademo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class OrderNacosMain83 { public static void main(String[] args) { SpringApplication.run(OrderNacosMain83.class,args); } }
5、依次启动服务提供者6001和6002,再启动服务消费者83,查看nacos中是否服务都注册成功
然后访问服务消费者83的接口,查看下调用服务是否成功并实现了负载均衡。