版本說明:java
springcloud:Greenwich.SR3
springboot:2.1.8mysql
新建父工程microservicecloud,切記是Packageing是pom模式web
此maven project目的:主要是定義POM文件,將後續各個子模塊公用的jar包等統一提出來,相似一個抽象父類spring
pom.xmlsql
<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.jiatp.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <log4j.version>1.2.17</log4j.version> <lombok.version>1.18.10</lombok.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR3</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.8.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.4</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.20</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> </dependencyManagement> <build> <finalName>microservicecloud</finalName> <resources> <resource> <!--以$ 開頭 在src/main/resources目錄下的 都能讀取 --> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <delimiters> <delimit>$</delimit> </delimiters> </configuration> </plugin> </plugins> </build> <modules> <module>microservicecloud-api</module> <module>microservicecloud-provider-dept-8001</module> <module>microservicecloud-consumer-dept-80</module> </modules> </project>
右擊父工程microservicecloud新建maven module,Packageing是jar模式,建立完成後請回到父工程查看pom文件。數據庫
pom.xmlapache
<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> <parent> <!-- 子類裏面顯示聲明纔能有明確的繼承表現,無心外就是父類的默認版本不然本身定義 --> <groupId>com.jiatp.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <!-- 當前Module我本身叫什麼名字 --> <artifactId>microservicecloud-api</artifactId> <dependencies><!-- 當前Module須要用到的jar包,按本身需求添加,若是父類已經包含了,能夠不用寫版本號 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
新建部門Entity且配合lombok使用api
package com.atguigu.springcloud.entities; import java.io.Serializable; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; @SuppressWarnings("serial") @NoArgsConstructor @Data @Accessors(chain=true) public class Dept implements Serializable //必須序列化 { private Long deptno; //主鍵 private String dname; //部門名稱 private String db_source;//來自那個數據庫,由於微服務架構能夠一個服務對應一個數據庫,同一個信息被存儲到不一樣數據庫 public Dept(String dname) { super(); this.dname = dname; } }
右鍵microservicecloud-api選擇Run as ,先mvn clean 後maven install後給其它模塊引用,達到通用目的。
也即須要用到部門實體的話,不用每一個工程都定義一份,直接引用本模塊便可。緩存
一、新建microservicecloud-provider-dept-8001 提供者Module,同理springboot
二、pom.xml
<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> <parent> <groupId>com.jiatp.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microservicecloud-provider-dept-8001</artifactId> <dependencies> <dependency><!-- 引入本身定義的api通用包,可使用Dept部門Entity --> <groupId>com.jiatp.springcloud</groupId> <artifactId>microservicecloud-api</artifactId> <version>${project.version}</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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- 修改後當即生效,熱部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
三、application.yml
server: port: 8001 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路徑 type-aliases-package: com.jiatp.springcloud.entity # 掃描此包下的entity ->全部entity別名類所在包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件 spring: application: name: microservicecloud-dept # 很重要,對外暴露的微服務名字 datasource: type: com.alibaba.druid.pool.DruidDataSource # 數據源類型 driver-class-name: org.gjt.mm.mysql.Driver # 數據庫驅動包 url: jdbc:mysql://localhost:3306/cloudDB01?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT username: root password: 123456 dbcp2: min-idle: 5 # 數據庫鏈接池的最小維持鏈接數 initial-size: 5 # 初始化鏈接數 max-idle: 5 # 最大鏈接數
四、工程src/main/resources目錄下新建mybatis文件夾後新建mybatis.cfg.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 name="cacheEnabled" value="true"/><!-- 二級緩存開啓 --> </settings> </configuration>
四、建立cloudDB01 數據庫,新建dept表而且插入數據
五、編寫DeptMapper接口
package com.jiatp.springcloud.dao; import java.util.List; import org.apache.ibatis.annotations.Mapper; import com.jiatp.springcloud.entity.Dept; @Mapper public interface DeptDao { public boolean addDept(Dept dept); public Dept findById(Long id); public List<Dept> findAll(); }
六、工程src/main/resources/mybatis目錄下新建mapper文件夾後再建DeptMapper.xml
注意:這裏若是採用註解方式,能夠直接在DeptMapper接口上直接寫sql語句
<?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.jiatp.springcloud.dao.DeptDao"> <select id="findById" resultType="Dept" parameterType="Long"> select deptno,dname,db_source from dept where deptno=#{deptno}; </select> <select id="findAll" resultType="Dept"> select deptno,dname,db_source from dept; </select> <insert id="addDept" parameterType="Dept"> INSERT INTO dept(dname,db_source) VALUES(#{dname},DATABASE()); </insert> </mapper>
七、DeptService和DeptServiceImpl部門服務接口和實現類
package com.jiatp.springcloud.service; import java.util.List; import com.jiatp.springcloud.entity.Dept; public interface DeptService { public boolean add(Dept dept); public Dept get(Long id); public List<Dept> list(); } //--------------------------------------------- package com.jiatp.springcloud.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.jiatp.springcloud.dao.DeptDao; import com.jiatp.springcloud.entity.Dept; import com.jiatp.springcloud.service.DeptService; @Service public class DeptServiceImpl implements DeptService{ @Autowired private DeptDao deptdao; @Override public boolean add(Dept dept) { // TODO Auto-generated method stub return deptdao.addDept(dept); } @Override public Dept get(Long id) { // TODO Auto-generated method stub return deptdao.findById(id); } @Override public List<Dept> list() { // TODO Auto-generated method stub return deptdao.findAll(); } }
八、編寫controller
@RestController public class DeptController { @Autowired private DeptService service; @RequestMapping(value="/dept/add",method=RequestMethod.POST) public boolean add(@RequestBody Dept dept) { return service.add(dept); } @RequestMapping(value="/dept/get/{id}",method=RequestMethod.GET) public Dept get(@PathVariable("id") Long id) { return service.get(id); } @RequestMapping(value="/dept/list",method=RequestMethod.GET) public List<Dept> list() { return service.list(); } }
九、編寫DeptProvider8001_App主啓動類
@SpringBootApplication public class DeptProvider8001_App { public static void main(String[] args) { SpringApplication.run(DeptProvider8001_App.class, args); } }
測試:http://localhost:8001/dept/get/2
一、新建microservicecloud-consumer-dept-80,同理
二、pom.xml
<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> <parent> <groupId>com.jiatp.springcloud</groupId> <artifactId>microservicecloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>microservicecloud-consumer-dept-80</artifactId> <description>部門微服務消費者</description> <dependencies> <dependency><!-- 本身定義的api --> <groupId>com.jiatp.springcloud</groupId> <artifactId>microservicecloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 修改後當即生效,熱部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
三、yml配置
server: port: 80
四、com.jiatp.springcloud包下ConfigBean的編寫
相似spring裏面的applicationContext.xml寫入的注入Bean
@Configuration public class ConfigBean { //優化spring,從spring(applicationContext.xml)== @Configuration配置 @Bean @LoadBalanced //開啓負載均衡 public RestTemplate restTemplate(){ return new RestTemplate(); }
RestTemplate提供了多種便捷訪問遠程Http服務的方法, 是一種簡單便捷的訪問restful服務模板類,是Spring提供的用於訪問Rest服務的客戶端模板工具集。 用法:使用restTemplate訪問restful接口很是的簡單粗暴無腦。
(url, requestMap, ResponseBean.class)這三個參數分別表明
REST請求地址、請求參數、HTTP響應轉換被轉換成的對象類型。
五、controller編寫
@RestController public class DeptController_Consumer { private static final String REST_URL_PREFIX = "http://localhost:8001"; @Autowired private RestTemplate restTemplate; @RequestMapping(value="/consumer/dept/add") public boolean add(Dept dept) { return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add", dept, Boolean.class); } @RequestMapping(value="/consumer/dept/get/{id}") public Dept get(@PathVariable("id") Long id) { return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id, Dept.class); } @SuppressWarnings("unchecked") @RequestMapping(value="/consumer/dept/list") public List<Dept> list() { return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class); } }
六、DeptConsumer80_App主啓動類
@SpringBootApplication public class DeptConsumer80_App { public static void main(String[] args) { SpringApplication.run(DeptConsumer80_App.class, args); } }
測試:http://localhost/consumer/dept/list