首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >编辑接口设计及微服务内部接口调用方式,feign接口调用

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

作者头像
oktokeep
发布2024-11-29 07:53:25
发布2024-11-29 07:53:25
32100
代码可运行
举报
文章被收录于专栏:第三方工具第三方工具
运行总次数:0
代码可运行

编辑接口设计及微服务内部接口调用方式,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类来调用也可以。如下代码:

代码语言:javascript
代码运行次数:0
运行
复制
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);
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-11-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档