一、父工程依赖管理
<?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>
<groupId>org.mumu</groupId>
<artifactId>eureka</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<modules>
<module>common</module>
<module>consumer</module>
<module>springcloud-service-provider</module>
</modules>
<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>
<mysql.version>5.1.47</mysql.version>
<lombok.version>1.16.18</lombok.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
<scope>runtime</scope>
</dependency>
<!-- <!- druid->-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
二、搭建公共模块common
放一些pojo类
1. 依赖引入
<?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>eureka</artifactId>
<groupId>org.mumu</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-service-common</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
</dependency>
</dependencies>
</project>
2.实体类
@Data // 省略写get set方法
@NoArgsConstructor //提供无参数的构造函数
@AllArgsConstructor //提供带所有参数的构造函数
public class Payment implements Serializable {
private long id;
private String serial;
}
三、搭建服务提供方provider
1.依赖引入
<?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>eureka</artifactId>
<groupId>org.mumu</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-service-provider</artifactId>
<version>1.0</version>
<groupId>org.mumu</groupId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.mumu</groupId>
<artifactId>springcloud-service-common</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
2.配置类
server:
port: 8001 #配置服务端口号
spring:
application:
name: service-provider # 配置服务提供方的名称
datasource: # 配置连接数据库的基本信息
driver-class-name: com.mysql.jdbc.Driver # 驱动
url: jdbc:mysql://localhost:3306/cloud2023 # 连接数据库的url
username: root # 连接数据库的用户名
password: 123456 # 连接数据库的密码
mybatis:
config-location: classpath:/mybatis/sqlMapConfig.xml # 引入mybatis的核心配置文件
mapper-locations: classpath:/mybatis/mapper/*.xml # 引入mybatis的映射文件
eureka:
client:
register-with-eureka: true # 允许将当前服务注册到eureka注册中心
fetch-registry: true # 允许当前微服务拉取注册中心中的服务信息
service-url:
defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/ # eureka注册中心的地址
3.编写启动类
@SpringBootApplication
@MapperScan(basePackages = "com.xq.dao")
@EnableDiscoveryClient //开启服务发现的功能
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class,args);
}
}
4. 编写业务逻辑
(1)整合mybatis
dao层
public interface PaymentDao {
//根据id查询payment信息
public Payment findById(long id);
//新增payment信息
public void add(Payment payment);
}
创建dao接口的映射文件还有mybatis的核心配置文件
<?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="org.mumu.dao.PaymentDao">
<!--根据id查询payment信息-->
<select id="findById" parameterType="long" resultType="payment">
SELECT * FROM `payment` WHERE id = #{id}
</select>
<!--新增payment信息-->
<insert id="add" parameterType="payment">
INSERT INTO `payment`(`id`,`serial`) VALUES(#{id},#{serial})
</insert>
</mapper>
配置 MyBatis 的类型别名,简化 MyBatis 映射文件中的配置
<?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>
<!--取别名-->
<typeAliases>
<package name="org.mumu.pojo"></package>
</typeAliases>
</configuration>
(2)Service
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
PaymentDao paymentDao;
@Override
public Payment findById(long id) {
return paymentDao.findById(id);
}
@Override
public void add(Payment payment) {
paymentDao.add(payment);
}
@Override
public void save(Payment payment) {
paymentDao.add(payment);
}
}
5.定义控制器
@RestController
@RequestMapping("provider")
public class PaymentController {
@Resource
PaymentService paymentService;
@Value("${server.port}")
String port;
@RequestMapping("findById")
public Result<Payment> findById(@RequestParam("id") long id){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Payment payment = paymentService.findById(id);
return new Result<>(200,"数据查询成功,当前服务端口号是:" + this.port,payment);
}
}
四、搭建服务消费方consumer
1.依赖引入
<dependencies>
<dependency>
<groupId>org.mumu</groupId>
<artifactId>springcloud-service-common</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</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>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
2.配置类
server:
port: 80
spring:
application:
name: service-consumer
eureka:
client:
register-with-eureka: true # 允许将当前服务注册到eureka注册中心
fetch-registry: true # 允许当前微服务拉取注册中心中的服务信息
service-url:
defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/ # eureka注册中心的地址
3.启动类
@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class,args);
}
}
4.注入RestTemplate组件到ioc容器
使用
RestTemplate
这个
Java
客户端组件来实现服务的远程调用。所以我们需要将
RestTemplate
使用
Java
配置类进行注入:
@Configuration
public class MyConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
5.定义控制器
@RestController
@RequestMapping("consumer")
@Slf4j
public class PaymentController {
@Resource
RestTemplate restTemplate;
@RequestMapping("findById/{id}")
public Result<Payment> findById(@PathVariable("id") long id){
String url = "http://localhost:8001/provider/findById?id=" + id; //维护服务提供方的ip+端口
Result result = restTemplate.getForObject(url, Result.class);
return result;
}
}
五、搭建服务注册中心
1.引入依赖server
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version> <!-- 使用与Tomcat 9.0.29 兼容的版本 -->
</dependency>
<!--eureka服务端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
</dependencies>
2.配置类
这里不需要要进行服务注册,因为这个模块的server模块
负责对其他Client进行服务注册
server:
port: 7001
# 配置eureka服务端
eureka:
client:
register-with-eureka: false # 禁止自己注册自己
fetch-registry: false # 禁止抓取注册中心中的服务信息
service-url:
defaultZone: http://localhost:7001/eureka/ # eureka服务端的地址
3.启动类
@SpringBootApplication
@EnableEurekaServer // 标识当前服务是Eurkea服务端
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
六、启动
访问地址:http://localhost:7001