1、创建Maven父项目
创建父项目的目的是为了统一依赖管理,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 https://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.7.1</version>
</parent>
<groupId>com.example</groupId>
<artifactId>clouddemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>clouddemo</name>
<description>clouddemo</description>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
</dependency>
</dependencies>
<!-- 所有子模块项目 -->
<modules>
<module>provider</module>
<module>eureka</module>
<module>invoker</module>
<module>gateway</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、创建Eureka应用项目
Maven项目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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>clouddemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>eureka</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<name>eureka</name>
<description>eureka</description>
</project>
Eureka主应用类
package com.example.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApp.class, args);
}
}
应用配置文件application.yml
server:
port: 8761
spring:
application:
name: eureka-server
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8761/eureka/ #集群注册中心时,写多个eureka地址,逗号分割
register-with-eureka: false #不向eureka服务注册自己
fetch-registry: false #不像Eureka检索服务
3、创建Gateway应用项目
<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>
<parent>
<groupId>com.example</groupId>
<artifactId>clouddemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>gateway</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<name>gateway</name>
<description>gateway</description>
</project>
创建Gateway主应用类
package com.example.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApp {
public static void main(String[] args) {
SpringApplication.run(GatewayApp.class, args);
}
}
application.yml文件配置
server:
port: 9000
eureka:
client:
service-url:
register-with-eureka: true
fetch-registry: true
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: gateway
cloud:
gateway:
routes:
- id: invoker-service
uri: lb://invoker
predicates:
- Path=/invoker-service/**
filters:
- StripPrefix=1
discovery:
locator:
enabled: true #启用Eureka默认路由,以大小应用名为服务rul前缀
完成Eureka和Gateway应用配置创建后,可以启动相关相关服务,然后用浏览器访问http://localhost:8761,检验Gateway应用是否成功与Eureka通讯。如果在应用内部中能看到名称为GATEWAY的应用,说明相关应用分配在成功。
4、创建provider应用
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>clouddemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>provider</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<name>provider</name>
<description>provider</description>
</project>
主应用类:
package com.example.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableEurekaClient
@SpringBootApplication
public class ProviderApp {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(ProviderApp.class);
application.run(args);
}
}
服务提供方controler类
package com.example.provider.hello.ctrler;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/helloDemo")
public class HelloDemoCtrler {
@PostMapping("/sayHello.do")
public String sayHello() {
return "Hello world!中文是否正常?";
}
}
application.yml
server:
port: 8081
spring:
application:
name: provider
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
可通过postman或者VSCode的Thunder Client插件向http://localhost:8081/helloDemo/sayHello.do发起POST请求,如果能获得 Hello world!中文是否正常? 的响应,说明提供方服务正常运行。如果向http://localhost:9000/PROVIDER/helloDemo/sayHello.do发起POST请求能获得相同的响应,说明Gateway的Eureka默认路由生效了。
5、创建消费方应用
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.example</groupId>
<artifactId>clouddemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>invoker</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<name>invoker</name>
<description>invoker</description>
</project>
应用主类:
package com.example.invoker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class InvokerApp {
public static void main(String[] args) {
SpringApplication.run(InvokerApp.class, args);
}
}
调用provider客户端接口
package com.example.invoker.helloinvoker.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
@Component
@FeignClient ("provider")
public interface IHelloProvider {
@PostMapping("/helloDemo/sayHello.do")
public String callSayHello();
}
invoker为前端提供服务的controler类
package com.example.invoker.helloinvoker.ctrler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.invoker.helloinvoker.client.IHelloProvider;
@RestController
@RequestMapping("/invokerClient")
public class InvokerClient {
@Autowired
private IHelloProvider helloProvider;
@PostMapping("/callProviderService.do")
public String callProviderService() {
return "callProviderService = " + helloProvider.callSayHello();
}
}
application.yml
server:
port: 8082
spring:
application:
name: invoker
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
可通过postman或者VSCode的Thunder Client插件向http://localhost:8082/invokerClient/callProviderService.do发起POST请求,如果能获得 callProviderService = Hello world!中文是否正常? 的响应,说明消费方服务以及调用提供方服务均正常运行。如果向http://localhost:9000/invoker-service/invokerClient/callProviderService.do和http://localhost:9000/INVOKER/invokerClient/callProviderService.do发起POST请求能获得相同的响应,说明Gateway的配置路由和Eureka默认路由均生效了。
6、spring boot应用集成druid和mybatis
pom.xml,重点加入druid和mybatis相关依赖:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.peng</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml,重点是配置druid和mybatis的map:
server:
port: 8081
spring:
datasource:
druid:
url: jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull
#url: jdbc:mysql://localhost:3306/testdb?serverTimezone=Asia/Shanghai
username: user
password: userpassword
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations:
- classpath:mappers/**/*.xml
sqlmap.xml文件,注意该文件因存放在resources目录下:
<?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.example.demo.hello.dao.IHelloDao">
<resultMap id="queryMap" type="com.example.demo.hello.vos.FirstTable">
<id column="id_col" jdbcType="VARCHAR" property="idCol"/>
<result column="name_col" jdbcType="VARCHAR" property="nameCol"/>
<result column="age_col" jdbcType="INTEGER" property="ageCol"/>
</resultMap>
<select id="queryData" resultMap="queryMap">
SELECT id_col, name_col, age_col
FROM first_table
</select>
<insert id="insertData">
INSERT INTO first_table (id_col, name_col, age_col)
VALUES('0000000000000002', '新增数据', 20);
</insert>
</mapper>
IHelloDao.java,重点是@Mapper注解:
package com.example.demo.hello.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import com.example.demo.hello.vos.FirstTable;
@Mapper
@Repository
public interface IHelloDao {
public List<FirstTable> queryData();
public int insertData();
}
调用dao的ServiceImpl类:
package com.example.demo.hello.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.hello.dao.IHelloDao;
import com.example.demo.hello.service.IHelloService;
import com.example.demo.hello.vos.FirstTable;
@Service
public class HelloServiceImpl implements IHelloService {
@Autowired
private IHelloDao helloDao;
@Override
public List<FirstTable> queryData() {
List<FirstTable> result = helloDao.queryData();
for (FirstTable firstTable : result) {
System.out.println(firstTable.getNameCol());
}
System.out.println("什么情况啊?");
return result;
}
@Override
public int insertData() {
return helloDao.insertData() + helloDao.insertData();
}
}