編寫pom文檔java
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.wang</groupId> <artifactId>SpringCloud</artifactId> <version>1.0-SNAPSHOT</version> <modules> <module>SpringCloud-API</module> <module>SpringCloud-provider-dept-8001</module> </modules> <!--打包方式 pom--> <packaging>pom</packaging> <!--在這裏管理版本號, 方便之後修改--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>14</maven.compiler.source> <maven.complier.target>14</maven.complier.target> <junit.version>4.12</junit.version> <lombok.version>1.18.12</lombok.version> <log4j.version>1.2.17</log4j.version> </properties> <!--dependencyManagement是個管理, 不會導入庫--> <dependencyManagement> <dependencies> <!--SpringCloud的依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <!--SpringBoot的依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--數據庫--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <!--Druid--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!--SpringBoot-Mybatis 啓動器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> </dependency> <!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!--日誌--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> </dependencies> </dependencyManagement> </project>
這裏有如下幾點要注意mysql
編寫Restful風格的API接口web
在API項目中寫實體類spring
在provider中寫提供者的具體實現sql
在微服務中, 個服務對應一個數據庫, 同一個數據可能存在不一樣的數據庫, 所以咱們須要一個字段來代表該數據庫是哪個, 這裏使用db_source字段數據庫
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>SpringCloud</artifactId> <groupId>com.wang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>SpringCloud-API</artifactId> <!--當前的Module本身須要的依賴, 若是父依賴中已經配置了版本, 這裏就不用寫了--> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
注意apache
package com.wang.springcloud.pojo; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import java.io.Serializable; @Data @NoArgsConstructor //支持鏈式寫法 @Accessors(chain = true) public class Dept implements Serializable { private Long deptno; private String dname; //這個數據存在哪一個數據庫的字段 ==> 微服務, 一個服務對應一個數據庫, 同一個數據可能存在不一樣的數據庫 private String db_source; public Dept(String dname) { this.dname = dname; } }
這裏命名規範最好寫上端口號api
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>SpringCloud</artifactId> <groupId>com.wang</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>SpringCloud-provider-dept-8001</artifactId> <dependencies> <!--咱們須要拿到實體類, 因此要配置api module--> <dependency> <groupId>com.wang</groupId> <artifactId>SpringCloud-API</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!--test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> </dependency> <!--web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--jetty--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <!--熱部署工具--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
注意緩存
server: port: 8001 #mybatis配置 mybatis: type-aliases-package: com.wang.springcloud.pojo mapper-locations: classpath:mybatis/mapper/*.xml config-location: classpath:mybatis/mybatis-config.xml #Spring的配置 spring: application: name: springcloud-provider-dept #數據源 datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8 username: root password: 123456
注意安全
<?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 name="cacheEnabled" value="true"/> </settings> </configuration>
package com.wang.springcloud.dao; import com.wang.springcloud.pojo.Dept; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; import java.util.List; @Mapper @Repository public interface DeptDao { boolean addDept(Dept dept); Dept queryById(Long deptno); List<Dept> queryAll(); }
注意
這裏要寫@Mapper和Mybatis綁定
Dao層使用@Repository被SpringBoot接管接口
package com.wang.springcloud.service; import com.wang.springcloud.pojo.Dept; import java.util.List; public interface DeptService { boolean addDept(Dept dept); Dept queryById(Long deptno); List<Dept> queryAll(); }
package com.wang.springcloud.service; import com.wang.springcloud.dao.DeptDao; import com.wang.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 deptno) { return deptDao.queryById(deptno); } @Override public List<Dept> queryAll() { return deptDao.queryAll(); } }
注意
package com.wang.springcloud.controller; import com.wang.springcloud.pojo.Dept; import com.wang.springcloud.service.DeptService; 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.PostMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; //提供RestFul服務! @RestController public class DeptController { @Autowired private DeptService deptService; @PostMapping("/dept/add") public boolean addDept(@RequestBody Dept dept){ return deptService.addDept(dept); } @GetMapping("/dept/get/{id}") public Dept get(@PathVariable("id") Long id){ return deptService.queryById(id); } @GetMapping("/dept/list") public List<Dept> queryAll(){ return deptService.queryAll(); } }
注意
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.wang.springcloud.dao.DeptDao"> <insert id="addDept" parameterType="Dept"> insert into db01.dept (dname, db_source) values (#{dname}, DATABASE()); </insert> <select id="queryById" resultType="Dept" parameterType="Long"> select * from db01.dept where deptno = #{deptno}; </select> <select id="queryAll" resultType="Dept"> select * from db01.dept; </select> </mapper>
注意
package com.wang.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //啓動類 @SpringBootApplication public class DeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(DeptProvider_8001.class, args); } }
注意
server: port: 8080
package com.wang.springcloud.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { //註冊RestTemplate @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } }
package com.wang.springcloud.controller; import com.wang.springcloud.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.List; @RestController public class DeptConsumerController { //消費者, 不該該有service層!(爲了實現解耦) //RestFul模板 ==> RestTemplate, 裏面有不少方法供咱們直接調用, 須要註冊到Spring中 //遠程調用provider的url //在ConfigBean中註冊了, 這裏能夠直接自動裝配(AutoWired是按照類型自動裝配) //RestTemplate的方法 ==> url, 請求的實體(request), 響應的類型(response)Class<T> //RestTemplate實質上就是提供多種便捷訪問遠程http服務的方法, 是一個簡單的RestFul服務模板 @Autowired private RestTemplate restTemplate; //這裏去http://localhost:8001/dept/{id}這個url拿數據 //因爲前綴是固定的, 咱們這裏利用常量寫死 private static final String REST_URL_PREFIX = "http://localhost:8001"; @RequestMapping("/consumer/dept/get/{id}") public Dept get(@PathVariable("id") Long id) { //因爲咱們在provider中的list是get方法, 這裏調用getForObject方法 return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class); } @RequestMapping("/consumer/dept/add") public boolean add(Dept dept){ return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class); } @RequestMapping("consumer/dept/list") public List<Dept> list() { return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class); } }
注意
package com.wang.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DeptConsumer_8080 { public static void main(String[] args) { SpringApplication.run(DeptConsumer_8080.class, args); } }
注意, 測試的時候必定要同時把提供者打開!
這裏利用postman測試