在消费者消费的时候,无论怎么样 都无法 找到mapping:
代码的目录结构是这样的:
总共两个工程。一个是ticket属于生产者,里面的service就打印一句话:get a ticket 然后消费者 user工程。测试类调用
package com.liqs.consolver.people; import com.liqs.consolver.people.service.GetTicketService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; @RunWith(SpringRunner.class) @SpringBootTest public class PeopleApplicationTests { @Autowired GetTicketService getTicketService; @Test public void getTicket() { System.out.println( getTicketService.getUserTicket());//调用tichetService打印 } }
注意的是,这里的 文件目录有一个 和ticket一样的结构的ticket接口
service代码:
package com.liqs.consolver.people.service.impl; import com.alibaba.dubbo.config.annotation.Reference; import com.liqs.consolver.people.service.GetTicketService; import com.liqs.producter.ticket.service.TicketService; import org.springframework.stereotype.Service; @Service public class GetTicketServiceImpl implements GetTicketService { @Reference TicketService ticketService; @Override public String getUserTicket() { return ticketService.getTicket(); } }
测试类代码:
package com.liqs.consolver.people; import com.liqs.consolver.people.service.GetTicketService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; @RunWith(SpringRunner.class) @SpringBootTest public class PeopleApplicationTests { @Autowired GetTicketService getTicketService; @Test public void getTicket() { System.out.println( getTicketService.getUserTicket()); } }
调用后,控制台报错
后来偶然发现,原来enablexxx注解里有@EnableDubbo这个属性,把它加上就好了
然后修改后的代码tiket代码 如下
package com.liqs.producter.ticket; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubbo//加入这个注解才有用 public class TicketApplication { public static void main(String[] args) { SpringApplication.run(TicketApplication.class, args); } }
原文:https://www.cnblogs.com/liqs-note/p/11144373.html