<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文主要是建立兩個springboot服務,以在該系列後面的教學中增添springcloud相關元件
修改為如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <packaging>pom</packaging> <modules> <module>openfeign-stu</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.wq</groupId> <artifactId>springcloud-stu</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springcloud-stu</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
<?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-stu</artifactId> <groupId>com.wq</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>openfeign-stu</artifactId> <packaging>pom</packaging> <modules> <module>feign-provider</module> <module>feign-consumer</module> <module>feign-api</module> </modules> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.2.2</version> </dependencies> </project>
feign-api的專案清單如下:
Result的內容如下:
package com.wq.feign.api.domain; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class Result { private int code; private String msg; private Object data; }
pom.xml內容就是建立時的內容,不做改動
完善後的專案結構如下:
修改為如下:
<?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>openfeign-stu</artifactId> <groupId>com.wq</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>feign-provider</artifactId> <dependencies> <dependency> <groupId>com.wq</groupId> <artifactId>feign-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
package com.wq.feign.provider.controller; import com.wq.feign.api.domain.Result; import com.wq.feign.provider.domain.entity.Product; import com.wq.feign.provider.service.ProductService; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; @RestController @Slf4j @RequestMapping("/") public class ProductController { @Resource private ProductService productService; /** * 查詢 * @param id * @return */ @GetMapping("product/provider/get/{id}") public Result selectById(@PathVariable("id") Long id){ return new Result(200, "查詢成功", productService.selectById(id)); } * 刪除 @GetMapping("product/provider/delete/{id}") public Result deleteById(@PathVariable("id") Long id){ return new Result(200, "刪除成功", productService.deleteById(id)); * 修改 * @param product @PostMapping("product/provider/update") public Result updateById(@RequestBody Product product){ return new Result(200, "修改成功", productService.updateById(product.getId(), product.getName())); * 新增 @PutMapping( "product/provider/add") public Result insertById(@RequestBody Product product){ return new Result(200, "修改成功", productService.insertOne(product)); }
package com.wq.feign.provider.dao; import com.wq.feign.provider.domain.entity.Product; import org.apache.ibatis.annotations.Param; import org.springframework.stereotype.Repository; @Repository public interface ProductMapper { /** * 查詢 * @param id * @return */ public Product selectById(@Param("id") Long id); * 刪除 public int deleteById(@Param("id") Long id); * 修改 * @param name public int updateById(@Param("id") Long id, @Param("name") String name); * 新增 * @param product public int insertOne(Product product); }
package com.wq.feign.provider.domain.entity; import lombok.Data; @Data public class Product { private Long id; private String name; private int stock; }
package com.wq.feign.provider.service.impl; import com.wq.feign.provider.dao.ProductMapper; import com.wq.feign.provider.domain.entity.Product; import com.wq.feign.provider.service.ProductService; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service public class ProductServiceImpl implements ProductService { @Resource private ProductMapper productMapper; public Product selectById(Long id) { return productMapper.selectById(id); } public int deleteById(Long id) { return productMapper.deleteById(id); public int updateById(Long id, String name) { return productMapper.updateById(id, name); public int insertOne(Product product) { return productMapper.insertOne(product); }
package com.wq.feign.provider.service; import com.wq.feign.provider.domain.entity.Product; /** * 商品服務類 */ public interface ProductService { /** * 查詢 * @param id * @return */ public Product selectById(Long id); * 刪除 public int deleteById(Long id); * 修改 * @param name public int updateById(Long id, String name); * 新增 * @param product public int insertOne(Product product); }
package com.wq.feign.provider; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.wq.feign.provider.dao") public class ProviderApplication { public static void main(String[] args) { SpringApplication.run(ProviderApplication.class, args); } }
<?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.wq.feign.provider.dao.ProductMapper"> <resultMap id="BaseResultMap" type="com.wq.feign.provider.domain.entity.Product"> <result column="id" jdbcType="BIGINT" property="id" /> <result column="name" jdbcType="VARCHAR" property="name" /> <result column="stock" jdbcType="INTEGER" property="stock" /> </resultMap> <!--查詢--> <select id="selectById" resultType="com.wq.feign.provider.domain.entity.Product"> select * from product where id = #{id} </select> <!--刪除--> <delete id="deleteById" parameterType="java.lang.Long"> delete from product where id = #{id} </delete> <!--修改--> <update id="updateById" parameterType="com.wq.feign.provider.domain.entity.Product"> update product set name = #{name} where id = #{id} </update> <!--新增--> <insert id="insertOne" parameterType="com.wq.feign.provider.domain.entity.Product"> insert into product(name, stock) values (#{name}, #{stock}) </insert> </mapper>
server: port: 8081 spring: application: name: feign-provider-8081 datasource: url: jdbc:mysql://localhost:3306/springcloud-stu?useUnicode=true&characterEncoding=utf8&userSSL=false driverClassName: com.mysql.cj.jdbc.Driver username: root password: 123456 mybatis: mapper-locations: classpath:mapper/*Mapper.xml # Mybatis 對映檔案位置 type-aliases-package: com.wq.feign.provider.domain.entity # 表對應的實體類包
DROP TABLE IF EXISTS `product`; CREATE TABLE `product` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL, `stock` int(10) UNSIGNED NULL DEFAULT NULL, PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of product INSERT INTO `product` VALUES (1, '華為 mate10', 10); INSERT INTO `product` VALUES (2, '華為 mate20', 20); INSERT INTO `product` VALUES (3, '華為 mate30', 30);
完善後的專案結構如下
<?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>openfeign-stu</artifactId> <groupId>com.wq</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>feign-consumer</artifactId> <dependencies> <dependency> <groupId>com.wq</groupId> <artifactId>feign-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
package com.wq.feign.consumer.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class RestConfig { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
package com.wq.feign.consumer.controller; import com.wq.feign.api.domain.Result; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController public class ProductConsumerController { @Resource RestTemplate restTemplate; public static String url = "http://localhost:8081/"; /** * 查詢 * @param id * @return */ @GetMapping("product/consumer/get/{id}") public Result selectById(@PathVariable("id") Long id){ return restTemplate.getForObject(url+"product/provider/get/"+id, Result.class); } }
package com.wq.feign.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(exclude={DataSourceAutoConfiguration.class}) public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }
server: port: 8082 spring: application: name: feign-consumer-8082
啟動ProviderApplication和ConsumerApplication
使用瀏覽器存取,得到結果如下:
我們至此建立了一個多模組的專案,然後有一個商品服務提供者feign-provider-8081 和一個商品服務消費者feign-consumer-8082 。
然後這就是最簡單的兩個微服務了,實現了功能的解耦。
但是這個簡單的微服務存在著很多問題:
比如都用公共的實體類Result,還有提供者地址在消費者裡面寫死了等等,這些問題,我們接下來會一一解決。
到此這篇關於教你建立springcloud微服務的基礎子服務的文章就介紹到這了,更多相關springcloud微服務內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45