编辑接口设计及微服务内部接口调用方式,feign接口调用

编辑接口设计及微服务内部接口调用方式,feign接口调用

1.根据ID新增修改接口
根据ID来区分,有值则认为是修改,否则是新增。 新增接口 /add
基本原则:编辑修改接口是基于ID来修改操作。


2.改成ip方式,而不是网关,否则报错:网关登录失效 内部服务之间的接口调用ip:端口方式,而不是网关的方式
网关的访问更多的是对外部。对内部是ip:端口方式。不经过网关服务。
myUserService.url = http://xxx:xx


3.feign使用,前提是微服务之间都使用了同一套服务注册。原则:未服务注册,不使用feign

##注册服务端口方式
spring.application.name = my-product-server
server.port=1418 //eureka注册上面的名称: hz-auto-tomcat-pro-14:my-product-server:1418 , hz-auto-tomcat-pro-13:my-product-server:1418
##注册服务名称方式(不是端口号,是服务名称)
spring.application.name = myUserService //eureka注册上面的名称(最后没有端口号): hz-auto-tomcat-pro-16:myUserService , hz-auto-tomcat-pro-15:myUserService

如果当前maven服务 未服务注册,最终还是不能使用feign方式。可以使用第2条的ip方式通过RestTemplate exchange方式来发起GET/POST调用。
可以通过服务提供方提供feign api jar包提供出去。也可以在服务调用方发起方,手动编写feign类来调用也可以。如下代码:

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(name = "my-user-server")
public interface FeignMyUserService {

    @PostMapping("/my/user/save")
    public ResponseData save(@RequestBody MyUserRequestVO vo);

}




//
public class ResponseData<T>  {
    @AutoDocProperty("返回代码")
    private String resCode;
    @AutoDocProperty("返回消息")
    private String resMsg;
    @AutoDocProperty("返回实体")
    private T data;
}


#注册feign

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;


@EnableFeignClients({"com.user","com.product"})
@EnableEurekaClient
@SpringBootApplication(scanBasePackages = {"com.user.order","com.product.order","com.product"})
@MapperScan({"com.product.order"})
public class MyUserServerSpringBoot extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(MyUserServerSpringBoot.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(MyUserServerSpringBoot.class);
    }
}

 

posted on 2024-11-27 22:21  oktokeep  阅读(67)  评论(0)    收藏  举报