飞滴出行网约车2022-service-price使用feign调用service-map,以及解决因参数注解RequestBody传值时GET请求自动转POST

master
yh 3 years ago
parent 831e3eeaaf
commit c0e453b6a5

@ -16,6 +16,12 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -2,10 +2,12 @@ package com.mashibing.servicemap;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient
public class ServiceMapApplication { public class ServiceMapApplication {
public static void main(String[] args) { public static void main(String[] args) {

@ -23,7 +23,6 @@ public class MapDirectionClient {
public DirectionResponse direction(String depLongitude, String depLatitude, String destLongitude, String destLatitude){ public DirectionResponse direction(String depLongitude, String depLatitude, String destLongitude, String destLatitude){
// 组装 url // 组装 url
// https://restapi.amap.com/v3/direction/driving?origin=116.481028,39.989643&destination=116.465302,40.004717&extensions=all&output=json&key=c5a197858bd6d73cf94cf49430fbe9d2
StringBuilder urLBuild = new StringBuilder(); StringBuilder urLBuild = new StringBuilder();
urLBuild.append(AmapConfigConstants.DIRECTION_URL); urLBuild.append(AmapConfigConstants.DIRECTION_URL);
urLBuild.append("?"); urLBuild.append("?");
@ -47,7 +46,7 @@ public class MapDirectionClient {
return directionResponse; return directionResponse;
} }
public DirectionResponse parseDirectionInfo(String directionStr){ private DirectionResponse parseDirectionInfo(String directionStr){
DirectionResponse directionResponse = null; DirectionResponse directionResponse = null;

@ -3,5 +3,9 @@ server:
spring: spring:
application: application:
name: service-map name: service-map
cloud:
nacos:
discovery:
server-addr: localhost:8848
amap: amap:
key: c5a197858bd6d73cf94cf49430fbe9d2 key: c5a197858bd6d73cf94cf49430fbe9d2

@ -16,6 +16,26 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<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-openfeign</artifactId>
</dependency>
<!-- feign 组件 基于 loadbalancer 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!--解决 feign组件 远程调用接口RequestBody参数注解GET请求自动转POST-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
</dependencies> </dependencies>

@ -2,8 +2,12 @@ package com.mashibing.serviceprice;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication @SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServicePriceApplication { public class ServicePriceApplication {
public static void main(String[] args) { public static void main(String[] args) {

@ -0,0 +1,20 @@
package com.mashibing.serviceprice.remote;
import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.request.ForecastPriceDTO;
import com.mashibing.internalcommon.response.DirectionResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Service
@FeignClient("service-map")
public interface ServiceMapClient {
@RequestMapping(method = RequestMethod.GET,value = "/direction/driving")
public ResponseResult<DirectionResponse> driving(@RequestBody ForecastPriceDTO forecastPriceDTO);
}

@ -1,14 +1,21 @@
package com.mashibing.serviceprice.service; package com.mashibing.serviceprice.service;
import com.mashibing.internalcommon.dto.ResponseResult; import com.mashibing.internalcommon.dto.ResponseResult;
import com.mashibing.internalcommon.request.ForecastPriceDTO;
import com.mashibing.internalcommon.response.DirectionResponse;
import com.mashibing.internalcommon.response.ForecastPriceResponse; import com.mashibing.internalcommon.response.ForecastPriceResponse;
import com.mashibing.serviceprice.remote.ServiceMapClient;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Slf4j @Slf4j
@Service @Service
public class ForecastPriceService { public class ForecastPriceService {
@Autowired
private ServiceMapClient serviceMapClient;
/** /**
* *
* @param depLongitude * @param depLongitude
@ -24,13 +31,23 @@ public class ForecastPriceService {
log.info("目的地经度:"+destLongitude); log.info("目的地经度:"+destLongitude);
log.info("目的地纬度:"+destLatitude); log.info("目的地纬度:"+destLatitude);
ForecastPriceDTO priceDTO = new ForecastPriceDTO();
priceDTO.setDepLatitude(depLatitude);
priceDTO.setDepLongitude(depLongitude);
priceDTO.setDestLatitude(destLatitude);
priceDTO.setDestLongitude(destLongitude);
// 调用 地图服务,查询距离时长 // 调用 地图服务,查询距离时长
ResponseResult<DirectionResponse> driving = serviceMapClient.driving(priceDTO);
Integer distance = driving.getData().getDistance();
Integer duration = driving.getData().getDuration();
// 读取计价规则 System.out.println("distance = " + distance);
System.out.println("duration = " + duration);
// 根据 距离时长 进行价格预估 // 读取计价规则
// 根据 距离时长 进行价格预估
ForecastPriceResponse priceResponse = new ForecastPriceResponse(); ForecastPriceResponse priceResponse = new ForecastPriceResponse();
priceResponse.setPrice(12.34); priceResponse.setPrice(12.34);

@ -2,4 +2,8 @@ server:
port: 8084 port: 8084
spring: spring:
application: application:
name: service-price name: service-price
cloud:
nacos:
discovery:
server-addr: localhost:8848
Loading…
Cancel
Save