Article Directory
1. What is SpringCloud?
A microservice ecosystem based on springboot. In springcloud, all services are modularized to help developers quickly build distributed systems, such as configuration management, service registration and discovery, fuse mechanisms, intelligent routing, global locks, and distribution Task, cluster construction, etc.
There are four main problems that SpringCloud solves:
- How does the client access the service
- How to connect between service and service
- Service registration and management
- Service crashed, how to deal with it
Then there are corresponding solutions around these four problems, and there are three main solutions.
- SpringCloud Netflix provides a one-stop solution
- Dubbo, focusing on service communication, zookeeper provides service registration and discovery
- springcloud Alibaba also provides a one-stop solution. It is slowly starting, and more companies will use it in the future.
It may sound unfamiliar, it doesn't matter, I will write a series of articles to make these clear.
So today's explanation is mainly how to build a simple microservice. There is a consumer of the service and a registrant of the service.
Second, use steps
1. Build the environment
Parent project dependency
<?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>com.lhh</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
</modules>
<!-- 父工程改为pom -->
<packaging>pom</packaging>
<properties>
<junit.version>4.12</junit.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- springcloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- springboot -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.16</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Create springcloud-api module
springcloud-api module import dependency
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
2.springcloud-provider-emp-8001
Structure
pom.xml
<!--引入实体类模块-->
<dependency>
<groupId>com.lhh</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
application.yaml configuration file
server:
port: 8001
mybatis:
type-aliases-package: com.lhh.entity
mapper-locations: classpath:mappers/*.xml
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
spring:
application:
name: springcloud-provider-emp
datasource:
driver-class-name: com.cj.mysql.jdbc.Driver
# url: jdbc:mysql://localhost:3306/db01
url: jdbc:mysql://localhost:3306/springcloud?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
# password: root
password: 123456
#数据源类别
type: com.alibaba.druid.pool.DruidDataSource
Empmapper.xml mapping file
<?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.lhh.springcloudprovideremp8001.dao.EmpMapper">
<select id="queryById" parameterType="Integer" resultType="Emp">
select *
from t_emp
where emp_id = #{empId}
</select>
<select id="selectAllEmps" resultType="Emp">
select *
from t_emp
</select>
</mapper>
Start class
package com.lhh.springcloudprovideremp8001;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.lhh.springcloudprovideremp8001.dao")
public class SpringcloudProviderEmp8001Application {
public static void main(String[] args) {
SpringApplication.run(SpringcloudProviderEmp8001Application.class, args);
}
}
2.springcloud-consumer-emp-8001
<!-- 引入实体类+web -->
<dependency>
<groupId>com.lhh</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
yaml configuration file
server:
port: 80
Consumer controller
package com.lhh.springcloudconsumer.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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
@RestController
@RequestMapping("/consumer")
public class EmpConsumerController {
//提供者的ip+端口
private static final String REST_URL_PREFIX = "http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/get/{empId}")
public com.lhh.entity.Emp get(@PathVariable("empId") Integer empId) {
return restTemplate.getForObject(REST_URL_PREFIX + "/provider/queryOne/" + empId, com.lhh.entity.Emp.class);
}
@GetMapping("/getAll")
public List<com.lhh.entity.Emp> get() {
return restTemplate.getForObject(REST_URL_PREFIX + "/provider/queryAll/", List.class);
}
}
Consumer startup class
package com.lhh.springcloudconsumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringcloudConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConsumerApplication.class, args);
}
}
to sum up
Congratulations, I know a little bit!
The more you know, the more you don’t know!
~Thank you for reading, your attention and comments, it is the greatest support for my study, come on, strangers, work hard together.