🍅我是小宋, 一个只熬夜但不秃头的Java程序员。
🍅关注我,带你过面试,读源码。提升简历亮点(14个demo)
🍅我的面试集已有12W+ 浏览量。
🌏号:tutou123com。拉你进面试专属群。
📖微信公众号:小宋编码
1.OpenFeign
1. 使用openfeign
消费端:
1.导入依赖
2.启动类加@EncableFeignClients注解(开启openfeign)
3.编写feign接口加上@FeignClient注解(绑定服务提供方)
4调用接口
2.依赖
<dependencies>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!--eureka client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--一般基础通用配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.主启动类添加注解
@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class OpenFeignApp
{
public static void main(String[] args)
{
SpringApplication.run(OrderHystrixMain80.class,args);
}
}
4.feign接口
@Component
@FeignClient("SERVICE-PROVIDER")
public interface ProductFeignService {
/**
* 编写技巧,直接把服务提供者那一端的控制器方法的两行拷贝过来,最后加一个分号就可以了,别的什么也不用动
*/
//通过控制层url进行帮订。
@RequestMapping("/provider/product/findAll")
public List<Product> findAll();
@RequestMapping("/provider/product/findByPid")
public String findByPid(@RequestParam("pid") Integer pid);
}
2.Dubbo
- 1.生产者消费者添加dubbo依赖
- 2.使用org.apache.dubbo.config.annotation.Service注解标记要远程引用的service
- 3.消费者使用@Refence注解注入Service
1.生产者消费者添加dubbo依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
2.使用org.apache.dubbo.config.annotation.Service注解标记要远程引用的service
//dubbo里的@Service注解
import org.apache.dubbo.config.annotation.Service;
@Service
public class AppServiceImpl implements IAppService {
@Autowired
AppMapper appMapper;
@Override
public AppDTO createApp(AppDTO appDTO) throws BizException {
return null;
}
}
3.消费者使用@Refence注解注入Service
@Slf4j
@RestController
public class AppController
{
//远程调用
@Reference
IAppService appService;
public AppDTO createApp(@RequestBody AppVo appVo){
return appService.createApp(appDTO);
}
}