maven镜像
<mirror>
<id>huaweicloud</id>
<url>https://repo.huaweicloud.com/repository/maven</url>
<mirrorOf>central</mirrorOf>
</mirror>
eureka-server:
yml:
server:
port: 5051
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: http://localhost:5051/eureka
register-with-eureka: false
fetch-registry: false
main方法:
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
公共项目public:
pom.xml分页依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
实体类:
public class Category implements Serializable {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Entry implements Serializable {
private int id;
private int categoryId;
private String title;
private String summary;
private String uploaduser;
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date createdate;
private String categoryName;
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCategoryId() {
return categoryId;
}
public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSummary() {
return summary;
}
public void setSummary(String summary) {
this.summary = summary;
}
public String getUploaduser() {
return uploaduser;
}
public void setUploaduser(String uploaduser) {
this.uploaduser = uploaduser;
}
public Date getCreatedate() {
return createdate;
}
public void setCreatedate(Date createdate) {
this.createdate = createdate;
}
}
remote接口:
public interface CategoryRemote {
@GetMapping("allCategory")
BizVo<List<Category>> allCategory();
}
public interface EntryRemote {
@RequestMapping("entryByPage")
BizVo<PageInfo<Entry>> entryByPage(@RequestBody Map<String,Object> params);
@RequestMapping("addEntry")
BizVo<Boolean> addEntry(@RequestBody Entry entry);
@RequestMapping("updateEntry")
BizVo<Boolean> updateEntry(@RequestBody Entry entry);
}
工具类:
public class BizVo<T> {
private int code;
private String msg;
private T payload;
public BizVo() {
}
public BizVo(int code, String msg, T payload) {
this.code = code;
this.msg = msg;
this.payload = payload;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getPayload() {
return payload;
}
public void setPayload(T payload) {
this.payload = payload;
}
}
项目:
分页依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
yml:
server:
port: 8081
spring:
application:
name: edoc-entry
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/edoc?characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: root
feign:
hystrix:
enabled: true
mybatis:
type-aliases-package: org.example.entity
mapper-locations: mapper/*.xml
eureka:
client:
service-url:
defaultZone: http://localhost:5051/eureka
dao层:
@Mapper
@Repository
public interface EntryMapper {
List<Entry> entryByPage(Map<String,Object> params);
int addEntry(Entry entry);
int updateEntry(Entry entry);
}
@Mapper
@Repository
public interface CategoryMapper {
List<Category> selectAll();
}
mapper
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.edocentry.dao.EntryMapper">
<select id="entryByPage" resultType="org.example.entity.Entry">
select * from `edoc-entry` where 1=1
<if test="categoryId!=null and categoryId!=0">
and categoryId = #{categoryId}
</if>
order by id desc
</select>
<insert id="addEntry">
insert into `edoc-entry` (categoryId,title,summary,uploaduser,createdate) value (#{categoryId},#{title},#{summary},#{uploaduser},#{createdate})
</insert>
<update id="updateEntry">
update `edoc-entry` set categoryId=#{categoryId},title=#{title},summary=#{summary},uploaduser=#{uploaduser},createdate=#{createdate}
where id=#{id}
</update>
</mapper>
service:
public interface EntryService {
PageInfo<Entry> entryByPage(Map<String,Object> params);
boolean addEntry(Entry entry);
boolean updateEntry(Entry entry);
}
实现类:
@Service
public class EntryServiceImpl implements EntryService {
@Autowired
private EntryMapper entryMapper;
@Autowired
private CategoryClient categoryClient;
public PageInfo<Entry> entryByPage(Map<String,Object> params){
PageHelper.startPage(params);
List<Entry> list = entryMapper.entryByPage(params);
System.out.println(categoryClient.allCategory());
List<Category> categoryList = categoryClient.allCategory().getPayload();
for(Entry entry:list){
if(categoryList!=null){
for(Category category : categoryList){
if(entry.getCategoryId()==category.getId()){
entry.setCategoryName(category.getName());
break;
}
}
}
}
return new PageInfo<>(list);
}
public boolean addEntry(Entry entry){
if(entryMapper.addEntry(entry)>0){
return true;
}
return false;
}
public boolean updateEntry(Entry entry){
if(entryMapper.updateEntry(entry)>0){
return true;
}
return false;
}
}
controller:
@RestController
public class EntryController implements EntryRemote {
@Autowired
private EntryService entryService;
@Override
public BizVo<PageInfo<Entry>> entryByPage(Map<String, Object> params) {
BizVo<PageInfo<Entry>> bizVo = new BizVo<>(200,"success",entryService.entryByPage(params));
return bizVo;
}
@Override
public BizVo<Boolean> addEntry(Entry entry) {
BizVo<Boolean> bizVo = new BizVo<>(200,"success",entryService.addEntry(entry));
return bizVo;
}
@Override
public BizVo<Boolean> updateEntry(Entry entry) {
BizVo<Boolean> bizVo = new BizVo<>(200,"success",entryService.updateEntry(entry));
return bizVo;
}
}
远程调用:
@FeignClient(name="edoc-category",fallbackFactory = CategoryClientFallback.class)
@Component
public interface CategoryClient extends CategoryRemote {
}
FallBack执行熔断
@Component
public class CategoryClientFallback implements FallbackFactory<CategoryRemote> {
@Override
public CategoryRemote create(Throwable throwable) {
System.out.println("执行熔断");
return new CategoryRemote() {
@Override
public BizVo<List<Category>> allCategory() {
return new BizVo<>(5500,"服务器暂不可用",null);
}
};
}
}
main方法:
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class EdocEntryApplication {
public static void main(String[] args) {
SpringApplication.run(EdocEntryApplication.class, args);
}
}