3spring_cloud_store-server(一个商品模块)实际需求可以根据项目来改
主要功能集成数据库,SpringCloud,等操作
3.1、pom:
org.springframework.cloud spring-cloud-starter-netflix-eureka-client
org.springframework.boot spring-boot-starter-web
mysql mysql-connector-java5.1.26
org.mybatis.spring.boot mybatis-spring-boot-starter1.1.1
com.jd
spring_cloud_base
1.0-SNAPSHOT
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文件:
insert into t_store (product_id,number) values (#{product_id},#{number}); select * from t_store where id = #{id}
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
new StoreFeign()
return Store.builder().id(-1l).number(100L).product_id(18).build();



