微服务架构是一种将应用程序拆分成小型、自治的服务的软件设计方法。每个服务都是相对独立的,拥有自己的数据库,部署在自己的容器中,可以使用不同的编程语言和技术栈。通过使用微服务,可以实现更高的可伸缩性、可靠性和灵活性,同时使开发人员能够更快地开发和交付新功能。
如图是spring官网的spring cloud微服务架构图:
下面是一个更加详细的具体项目的架构图
后续将具体学习各个组件,输出代码,开发属于自己的微服务项目脚手架。
使用spring cloud的时候,一定要注意spring cloud和spring boot的版本是有对应关系的,如果版本不一致会有一些bug。
可以通过官方给的网址来获取当前具体的版本对应:https://start.spring.io/actuator/info
后续版本搭配:
spring cloud H版本
spring boot 2.2.5RELEASE
JDK 8
pom引入基本依赖:
4.0.0 com.lufei lf-cloud 1.0-SNAPSHOT 8 8 UTF-8 1.8 1.8 4.12 1.2.17 1.16.18 5.1.47 1.2.16 1.3.0 org.springframework.boot spring-boot-dependencies 2.2.5.RELEASE pom import org.springframework.cloud spring-cloud-dependencies Hoxton.SR1 pom import mysql mysql-connector-java ${mysql.version} com.alibaba druid ${druid.version} org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis.spring.boot.version} junit junit ${junit.version} log4j log4j ${log4j.version} org.projectlombok lombok ${lombok.version} true lf-cloud org.springframework.boot spring-boot-maven-plugin 2.2.5.RELEASE true true
新建一个auth用户授权服务,一个用户信息服务。
简单模拟服务的远程调用
pom.xml
lf-cloud com.lufei 1.0-SNAPSHOT 4.0.0 lf-user org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.mybatis.spring.boot mybatis-spring-boot-starter com.alibaba druid-spring-boot-starter 1.1.10 mysql mysql-connector-java org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
application.yml
server:port: 9001spring:application:name: lf-userdatasource:type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=utf-8&useSSL=falseusername: rootpassword: rootmybatis:mapperLocations: classpath:mapper/*.xmltype-aliases-package: com.lufei.springcloud.domain
启动类UserApplication
@SpringBootApplication
public class UserApplication {public static void main(String[] args) {SpringApplication.run(UserApplication.class,args);}}
UserController类,暂时不查数据库,直接返回
@RestController
@RequestMapping("/user")
public class UserController {/*** 获取当前用户信息*/@GetMapping("/info/{username}")public String info(@PathVariable("username") String username){return username + " login success";}}
启动测试可以访问(数据库未配置好可能会有些报错,先不管)
pom.xml
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
application.yml
server:port: 9002spring:application:name: lf-auth
启动类AuthApplication
@SpringBootApplication
public class AuthApplication {public static void main(String[] args) {SpringApplication.run(AuthApplication.class,args);}}
运行测试确保项目不报错
上述两个简单的子项目项目已经搭建好。
接下来,我们简单模拟下,auth认证服务登录时,远程调用user服务,获取信息成功登录。
我们先用RestTemplate 来实现这一操作
RestTemplate是Spring框架提供的一种HTTP客户端工具,用于与RESTful Web服务进行交互。它简化了开发人员使用Java代码发送HTTP请求和处理HTTP响应的过程。
使用RestTemplate,可以轻松地发送GET、POST、PUT、DELETE等HTTP请求,并处理响应。它还支持各种HTTP身份验证、请求头、Cookie等常用的HTTP特性。
RestTemplate的另一个优点是它可以与Spring的各种数据绑定和转换技术(例如Jackson JSON处理器)结合使用,这样您就可以方便地将HTTP响应转换为Java对象。
@Configuration
public class ApplicationContextConfig {@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}}
Auth服务controller接口
@RestController
@RequestMapping("/auth")
public class AuthController {@AutowiredRestTemplate restTemplate;public static final String USER_URL = "http://localhost:9001";@PostMapping("login")public String login(@RequestBody String name){return restTemplate.getForObject(USER_URL+"/user/info/"+name,String.class);}}
如图,9002的授权服务,最终成功访问9001的用户服务
至此我们已经可以通过RestTemplate 远程调用服务,但是也有一些明显问题:
这些问题就需要通过注册中心动态的对服务注册和服务发现。
下一期继续微服务的注册与发现。