DeptController.java
package com.kuang.springcloud.controller; import com.kuang.springcloud.pojo.Dept; import com.kuang.springcloud.service.DeptService; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; //Controller提供restful服务 @RestController //以json格式传输 public class DeptController { @Autowired //这里就体现了controller调service层 private DeptService deptService; @GetMapping("/dept/get/{id}") @HystrixCommand(fallbackMethod = "hystrixGet") //只要失败 就会调用 hystrixGet public Dept get(@PathVariable("id") Long id){ Dept dept = deptService.queryById(id); if(dept==null){ throw new RuntimeException("id=>" + id + ",不存在该用户,或者信息无法找到");//运行时异常 } return dept; } //备选方案 public Dept hystrixGet(@PathVariable("id") Long id){ return new Dept() .setDeptno(id) .setDname("id=>"+id+"没有对应的信息, null--@Hystrix") .setDb_source("no this database in MySQL"); } }dao
DeptDao.java
package com.kuang.springcloud.dao; import com.kuang.springcloud.pojo.Dept; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository //被spring托管 public interface DeptDao { public boolean addDept(Dept dept); public Dept queryById(Long id);//查询部门信息 public List<Dept> queryAll(); }service
DeptService.java
package com.kuang.springcloud.service; import com.kuang.springcloud.pojo.Dept; import java.util.List; public interface DeptService { public boolean addDept(Dept dept); public Dept queryById(Long id); public List<Dept> queryAll(); }DeptServiceImpl.java
package com.kuang.springcloud.service; import com.kuang.springcloud.dao.DeptDao; import com.kuang.springcloud.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class DeptServiceImpl implements DeptService{ @Autowired private DeptDao deptDao; @Override public boolean addDept(Dept dept) { return deptDao.addDept(dept); } @Override public Dept queryById(Long id) { return deptDao.queryById(id); } @Override public List<Dept> queryAll() { return deptDao.queryAll(); } }DeptProviderHystrix_8001.java
package com.kuang.springcloud; //启动类 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient//在服务启动后自动注册到Eureka中 @EnableDiscoveryClient //服务发现 @EnableCircuitBreaker //添加对熔断的支持 public class DeptProviderHystrix_8001 { public static void main(String[] args){ SpringApplication.run(DeptProviderHystrix_8001.class,args); } }resources
mybatis
mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- 一级缓存是默认的 开启二级缓存--> <setting value="true"/> </settings> </configuration>mapper
DeptMapper.xml
<?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"> <mapper namespace="com.kuang.springcloud.dao.DeptDao"> <insert parameterType="Dept"> insert into dept (dname,db_source) values (#{dname},DATABASE()) </insert> <select resultType="Dept" parameterType="Long"> select * from dept where deptno = #{deptno}; </select> <select resultType="Dept"> select * from dept; </select> </mapper>application.yml
server: port: 8001 mybatis: type-aliases-package: com.kuang.springcloud.pojo config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml spring: application: name: springcloud-provider-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # 3个服务名称一致, 只是请求不一样了 driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8 username: root password: 123456 # Eureka的配置,服务注册到哪里 eureka: client: service-url: defaultZone: :7001/eureka/,:7002/eureka/,:7003/eureka/ instance: instance-id: springcloud-provider-dept-hystrix-8001 # 修改eureka上的默认描述信息 # info 配置 info: app.name: kuangshen-springcloud company.name: blog.kuangstudy.com