eureka微服务之间相互调用

eureka之间相互调用,其实很简单,但还是遇到很多问题,记录一下。首先说下,不要去看那些代码不全的文章,害死人,推荐一个写的比较全的https://blog.csdn.net/weixin_43928997/article/details/90668007

首先,将各种微服务及eureka服务搭建好,然后再被调用的服务中写一个Controller,这些都是常规操作,以上文章上有。

然后,就是调用服务者,引包也看上面文章上即可。我写下必要步骤及注意事项:

1、是启动类(此处注意一定要@EnableFeignClients注解来开启feign)

package com.ymkj.property;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@EnableFeignClients//注意此注解一定要,不然不能开启feigns
@EnableDiscoveryClient
@SpringBootApplication
public class PropertyApplication {

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

}

2、写一个接口(这里有两个注意事项:FeignClient 的 value 值,即被访问的服务名称,这个名称不能使用下划线,不然怎么也调用不到,第二就是一定要是接口,FeignClient注解只能注解在接口上。)

package com.ymkj.property.feign_client;

import com.ymkj.property.entity.response.ResponseBase;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author zhengyue
 * @data 2019-08-09 14:03
 */
@Component
@FeignClient(value="face-server", fallback=FaceConnectionFallBack.class)
public interface FaceManager {

    @RequestMapping(value = "/face/add", method = RequestMethod.POST)
    public ResponseBase addFace(@RequestParam(value = "serialNumbers") String serialNumbers,
                                @RequestParam(value = "name") String name,
                                @RequestParam(value = "sex") String sex,
                                @RequestParam(value = "cardId") String cardId,
                                @RequestParam(value = "start") String start,
                                @RequestParam(value = "end") String end,
                                @RequestParam(value = "imgUrl") String imgUrl);
}

3、在需要的地方调用该类即可

你可能感兴趣的:(eureka微服务之间相互调用)