SpringCloud基础服务搭建
前提准备
1.Idea工具
2.Maven设置
3.JDK环境(我这里使用java1.8
)
先创建一个Maven工程(删除src包),作为父工程并在pom.xml中配置如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
总项目模块如图:
1.注册中心
创建子模块eureka-server
进行如下配置:
1.1 pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
1.2 resource
下新建application.yml
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: false
fetch-registry: false
1.3 启动类
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class,args);
}
}
2.配置中心(本地)
创建子模块config-server
:
2.1 pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
</dependencies>
2.2 resource
目录下添加application.yml
server:
port: 8762
spring:
application:
name: configserver
profiles:
active: native
cloud:
config:
server:
native:
search-locations: classpath:/shared
由于此处添加了配置文件目录search-locations: classpath:/shared
,即其他微服务模块的yml配置放在resource目录下的shared目录中,因此需要【在resource目录下新建shared目录】。
2.3 share
目录添加client2-dev.yml
文件
server:
port: 8020
spring:
application:
name: client2
datasource:
name: test
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
instance:
prefer-ip-address: true
mybatis:
mapper-locations: classpath:/mapper/*.xml
type-aliases-package: com.zstu.entity
2.4 启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class,args);
}
}
3.服务提供者
新建模块eurekaclient-demo02
,这里提供了数据库连接以及利用mybatis-generator
自动生成代码,整体框架如下:
3.1 pom.xml 配置
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
<version>2.0.2.RELEASE</version>
</dependency>
<!--Mybatis Generator-->
<!--利用mybatis-generator自动生成代码-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-spring-boot-starter -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-generator</artifactId>
<version>1.1.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/tk.mybatis/mapper-generator -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-generator</artifactId>
<version>1.1.5</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>mybatis generator</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<!--为避免反复生成-->
<configuration>
<!--允许移动生成的文件-->
<verbose>true</verbose>
<!--允许自动覆盖-->
<overwrite>true</overwrite>
<configurationFile>
src/main/resources/generator/mybatis-generator.xml
</configurationFile>
</configuration>
</plugin>
</plugins>
</build>
3.2 bootstrap.yml
【规定必须这样命名yml文件】
spring:
application:
name: client2
profiles:
active: dev
cloud:
config:
uri: http://localhost:8762
fail-fast: true #是否启动快速失败功能,功能开启则优先判断config server是否正常
其中,name
与active
的属性会进行拼接为【client2-dev】,然后去http://localhost:8762
配置中心config-server
模块的resource/shared目录下查找一个名为client2-dev.yml
的文件进行读取配置。
3.3 mybatis-generator配置
resource目录下新建generator目录,再新建mybatis-generator.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--导入属性配置-->
<!-- context 是逆向工程的主要配置信息 -->
<!-- id:起个名字 -->
<!-- targetRuntime:设置生成的文件适用于那个 mybatis 版本 -->
<context id="DB2Tables" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<!-- 生成的Java文件的编码 -->
<property name="javaFileEncoding" value="UTF-8"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 格式化java代码 -->
<property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<!-- 格式化XML代码 -->
<property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<!-- 整合lombock,参考方式https://github.com/GuoGuiRong/mybatis-generator-lombok-plugin 暂时不整合-->
<!-- <plugin type="org.mybatis.generator.plugins.LombokPlugin" >-->
<!-- <property name="hasLombok" value="true"/>-->
<!-- </plugin>-->
<!-- 配置 tk.mybatis 插件 这边value指向mybatis通用接口 -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
</plugin>
<!--optional,旨在创建class时,对注释进行控制-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false"/>
<!-- 是否带上注释-->
<property name="addRemarkComments" value="true"/>
</commentGenerator>
<!--jdbc的数据库连接-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?useInformationSchema=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC"
userId="root"
password="123456">
</jdbcConnection>
<!-- targetPackage:生成的实体类所在的包 -->
<!-- targetProject:生成的实体类所在的硬盘位置 -->
<javaModelGenerator targetPackage="com.zstu.entity"
targetProject="src/main/java">
<!-- 是否允许子包 -->
<property name="enableSubPackages" value="false"/>
<!-- 是否对modal添加构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 是否清理从数据库中查询出的字符串左右两边的空白字符 -->
<property name="trimStrings" value="true"/>
<!-- 建立modal对象是否不可改变 即生成的modal对象不会有setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- targetPackage 和 targetProject:生成的 mapper 文件的包和位置 -->
<sqlMapGenerator targetPackage="mappers"
targetProject="src/main/resources/mapper">
<!-- 针对数据库的一个配置,是否把 schema 作为字包名 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!--生成dao类存放位置-->
<!-- targetPackage 和 targetProject:生成的 interface 文件的包和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.zstu.dao" targetProject="src/main/java">
<!-- 针对 oracle 数据库的一个配置,是否把 schema 作为字包名 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!--生成表及类名-->
<table tableName="student">
<property name="modelOnly" value="false"/>
</table>
</context>
</generatorConfiguration>
需要生成实体类的表填写在<table tableName="user2">
的tableName
属性后
- 4 dao层配置
public interface StudentMapper extends Mapper<Student> {
@Select("select * from student")
List<Student> findAllStudent();
@Delete("delete from student where id={#id}")
String removeStudent(Integer id);
}
- 5 service层配置
public interface ClientDemo02Service {
List<Student> listAllStudent();
String rmStudentById(Integer id);
}
@Service
public class ClientDemo02ServiceImpl implements ClientDemo02Service {
@Resource
private StudentMapper studentMapper;
@Override
public List<Student> listAllStudent() {
List<Student> allStudent = studentMapper.findAllStudent();
return allStudent;
}
@Override
public String rmStudentById(Integer id) {
String s = studentMapper.removeStudent(id);
return "删除Student成功";
}
}
3.6 controller层
@RestController
public class EurekaClient2Controller {
@Autowired
private ClientDemo02Service clientDemo02Service;
@GetMapping("/getAllStudent")
public List<Student> getAllStudent(){
List<Student> students = clientDemo02Service.listAllStudent();
return students;
}
@DeleteMapping("/deleteStudentById")
public String deleteStudentById(@PathVariable("id") Integer id){
String s = clientDemo02Service.rmStudentById(id);
return s;
}
}
4.Zuul网关
4.1 pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.4.RELEASE</version>
</dependency>
</dependencies>
4.2application.yml
配置
server:
port: 8030
spring:
application:
name: gateway
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
zuul:
routes:
api-b:
path: /c2/**
serviceId: client2
4.3 启动类
@EnableZuulProxy
@EnableAutoConfiguration
public class ZuulServerApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulServerApplication.class,args);
}
}
请求结果: