3spring_cloud_store-server(一个商品模块)实际需求可以根据项目来改
主要功能集成数据库,SpringCloud,等操作
3.1、pom:
<dependencies> <!--导入EurekaClient基础依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
<!--服务器之间请求服务--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<!--连接数据库 可以写,可以写在父类,建议写在当前子类 注意版本号 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.26</version> </dependency>
<!--,在SpringBoot启动时,去扫描所有Mapper接口,然后为其增加一个代理实现类,在调用的过程中, 我们实际调用的是这个代理对象 可以写,可以写在父类,建议写在当前子类--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
<!--提取公共类 提取base公共类里获取doamin,这样配置就比较方便--> <dependency> <groupId>com.jd</groupId> <artifactId>spring_cloud_base</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
3.2启动类
@SpringBootApplication //客户端的Eureka @EnableEurekaClient //扫描包路径 @MapperScan("com.jd.mapper") public class StoreStarter { public static void main(String[] args) { SpringApplication.run(StoreStarter.class); } }
3.3三层架构
最好不要抽domain,关联会出问题。
mapper层:加通用注解
//注解,它的作用就是实现bean的注入
@Component,注意是接口
server层是接口,IXXXX是接口,不加注解
XXXimpl是
@Service public class StoreServiceImpl implements IStoreServer(实现类)
conreoller层:加注解
@RestController 添加用的是:@PostMapping("/getinsert"),添加单个是
@GetMapping("/getfindOne/{id}")
application.yml文件配置:
eureka: client: serviceUrl: #注册地址,要和注册中心服务端配置一样 defaultZone: http://localhost:2500/eureka/ instance: prefer-ip-address: true #使用ip注册到注册中心 instance-id: 商品服务:${server.port} #显示在注册中心的实例的id server: port: 10011 spring: application: name: store-server #服务名 datasource: username: root password: java driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///itsourcesstore?useUnicode=true&characterEncoding=UTF-8 //配置别名 mybatis: type-aliases-package: com.jd.domain
xxxmapper文件:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace:接口的完全限定名 --> <mapper namespace="com.jd.mapper.StoreMapper"> <!-- void insert(Store store);--> <insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id"> insert into t_store (product_id,number) values (#{product_id},#{number}); </insert> <!-- Store findOne(Long id);--> <select id="findOne" resultType="com.jd.domain.Store"> select * from t_store where id = #{id} </select> </mapper>
feign配置文件:
xxxfeign文件 @FeignClient(value ="store-server",fallbackFactory = FallbackStore.class)
注意:请求名要一致调用子服务,请求名
public interface StoreFeign { @GetMapping("/getfindOne/{id}") Store findOne(@PathVariable Long id); //http://localhost:10011/getinsert @PostMapping("/getinsert") void insert(@RequestBody Long id);
熔断写法:Fallbackxxx
implements FallbackFactory<StoreFeign>
new StoreFeign()
return Store.builder().id(-1l).number(100L).product_id(18).build();