😏DEMO我放在git上。
https://github.com/HongXiangg/Cloud-Api
https://github.com/HongXiangg/Cloud-EurekaServer
https://github.com/HongXiangg/Cloud-Cloud-Provider
https://github.com/HongXiangg/Cloud-Cloud-Consumer
😏先了解Eureka
寄几百度🤨
😏工程架构
五个工程。一个父工程,四个子模块。
😏一.Cloud-Parent (父工程 parent)
1.创建一个maven simple 的工程
simple的工程默认会用jdk1.5,eureka最低支持1.8,所以可以修改工程的「.seetings/org.eclipse.jdt.core.prefs」文件 把里面的1.5 -->1.8 (没有问题请忽略)
2.添加pom依赖
<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>
<groupId>com.hongxiang</groupId>
<artifactId>Cloud-Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<spring-boot-dependencies.version>2.1.9.RELEASE</spring-boot-dependencies.version>
<spring-cloud-dependencies.version>Greenwich.SR2</spring-cloud-dependencies.version>
<spring-cloud-starter-eureka-server.version>1.3.6.RELEASE</spring-cloud-starter-eureka-server.version>
<spring-cloud-starter-eureka.version>1.3.6.RELEASE</spring-cloud-starter-eureka.version>
<druid.version>1.0.31</druid.version>
<mybatis-spring-boot-starter.version>1.3.2</mybatis-spring-boot-starter.version>
<springloaded.version>1.2.0.RELEASE</springloaded.version>
</properties>
<!--父工程只做依赖管理,不做实际依赖-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot-dependencies.version}</version>
<type>pom</type>
<!--maven项目只能单继承,因此这里使用 import-->
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud-dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>${spring-cloud-starter-eureka-server.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>${spring-cloud-starter-eureka.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot-starter.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>${springloaded.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- modules里面的东西是通过后期配置的 -->
<modules>
<module>Cloud-Api</module>
<module>Cloud-EurekaServer</module>
<module>Cloud-Provider</module>
<module>Cloud-Consumer</module>
</modules>
</project>
😏 二.Cloud-Api (子模块 api)(一般存放Entity)
1.创建 Maven Model工程
2.添加pom依赖
由于我的Api里面只想存Entity,所以pom不需要添加新的依赖了,有想用小辣椒的小伙伴可以添加Lombok,根据自己需求添加即可。
3.新建一个 entities 的包, 再新建一个 Student 类,之后测试使用
package com.hongxiang.entities;
public class Student {
private String studentId;
private String studentName;
private String gender;
private String classId;
private String teacherId;
//GET SET 自行生成
}
😏 三.Cloud-Eureka-Server (子模块 用来注册服务)
1.创建 Maven Model工程
跟上面创建Cloud-Api一样就不重复了,参照Cloud-Api的创建。
2.添加pom依赖
<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.hongxiang</groupId>
<artifactId>Cloud-Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Cloud-EurekaServer</artifactId>
<dependencies>
<!-- spring-cloud-starter-eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 热部署 -->
<!-- springloaded -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<!-- spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>
3.配置application.yml
默认不生成,在src/main/resources下新建即可。
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
register-with-eureka: false #服务端无需注册自己
fetch-registry: false #表示是否从eureka-server获取注册信息
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与eureka-server交互的地址查询服务和注册服务都需要依赖这个地址
4.创建启动类
package com.hongxiang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication_7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication_7001.class, args);
}
}
5.启动服务
访问http://localhost:7001/ 验证是否访问到 Eureka界面
😏 四.Cloud-provider (子模块 提供服务)
这里整合的是 Mybatis + Postgre
1.创建 Maven Model工程
跟上面创建Cloud-Api一样就不重复了,参照Cloud-Api的创建。
2.添加pom依赖
<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.hongxiang</groupId>
<artifactId>Cloud-Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Cloud-Provider</artifactId>
<dependencies>
<!-- springcloud-api 可以取到 entities 的信息 -->
<dependency>
<groupId>com.hongxiang</groupId>
<artifactId>Cloud-Api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- druid(数据库连接池) -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!-- mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- 热部署 -->
<!-- springloaded -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<!-- spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- 加载postgresql驱动 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
3.在数据库中添加 student 表以及表信息
CREATE TABLE "student" (
"student_id" VARCHAR(32) NOT NULL ,
"student_name" VARCHAR(32) NULL ,
"gender" VARCHAR(4) NULL ,
"class_id" VARCHAR(32) NULL ,
"teacher_id" VARCHAR(32) NULL ,
PRIMARY KEY (student_id")
);
INSERT INTO "student" VALUES(
'001', 'banana', '男', '1024', '2048'
);
4.配置application.yml
默认不生成,在src/main/resources下新建即可。
server:
port: 8001
mybatis:
config-location: classpath:mybatis/mybatis-config.xml #mybatis配置文件所在路径
mapper-locations:
- classpath:mybatis/mapper/**/*.xml #mapper映射文件
spring:
application:
name: springcloud-provider-dept #对外暴露的微服务的名字(微服务最重要的名字)
datasource:
type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
driverClassName: org.postgresql.Driver #数据库名称
url: jdbc:postgresql://localhost:5432/test?useSSL=false
username: ****** #用户名
password: ****** #密码
dbcp2: #druid 数据库连接池的配置
min-idle: 5 #最小闲置连接数
initial-size: 5 #初始化连接数
max-total: 5 #最大总连接数
max-wait-millis: 200 #最长连接等待时间
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka #将服务注册进eureka-server服务列表中
instance:
instance-id: Cloud-Provider-8001
prefer-ip-address: true
5.配置mybatis自动生成的xml
在 src/main/resource/mybatis 下新建 mybatis-config.xml 配置文件
<?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>
6.创建 StudentDao 接口
可以通过上面配置的xml自动生成,需要的话自行百度
package com.hongxiang.dao;
import org.apache.ibatis.annotations.Mapper;
import com.hongxiang.entities.Student;
@Mapper
public interface StudentDao {
Student findById(String studentId);
}
7.生成StudentMapper.xml 映射文件
可以通过上面配置的xml自动生成,需要的话自行百度,最好放在 src/main/resource/mybatis/mapper/student
<?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.hongxiang.dao.StudentDao">
<resultMap type="com.hongxiang.entities.Student" id="StudentMap">
<id property="studentId" column="STUDENT_ID" />
<result property="studentName" column="STUDENT_NAME" />
</resultMap>
<select id="findById" parameterType="java.lang.String"
resultMap="StudentMap">
select *
from student
where student_id = #{studentId}
</select>
</mapper>
8.创建 StudentService 接口
package com.hongxiang.service;
import com.hongxiang.entities.Student;
public interface StudentService {
Student findById(String studentId);
}
9.创建 StudentServiceImpl 实现类
package com.hongxiang.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hongxiang.dao.StudentDao;
import com.hongxiang.entities.Student;
import com.hongxiang.service.StudentService;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Override
public Student findById(String studentId) {
return studentDao.findById(studentId);
}
}
10.创建 StudentController 类
package com.hongxiang.controller;
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.RestController;
import com.hongxiang.entities.Student;
import com.hongxiang.service.StudentService;
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/server/student/get/{studentId}")
public Student findById(@PathVariable("studentId") String studentId) {
return studentService.findById(studentId);
}
}
11.创建启动类
package com.hongxiang;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.hongxiang.dao")
public class ProviderServerApplication_8001 {
public static void main(String[] args) {
SpringApplication.run(ProviderServerApplication_8001.class, args);
}
}
12.启动服务
访问 http://localhost:8001/server/student/get/001 验证是否返回数据库对应的json数据
😏 五.Cloud-Consumer (子模块 服务消费模块)
1.创建 Maven Model工程
跟上面创建Cloud-Api一样就不重复了,参照Cloud-Api的创建。
2.添加pom依赖
<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.hongxiang</groupId>
<artifactId>Cloud-Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>Cloud-Consumer</artifactId>
<dependencies>
<!-- spring-api -->
<dependency>
<groupId>com.hongxiang</groupId>
<artifactId>Cloud-Api</artifactId>
<version>${project.version}</version>
</dependency>
<!-- spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署 -->
<!-- springloaded -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<!-- spring-boot-devtools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
3.配置application.yml
server:
port: 80
4.创建 ConfigBean 配置类
package com.hongxiang.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();
}
}
5.创建 StudentController 类
package com.hongxiang.controller;
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.RestController;
import org.springframework.web.client.RestTemplate;
import com.hongxiang.entities.Student;
@RestController
public class StudentController {
private static final String REST_URL_PREFIX = "http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer/student/get/{studentId}")
public Student select(@PathVariable("studentId") String studentId) {
return restTemplate.getForObject(REST_URL_PREFIX + "/server/student/get/" + studentId, Student.class);
}
}
6.创建启动类
package com.hongxiang;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConsumerApplication_80 {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication_80.class, args);
}
}
12.启动服务
访问 http://localhost:80/consumer/student/get/001 验证是否返回数据库对应的json数据
😎 😎 😎 大功告成!