文章目录
一、SpringCloud是什么?
基于springboot的一种微服务生态,在springcloud当中,所有的服务都是模块化的,帮助开发者快速构建分布式系统,例如配置管理,服务注册与发现,熔断机制,智能路由,全局锁,分布式任务,集群构建等等。
SpringCloud解决的问题主要有四点:
- 客户端如何访问服务
- 服务于服务之间如何联系
- 服务的注册与管理
- 服务崩了,如何去处理
那么围绕这4个问题,就有了对应的解决方案,解决方案主要有三个。
- SpringCloud Netflix提供一站式的解决方案
- dubbo,专注于服务通信这一块,zookeeper提供服务注册与发现
- springcloud Alibaba,也提供一站式的解决方案,慢慢在起步,以后用的企业应该会慢慢多起来。
听起来可能有点陌生,没关系,我会写系列文章把这些讲的明明白白。
那么今天讲解的主要是怎么构建一个简单的微服务,有一个服务的消费者,也有一个服务的注册者。
二、使用步骤
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">
<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>
创建springcloud-api模块
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
2.springcloud-provider-emp-8001
结构
<!--引入实体类模块-->
<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配置文件
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映射文件
<?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>
启动类
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配置文件
server:
port: 80
消费者控制器
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);
}
}
消费者启动类
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);
}
}
总结
看完恭喜你,又知道了一点点!
你知道的越多,不知道的越多!
~感谢您的阅读,你的关注和评论,是对我学习的最大的支持,加油,陌生人,一起努力。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。