最近在研究微服务以及相关组件,搭建了一个工程研究一下服务注册、服务发现、配置管理、feign调用、zuul网关等。
一、工程结构
nacos-happy作为父工程,引入了绝大多数必要的jar包,例如spring-boot-starter-parent、spring-boot-starter-web、spring-cloud-starter-alibaba-nacos-discovery、spring-cloud-starter-alibaba-nacos-config、spring-cloud-starter-openfeign等(备注:初次尝试最好像我一样引入这些包,不要尝试奇奇怪怪的乍一看像但实际不是的包,要不然可能出现jar冲突之类的,很难排查)。
父工程下有三个子工程,均把自己注册到了nacos server中:
schoolService:
端口号55001
服务名school
上下文路径:school
studentService:
端口号55002
服务名student
上下文路径:无
gateway:
端口号55000
服务名gateway
上下文路径:无
二、服务间使用Feign调用
studentService使用feign调用schoolService:
在studentService中新增接口,使用@FeignClient注解,指定服务名为school,编写失败时调用的类,由于school服务有上下文路径,新增配置文件server.other.school.context并配置为了school。
SchoolFeignDao代码如下:
@FeignClient(value = "school",fallback = SchoolFeignDaoFallbackImpl.class,path="${server.other.school.context}")
public interface SchoolFeignDao {
@RequestMapping(method = RequestMethod.GET,value = "/getLocation")
public String getLocation();
@RequestMapping(method = RequestMethod.POST,value = "/getSchoo