■説明
SpringCloudの初心者に向け、詳しくの配置流れを紹介します。
■ライブラリ
・OracleのJDK:17
・SpringCloud:2021.0.8
・SpringBoot:2.7.17
・MySql:8.0.33
・Druid:1.2.20
・MyBatis:2.3.1
・LogbackCore:1.3.11
・Lombok:1.18.22
・Log4j:1.2.17
・Junit:5.9.1
・Gradle:8
■構造
■目録説明
・spring-cloud-wk:親項目(Project)、共通のpluginを導入する(JDK、フレームワーク、子項目の共通設定等)
・cloud-server:子項目(Module)、Object(Entity)を設定
・cloud-provider:子項目(Module)、サービスの提供者として
・cloud-consumer:子項目(Module)、サービスの消費者として、「POM.xml」に提供者のURLを配置する
■「build.gradle」の配置
・親「spring-cloud-wk」
buildscript { repositories { mavenCentral() } dependencies { classpath 'io.spring.gradle:dependency-management-plugin:1.0.11.RELEASE' classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.7.17' } } plugins { id 'java' } ‘ group = 'com.lnsylt' version = '1.0-SNAPSHOT' archivesBaseName = 'cloud-parent' java { sourceCompatibility = JavaVersion.VERSION_17 targetCompatibility = JavaVersion.VERSION_17 } repositories { mavenCentral() } dependencies { testImplementation platform('org.junit:junit-bom:5.9.1') testImplementation 'org.junit.jupiter:junit-jupiter' } test { useJUnitPlatform() } //设定字体UTF-8 allprojects { tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } } //为所有子项目设置共通的配置,子项目自动继承父项目设定的内容 subprojects { apply plugin: 'java' repositories { mavenCentral() } ext { springbootVersion = '2.7.17' springcloudVersion = '2021.0.8' lombokVersion = '1.18.22' log4jVersion = '1.2.17' logbackcoreVersion = '1.3.11' junitVersion = '5.9.1' } // 添加Spring Boot插件 apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' // 添加Spring Boot依赖 dependencies { implementation "org.springframework.boot:spring-boot-starter:${springbootVersion}" compileOnly "org.projectlombok:lombok:${lombokVersion}" annotationProcessor "org.projectlombok:lombok:${lombokVersion}" implementation "log4j:log4j:${log4jVersion}" } //spring-cloud-starter-gateway与spring-boot-starter-web不兼容 // dependencies { // implementation 'org.springframework.cloud:spring-cloud-starter-gateway' // } // 添加测试依赖 dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:${junitVersion}' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:${junitVersion}' } // 添加Spring Cloud依赖 dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springcloudVersion}" } } }親
・子「cloud-server」
plugins { id 'java' } group = 'com.lnsylt' version = '1.0-SNAPSHOT' archivesBaseName = 'cloud-server' dependencies { testImplementation "org.junit.jupiter:junit-jupiter-api:${project.junitVersion}" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${project.junitVersion}" }子「server」
・子「cloud-provider」
plugins { id 'java' } group = 'com.lnsylt' version = '1.0-SNAPSHOT' archivesBaseName = 'cloud-provider' ext { mysqlVersion = '8.0.33' druidVersion = '1.2.20' mybatisVersion = '2.3.1' } dependencies { implementation project(':cloud-server') } dependencies{ implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-test' developmentOnly 'org.springframework.boot:spring-boot-devtools' implementation "mysql:mysql-connector-java:${mysqlVersion}" implementation "com.alibaba:druid:${druidVersion}" implementation "org.mybatis.spring.boot:mybatis-spring-boot-starter:${mybatisVersion}" implementation "ch.qos.logback:logback-core:${logbackcoreVersion}" } dependencies { testImplementation "org.junit.jupiter:junit-jupiter-api:${project.junitVersion}" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${project.junitVersion}" }子「provider」
・子「cloud-consumer」
plugins { id 'java' } group = 'com.lnsylt' version = '1.0-SNAPSHOT' archivesBaseName = 'cloud-consumer' dependencies { implementation project(':cloud-server') } dependencies{ implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-test' developmentOnly 'org.springframework.boot:spring-boot-devtools' } dependencies { testImplementation "org.junit.jupiter:junit-jupiter-api:${project.junitVersion}" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${project.junitVersion}" }子「consumer」
■「application.yaml」の配置
・子「cloud-provider」
#端口+路径 server: port: 8001 servlet: context-path: /provider #配置Mybatis的环境 mybatis: type-aliases-package: com.lnsylt.springcloud.pojo config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml spring: #调用application-dev.yaml profiles: active: dev #定义系统的名称 application: name: cloud-provider #配置数据信息(数据源) datasource: type: com.alibaba.druid.pool.DruidDataSource #数据源 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/db01?useSSL=false&useUnicode=true&characterEncoding=utf-8 username: root password: password #配置热部署端口 devtools: add-properties: false livereload: port: 35729 #访问数据的SQL输出 jpa: show-sql: true open-in-view: true #配置日志输出级别 logging: level: web: INFOapplication.xml
#端口+路径 server: port: 8888 servlet: context-path: /provider #配置日志输出级别 logging: level: web: DEBUGapplication-dev.xml
・子「cloud-consumer」
#端口+路径 server: port: 8080 servlet : context-path : /consumer spring: #配置热部署端口 devtools: add-properties: false livereload: port: 35730 #访问数据的SQL输出 jpa: show-sql: true open-in-view: true #配置日志输出级别 logging: level: web: INFO #访问服务提供者 provider: path: http://localhost:8001/providerapplication.xml
■ソース
・「server」側に、Object「Dept.java」を作成する
package com.lnsylt.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; } }Dept.java
・「provider」側に、四つのファイル(DeptController/dao/service/serviceImpl)とmybatisの配置を作成する
package com.lnsylt.springcloud.controller; import com.lnsylt.springcloud.pojo.Dept; import com.lnsylt.springcloud.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/dept") public class DeptController { @Autowired private DeptService service; @PostMapping("/add") public boolean add(@RequestBody Dept dept){ return service.addDept(dept); } @GetMapping("/get/{id}") public Dept get(@PathVariable("id") Long id){ return service.queryById(id); } @GetMapping("/list") public List<Dept> queryAll(){ return service.queryAll(); } }controller
package com.lnsylt.springcloud.dao; import com.lnsylt.springcloud.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(); }dao
package com.lnsylt.springcloud.controller; import com.lnsylt.springcloud.pojo.Dept; import com.lnsylt.springcloud.service.DeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/dept") public class DeptController { @Autowired private DeptService service; @PostMapping("/add") public boolean add(@RequestBody Dept dept){ return service.addDept(dept); } @GetMapping("/get/{id}") public Dept get(@PathVariable("id") Long id){ return service.queryById(id); } @GetMapping("/list") public List<Dept> queryAll(){ return service.queryAll(); } }service
package com.lnsylt.springcloud.service; import com.lnsylt.springcloud.dao.DeptDao; import com.lnsylt.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 dao; @Override public boolean addDept(Dept dept) { return dao.addDept(dept); } @Override public Dept queryById(Long id) { return dao.queryById(id); } @Override public List<Dept> queryAll() { return dao.queryAll(); } }serviceImpl
<?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>mybatis-config.xml
<?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.lnsylt.springcloud.dao.DeptDao"> <insert id="addDept" parameterType="Dept"> insert into dept(dname,db_source) values (#{dname},DATABASE()) </insert> <select id="queryById" resultType="Dept" parameterType="Long"> select * from dept where deptno = #{deptno} </select> <select id="queryAll" resultType="Dept"> select * from dept </select> </mapper>DeptMapper.xml
・「consumer」側に、2つファイル(ConfigBean.java/DeptConsumerController.java)を作成する
package com.lnsylt.springcloud.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class ConfigBean { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }ConfigBean.java
package com.lnsylt.springcloud.controller; import com.lnsylt.springcloud.pojo.Dept; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; 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 @RequestMapping("/dept") public class DeptConsumerController { //理解:消费者,不应该有service层~ //RestTemplate .... 供我们直接调用就可以了! 注册到spring中 //三个参数:(url,实体:Map,Class<T> responseType) @Autowired private RestTemplate restTemplate; //Provider的请求路径 @Value("${provider.path}") private String REST_URL_PREFIX; @RequestMapping("/add") public boolean add(Dept dept){ return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class); } @RequestMapping("/get/{id}") public Dept get(@PathVariable("id") Long id){ return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class); } @RequestMapping("/list") public List<Dept> list(){ return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list", List.class); } }DeptConsumerController.java
■入口
・「provider」側に、SpringBoot用のMainクラスを作成
package com.lnsylt; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ProviderApp { public static void main(String[] args) { SpringApplication.run(ProviderApp.class,args); } }ProviderApp.java
・「consumer」側に、SpringBoot用のMainクラスを作成
package com.lnsylt; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ConsumerApp { public static void main(String[] args) { SpringApplication.run(ConsumerApp.class,args); } }ConsumerApp.java
■起動
S1:提供者(provider)のMainクラスを起動してください。
S2:消費者(consumer)のMainクラスを起動してください。
S3:URL「http://localhost:8888/provider/dept/list」で提供者へ訪問しましょう。
S4:URL「http://localhost:9999/consumer/dept/list」で提供者へ訪問しましょう。
S5:S3とS4の結果が一致すれば、終わり。
ここまで、基本的な環境を構築しました。
标签:Spring,構築,boot,springframework,環境,import,org,com,public From: https://www.cnblogs.com/lnsylt/p/17848559.html