Spring Cloud(微服务)学习篇(三)
1 nacos中使用openFeign(调用方式)实现短信发送
1.1 在shop-sms-api中创建com.zlz.shop.sms.api.service/vo/dto/util,目录结构如下所示
1.2 在pom.xml(shop-sms-api)中加入如下依赖
< dependencies>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-openfeign</ artifactId>
</ dependency>
</ dependencies>
1.3 在service包下创建SmsService接口
package com. zlz. shop. sms. api. service ;
import org. springframework. cloud. openfeign. FeignClient ;
import org. springframework. web. bind. annotation. RequestMapping ;
@FeignClient ( "shop-sms" )
public interface SmsService {
@RequestMapping ( "send" )
String send ( ) ;
}
1.4 把父项目(shop-sms)打包到本地仓库(只需要打包一次)
1.4.1 找到shop-sms项目➡双击install按钮
1.4.2 打包成功的界面
1.5 把子项目shop-sms-api打包到本地仓库
1.5.1 找到shop-sms-api项目➡双击install按钮
1.5.2 打包成功的界面
1.6 在子项目中shop-sms-server的pom.xml中引入短信验证模块
1.6.1 引入短信验证模块代码
<!--引入短信接口模块-->
<dependency>
<groupId>com.zlz</groupId>
<artifactId>shop-sms-api</artifactId>
<version>1.0</version>
</dependency>
1.6.2 完整的pom.xml文件(shop-sms-server)如下所示
<?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> shop-sms</ artifactId>
< groupId> com.zlz</ groupId>
< version> 1.0</ version>
</ parent>
< modelVersion> 4.0.0</ modelVersion>
< artifactId> shop-sms-api</ artifactId>
< dependencies>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-openfeign</ artifactId>
</ dependency>
</ dependencies>
</ project>
1.7 在UserStart类中(shop-sms-server项目)加上注解
1.7.1 加入的注解代码
@EnableFeignClients
1.7.2 完整的UserStart类代码
package com. zlz ;
import org. springframework. boot. SpringApplication ;
import org. springframework. boot. autoconfigure. SpringBootApplication ;
import org. springframework. cloud. client. discovery. EnableDiscoveryClient ;
import org. springframework. cloud. client. loadbalancer. LoadBalanced ;
import org. springframework. cloud. openfeign. EnableFeignClients ;
import org. springframework. context. annotation. Bean ;
import org. springframework. web. client. RestTemplate ;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class UserStart {
public static void main ( String [ ] args) {
SpringApplication . run ( UserStart . class ) ;
}
@Bean
@LoadBalanced
public RestTemplate restTemplate ( ) {
return new RestTemplate ( ) ;
}
}
1.8 变更UserController类
1.8.1 在里面加入如下代码
@Autowired
SmsService smsService;
@RequestMapping ( "test2" )
public String t2 ( ) {
String s= smsService. send ( ) ;
return "用户服务调用短信服务 结果:" + s;
}
1.8.2 完整的UserController类
package com. zlz. controller ;
import com. zlz. shop. sms. api. service. SmsService ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RestController ;
import org. springframework. web. client. RestTemplate ;
@RestController
public class UserController {
@Autowired
RestTemplate restTemplate;
@RequestMapping ( "test" )
public String t ( ) {
String s= restTemplate. postForObject ( "http://shop-sms/send" , null , String . class ) ;
return "用户服务调用短信服务 结果:" + s;
}
@Autowired
SmsService smsService;
@RequestMapping ( "test2" )
public String t2 ( ) {
String s= smsService. send ( ) ;
return "用户服务调用短信服务 结果:" + s;
}
}
1.9 测试
1.9.1 打开nacos窗口
1.9.2 打开两个不同端口的短信服务
a 打开端口号为8021的短信服务
b 打开端口号为8022的短信服务
1.9.3 打开用户服务
1.9.4 首次访问127.0.0.1:8010/test2地址
1.9.5 再次访问127.0.0.1:8010/test2地址