Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to invoke SpringCloud remote service

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to invoke SpringCloud remote service, the article is very detailed, has a certain reference value, interested friends must read it!

Notes

In micro services, if you want to use remote calls, you need to introduce spring-cloud-starter-openfeign (in the case of using a registry)

Org.springframework.cloud spring-cloud-starter-openfeign xxx

Because open-feign is a declarative remote call, you need to write an interface and tell SpringCloud that the interface needs to invoke the remote service. I put this interface in the feign under the common module.

Package top.ctong.gulimall.common.feign;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import top.ctong.gulimall.common.utils.R;import java.util.Map @ FeignClient ("gulimall-coupon") @ RequestMapping ("/ coupon/coupon") public interface CouponFeignService {/ * Custom parameters * @ return R * @ author Clover You * @ date 09:11 on 2021-11-17 * / @ RequestMapping ("/ list") R list (@ RequestParam Map params);}

@ FeignClient ("xxx") this annotation is used to tell SpringCloud that this is a remote invocation interface, where value is the name of the service you registered with the registry.

The signature of the interface method needs to be consistent with the signature of the remote service being called, such as the remote service to be invoked by which of the above code:

Package top.ctong.gulimall.coupon.controller;@RestController@RequestMapping ("coupon/coupon") public class CouponController {@ Autowired private CouponService couponService; / * list * / @ RequestMapping ("/ list") / / @ RequiresPermissions ("coupon:coupon:list") public R list (@ RequestParam Map params) {PageUtils page = couponService.queryPage (params); return R.ok (). Put ("page", page);}}

Then use @ EnableFeignClients to open the remote invocation of the service that currently needs to be called remotely, and specify which package your remote invocation interface is under.

@ EnableFeignClients (basePackages = "top.ctong.gulimall.common.feign") @ EnableDiscoveryClient@MapperScan ("top.ctong.gulimall.member.dao") @ SpringBootApplicationpublic class GulimallMemberApplication {public static void main (String [] args) {SpringApplication.run (GulimallMemberApplication.class, args);}}

@ EnableFeignClients this annotation is used to enable the remote invocation function of the current service

BasePackages is used to specify the package where the remote invocation interface is located, so that it can be quickly scanned when the service is started. He can receive multiple package names because it is a String []

Finally, when the remote call is needed, just inject the corresponding remote call interface.

Package top.ctong.gulimall.member.controller;@RestController@RequestMapping ("member/member") public class MemberController {private final CouponFeignService couponFeignService; @ Autowired public MemberController (CouponFeignService couponFeignService, MemberService memberService) {this.couponFeignService = couponFeignService; this.memberService = memberService;} @ RequestMapping ("/ testFeignInvoke") public R testFeignInvoke () {Map parem = new HashMap (10); return couponFeignService.list (parem);}} error (nacos)

If No Feign Client for loadBalancing defined appears at startup. Did you forget to include spring-cloud-starter-loadbalancer? Error, then your SpringCloud version is higher, in the higher version of SpringCloud no longer use spring-cloud-starter-netflix-ribbon, but use spring-cloud-starter-loadbalancer. And nacos still uses spring-cloud-starter-netflix-ribbon.

There is nothing wrong with introducing spring-cloud-starter-loadbalancer into the pom.xml file and then starting it.

Org.springframework.cloud spring-cloud-starter-loadbalancer 3.0.4

An AbstractMethodError exception occurred during a test remote call. Ribbon introduced in nacos needs to be excluded from pom.xml. Otherwise, spring-cloud-starter-loadbalancer will not work.

Com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.netflix.ribbon ribbon above is all the contents of the article "how to invoke SpringCloud remote Services". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report