古人学问无遗力,少壮功夫老始成
-陆游
最近项目需要用到远程调用,在博客上记录一下。
Demo样例:
做2个服务,服务A,服务B
服务A会调用服务B
服务A:BigDecimal cale(BigDecimal,BigDecimal),调用divided,并对返回的结果+1后返回
服务B:BigDecimal divided(BigDecimal,BigDecimal),除法运算
这里我的服务中心用的是nacos,SpringCloud-Alibaba 之nacos服务注册中心这是我的上一篇博客.
老样子,maven中所引入的依赖在上一篇中都有,你们可以自己查看.
A服务相当于消费者,B服务相当于提供者。
提供者模块依赖如下:
因为我的,父工程中写了springcloud和springcloudAlibaba和springboot的版本号,所以我这里就不用写版本号,版本都在上一篇中有。
dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring boot test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
其中的controller如下:
@RestController
public class TestOpenFeignController {
@RequestMapping("/divided")
public BigDecimal test01(){
BigDecimal b1 = BigDecimal.TEN;
BigDecimal b2 = BigDecimal.ONE;
return b1.divide(b2);
}
yml配置文件:
server:
port: 8992
spring:
application:
name: cloud-provider1
cloud:
nacos:
discovery:
server-addr: localhost:8848
management:
# 端点检查(健康检查)
endpoints:
web:
exposure:
include: "*"
主启动类:
@SpringBootApplication
@EnableDiscoveryClient
public class CloudProvider1 {
public static void main(String[] args){
SpringApplication.run(CloudProvider1.class,args);
}
}
好,提供者写完了,接下来写消费者了。
maven依赖如下:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 远程调用 SpringCloud Openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
我和个版本的的springcloud不引入spring-cloud-starter-loadbalancer这个会报错。
自定义一个接口如下:
@FeignClient(name = "cloud-provider1")
public interface OpenFeignService1 {
@RequestMapping("/divided")
public BigDecimal test01();
}
@FeignClient中的name是你的服务提供者的名子例如:
所以我的FeignClient中的name写 cloud-provider1 , 而下面的方法就是你提供者的的方法如(就是我上面的提供者的controller方法)
yml配置文件:
server:
port: 8993
spring:
application:
name: cloud-consumer1
cloud:
nacos:
# 服务注册中心
discovery:
server-addr: localhost:8848
management:
# 端点检查(健康检查)
endpoints:
web:
exposure:
include: "*"
消费者测试远程调用的controller:
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
OpenFeignService1 openFeignService;
@RequestMapping("/test01")
public BigDecimal getBig(){
BigDecimal bigDecimal = openFeignService.test01();
return bigDecimal.add(BigDecimal.ONE);
}
主启动类上加上@EnableFeignClients :
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class CloudConsumer1 {
public static void main(String[] args){
SpringApplication.run(CloudConsumer1.class,args);
}
}
接下来就是测试了,我们先测试提供者的自己的controller效果如下:
接下来测试,消费者远程调用:
到此,openFeign整合成功,如有不对,请指出,谢谢。