日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

【Java进阶】初识SpringCloud

發(fā)布時(shí)間:2024/9/30 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Java进阶】初识SpringCloud 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

初識SpringCloud

Spring Cloud 是一套完整的微服務(wù)解決方案,基于 Spring Boot 框架,準(zhǔn)確的說,它不是一個(gè)框架,而是一個(gè)大的容器,它將市面上較好的微服務(wù)框架集成進(jìn)來,從而簡化了開發(fā)者的代碼量。

Spring Cloud 是什么?

Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發(fā)便利性簡化了分布式系統(tǒng)的開發(fā),比如服務(wù)發(fā)現(xiàn)、服務(wù)網(wǎng)關(guān)、服務(wù)路由、鏈路追蹤等。Spring Cloud 并不重復(fù)造輪子,而是將市面上開發(fā)得比較好的模塊集成進(jìn)去,進(jìn)行封裝,從而減少了各模塊的開發(fā)成本。換句話說:Spring Cloud 提供了構(gòu)建分布式系統(tǒng)所需的“全家桶”。

Spring Cloud 現(xiàn)狀

目前,國內(nèi)使用 Spring Cloud 技術(shù)的公司并不多見,不是因?yàn)?Spring Cloud 不好,主要原因有以下幾點(diǎn):

  • Spring Cloud 中文文檔較少,出現(xiàn)問題網(wǎng)上沒有太多的解決方案。
  • 國內(nèi)創(chuàng)業(yè)型公司技術(shù)老大大多是阿里系員工,而阿里系多采用 Dubbo 來構(gòu)建微服務(wù)架構(gòu)。
  • 大型公司基本都有自己的分布式解決方案,而中小型公司的架構(gòu)很多用不上微服務(wù),所以沒有采用 Spring Cloud 的必要性。

但是,微服務(wù)架構(gòu)是一個(gè)趨勢,而 Spring Cloud 是微服務(wù)解決方案的佼佼者.

Spring Cloud 優(yōu)缺點(diǎn)

優(yōu)點(diǎn)

  • 集大成者,Spring Cloud 包含了微服務(wù)架構(gòu)的方方面面。
  • 約定優(yōu)于配置,基于注解,沒有配置文件。
  • 輕量級組件,Spring Cloud 整合的組件大多比較輕量級,且都是各自領(lǐng)域的佼佼者。
  • 開發(fā)簡便,Spring Cloud 對各個(gè)組件進(jìn)行了大量的封裝,從而簡化了開發(fā)。
  • 開發(fā)靈活,Spring Cloud 的組件都是解耦的,開發(fā)人員可以靈活按需選擇組件。

缺點(diǎn)

  • 項(xiàng)目結(jié)構(gòu)復(fù)雜,每一個(gè)組件或者每一個(gè)服務(wù)都需要創(chuàng)建一個(gè)項(xiàng)目。
  • 部署門檻高,項(xiàng)目部署需要配合 Docker 等容器技術(shù)進(jìn)行集群部署,而要想深入了解 Docker,學(xué)習(xí)成本高。

Spring Cloud 的優(yōu)勢是顯而易見的。學(xué)習(xí) Spring Cloud 是一個(gè)不錯(cuò)的選擇。

Spring Cloud 和 Dubbo 對比

Dubbo 只是實(shí)現(xiàn)了服務(wù)治理,而 Spring Cloud 實(shí)現(xiàn)了微服務(wù)架構(gòu)的方方面面,服務(wù)治理只是其中的一個(gè)方面。下面通過一張圖對其進(jìn)行比較:

可以看出,Spring Cloud 比較全面,而 Dubbo 由于只實(shí)現(xiàn)了服務(wù)治理,需要集成其他模塊,需要單獨(dú)引入,增加了學(xué)習(xí)成本和集成成本。

項(xiàng)目結(jié)構(gòu)

父項(xiàng)目pom

<?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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>cn.com.codingce</groupId><artifactId>springcloud</artifactId><version>1.0-SNAPSHOT</version><modules><module>springcloud-api</module><module>springcloud-provider-dept</module><module>springcloud-consumer-dept</module></modules><!--打包方式--><packaging>pom</packaging><!--統(tǒng)一配置 版本號--><properties><junit.version>4.12</junit.version><lombok.version>1.16.10</lombok.version><mysql.version>8.0.11</mysql.version><druid.version>1.1.10</druid.version><logbackcore.version>1.2.3</logbackcore.version></properties><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><scope>2.1.4.RELEASE</scope></dependency><!--連接數(shù)據(jù)庫--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.version}</version></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>${druid.version}</version></dependency><!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core --><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-core</artifactId><version>1.2.3</version></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><version>2.2.2.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jetty --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId><version>2.0.1.RELEASE</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.0.1.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><version>2.1.4.RELEASE</version></dependency></dependencies></dependencyManagement></project>

實(shí)體類

pom

<?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>cn.com.codingce</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>springcloud-api</artifactId><dependencies><!--當(dāng)前module自己需要的依賴, 如果父依賴中已經(jīng)配置了版本--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><!--junit--> <!-- <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></dependencies> </project>

Dept

package cn.com.codingce.pojo;import lombok.experimental.Accessors;import java.io.Serializable;/*** @author xzMa* Dept實(shí)體類 orm 類表關(guān)系映射*/ @Accessors //鏈?zhǔn)綄懛?/span> public class Dept implements Serializable {private Long deptno;private String dname;private String db_source;/*** Dept dept = new Dept();* dept.setDeptNo("").setDeptyName()..... 都行*/public Dept() {}public Dept(Long deptno, String dname, String db_source) {this.deptno = deptno;this.dname = dname;this.db_source = db_source;}public Long getDeptno() {return deptno;}public void setDeptno(Long deptno) {this.deptno = deptno;}public String getDname() {return dname;}public void setDname(String dname) {this.dname = dname;}public String getDb_source() {return db_source;}public void setDb_source(String db_source) {this.db_source = db_source;}@Overridepublic String toString() {return "Dept{" +"deptno=" + deptno +", dname='" + dname + '\'' +", db_source='" + db_source + '\'' +'}';} }

服務(wù)端

pom

<?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>cn.com.codingce</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>springcloud-provider-dept</artifactId><dependencies><!--我們需要拿到實(shí)體類, 所以要配置api -module--><dependency><groupId>cn.com.codingce</groupId><artifactId>springcloud-api</artifactId><version>1.0-SNAPSHOT</version></dependency><!--junit--><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><!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core --><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-test</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- &lt;!&ndash;jetty&ndash;&gt;--> <!-- &lt;!&ndash; https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jetty &ndash;&gt;--><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>

Controller

package cn.com.codingce.controller;import cn.com.codingce.pojo.Dept; import cn.com.codingce.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 javax.annotation.Resource; import java.util.List;/*** @author xzMa** 提供RestFul服務(wù)*/ @RestController public class DeptController {@Resourceprivate DeptService deptService;@PostMapping("/dept/add")public boolean addDept(Dept dept) {return deptService.addDept(dept);}@GetMapping("/dept/get/{id}")public Dept queryBuId(@PathVariable("id") Long id) {return deptService.queryById(id);}@GetMapping("/dept/list")public List<Dept> queryAll() {return deptService.queryAll();}}

DAO (mapper)

package cn.com.codingce.dao;import cn.com.codingce.pojo.Dept; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository;import java.util.List;@Mapper @Repository public interface DeptDao {public boolean addDept(Dept dept);public Dept queryById(Long id);public List<Dept> queryAll();}

Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.com.codingce.dao.DeptDao"><insert id="addDept" parameterType="Dept">insert into dept(dname, db_source) values (#{dname}, DATABASE())</insert><select id="queryById" parameterType="Long" resultType="Dept">select * from dept where deptno = #{deptno}</select><select id="queryAll" resultType="Dept">select * from dept</select></mapper>

Service接口

package cn.com.codingce.service;import cn.com.codingce.pojo.Dept;import java.util.List;public interface DeptService {public boolean addDept(Dept dept);public Dept queryById(Long id);public List<Dept> queryAll(); }

Service實(shí)現(xiàn)類

package cn.com.codingce.service;import cn.com.codingce.dao.DeptDao; import cn.com.codingce.pojo.Dept; import org.springframework.stereotype.Service;import javax.annotation.Resource; import java.util.List;@Service public class DeptServiceImpl implements DeptService {@Resourceprivate DeptDao deptDao;@Overridepublic boolean addDept(Dept dept) {return deptDao.addDept(dept);}@Overridepublic Dept queryById(Long id) {return deptDao.queryById(id);}@Overridepublic List<Dept> queryAll() {return deptDao.queryAll();} }

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> <!--核心配置文件--> <configuration><!--開啟二級緩存--><settings><setting name="cacheEnabled" value="true"/><setting name="logImpl" value="STDOUT_LOGGING"/></settings> </configuration>

application.yml

server:port: 8001#mybatis配置 mybatis:type-aliases-package: cn.com.codingce.pojoconfig-location: classpath:mybatis/mybatis-config.xmlmapper-locations: classpath:mybatis/mapper/*.xml # configuration: # map-underscore-to-camel-case: true# spring配置 spring:application:name: springcloud-provider-deptdatasource:type: com.alibaba.druid.pool.DruidDataSource # 數(shù)據(jù)源 德魯伊driver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://cdb-q9atzwrq.bj.tencentcdb.com:10167/db01?useSSL=trueusername: rootpassword: 123456

客戶端

pom

<?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>cn.com.codingce</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>springcloud-consumer-dept</artifactId><dependencies><!--我們需要拿到實(shí)體類, 所以要配置api -module--><dependency><groupId>cn.com.codingce</groupId><artifactId>springcloud-api</artifactId><version>1.0-SNAPSHOT</version></dependency><!--熱部署工具--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies> </project>

配置文件

package cn.com.codingce.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate;@Configuration public class ConfigBean { //configuration -- spring applicationContext.xml@Beanpublic RestTemplate getRestTemplate() {return new RestTemplate();}}

Controller

package cn.com.codingce.controller;import cn.com.codingce.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 {// 理解消費(fèi)者, 不應(yīng)該有service層//RestFul風(fēng)格//(url, 實(shí)體: Map classs<T> responseType)@Autowiredprivate RestTemplate restTemplate; //提供多種便捷訪問遠(yuǎn)程http服務(wù)的方法private static final String REST_URL_PREFIX = "http://localhost:8001";@RequestMapping("/consumer/dept/add")public boolean add(Dept dept) {return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class);}//http://localhost:8001/dept/list@RequestMapping("/consumer/dept/get/{id}")public Dept get(@PathVariable("id") Long id) {return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class );}@RequestMapping("/consumer/dept/list")public List<Dept> list() {return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list" , List.class );}}

application.yml

server:port: 80

總結(jié)

以上是生活随笔為你收集整理的【Java进阶】初识SpringCloud的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。