欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

部门服务消费者|学习笔记

最编程 2024-08-13 07:11:00
...

开发者学堂课程【微服务框架 Spring Cloud 快速入门:部门服务消费者】学习笔记与课程紧密联系,让用户快速学习知识

课程地址https://developer.aliyun.com/learning/course/614/detail/9342


部门服务消费者

 

内容介绍

一、新建 microservicecloud-consumer-dept-80

二、POM

三、YML

四、配置类

五、消费者类

六、DeptConsumer80_App主启动类

七、测试

 

一、新建 microservicecloud-consumer-dept-80

右键新建 Other 再点击Maven Module下一步,勾选 Create,Module Name 为microservicecloud-consumer-dept -80,检查 Group Id 和包名是否为 jar,最后点击 Finish 创建完成。

 

二、POM

pom.xml<parent>为父类,<artifactId>是子类,<description>说明是部门微服务消费者,这里提到部门所以就要再建一个 Dept.java,那此时就是消费者带一份 Dept.java,提供者带一份 Dept.java,对于共有的就可以把 Dept.java 提出为公共模块,然后jav注入进去。接下面消费者工程同样引入自定义 api,然后 web 热部署启动,因为是面对消费者偏向 web,所以一定要导入程序包org.springframework.boot

pom.xml 文件内容为:

<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>

<parent>

<groupId>com.lss.springcloud</groupId>

<artifactId>microservicecloud</artifactId>

<version>0.0.1-SNAPSHOT</version>

</parent>

<artifactId>microservicecloud-consumer-dept-80</artifactId>

<description>部门微服务消费者</description>

<dependencies>

<dependency><!-- 自己定义的api -->

<groupId>com.lss.springcloud</groupId>

<artifactId>microservicecloud-api</artifactId>

<version>${project.version}</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- 修改后立即生效,热部署 -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>springloaded</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

</dependency>  

</dependencies>

</project>

 

三、YML

新建文件 application.yml

如果此时 Spring Cloud 的工具都在 application 整合了的话,输入代码时会有自动提示图片。

application.yml 文件内容为

server:

port: 80

 

四、配置类

com.atguigu.springcloud.cfgbeans ConfigBean 的编写(类似 spring 里面的applicationContext.xml 写入的注入 Bean)

在80下新建包,包名为 com.atguigu.springcloud.cfgbeans,注解版的配置类,就是建立两个类,一个类是 configuration,另一个类就是 Bean,在上面的包中,新建一个注解版的配置类 ConfigBean

这个类单写没什么意义,但是前面加上注释 @Configuration,加上后因为boot中逐渐优化了 spring 框架,spring 用到配置文件是 applicationContext.xml,但是现在没有这个文件了,用的是注解版的 @Configuration 配置,言下之意ConfigBean 等于 applicationContext.xml,而 applicationContext.xml 中会写入<bean id="" class="">。回到boot中只要类名 ConfigBean 上有注解@Configuration 就是注释版的 applicationContext.xml,以前在配置文件中写<bean id-"userServcie" class="com. atguigu. tmall.UserServiceImpl">,而现在其中的bean就是注解@Bean,id userServcie 当作 UserServcie,new UserServcieImpl 就是 com. atguigu. tmall.UserServiceImpl

接下来回到 ConfigBean 要提出一个新对象,叫 RestTemplate,就是要在类里面注入一个 Bean,叫 RestTemplateRestTemplate 具体是什么暂时不介绍。

ConfigBean.java 的内容是:

package com.lss.springcloud.cfgbeans;

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();

}

}

/*

* @Bean

* public UserService getUserService(){

* return new UserServiceImpl();

* }

*/

//<bean id="userService"  class="com.lss.tmall.UserServiceImpl">

//<bean id="userServcie" class="com. atguigu. tmall .UserServiceImpl">

 

五、消费者类

下面依旧要建一个包,因为以前 applicationContext.xml 是放在 resources 上面,但是到 boot 开发后,不再推荐 applicationContext.xml,而是形成注解,集成到ConfigBean.java 下成为一个实体类。所以新建com.atguigu.springcloud.controller 包,包下新建 DeptController Consumer类,DeptController Consumer 类就是要暴露出来被访问的。

网络异常,图片无法展示
|

REST 微服务消费者在服务消费者里面的结构,没有服务层,即没有service 接口层和 service 实现层只有 controller 层。所以要进行模拟controller 层通过 restTemplate 来进行 REST 的调用和发出。就像 JDBC 中 spring 框架里面的 JDBCTemplate,和 redis 中 spring 合理后的 redisTemplate。这里要调用 Rest 微服务,spring 提供了便利,就是要调用 Rest,JDBC,redis 接口都会给出一个模板,所以要发 Rest 请求可以用 RestTemplate 来完成接口调用。

1、RestTemplate

什么是RestTemplate?

RestTemplate 提供了多种便捷访问远程 Http 服务的方法, 是 spring 高度抽象后针对简单便捷的访问 restful 服务模板类,是Spring提供的用于访问 Rest 服务的客户端模板工具集。注意:是客户端!

官网及使用:

https://docs.spring.io/spring-framework/docs/4.3.7.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html

使用:

使用 restTemplate 访问 restful 接口非常的简单粗暴无脑。

(url, requestMap, ResponseBean.class) 这三个参数分别代表

REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。

2、DeptController_Consumer.java内容分析

因为是消费端,所以定义公有类布尔型 add 方法,传入参数 Dept,dept,而 Dept原来并不在工程里面,是因为 Maven 引入了 api 所以可以直接调用,省了很多代码的编写,一处定义处处使用。然后 return 返回 restTemplate,可能以前习惯于service.add(),但现在是消费端,没有 service 层,所以不需要写业务逻辑层,这里的业务逻辑层应该由上一个工程8001提供,所以应该根据 restTemplate 风格写add插入操作,使用 postForObject 方法传递对象。用处理地址请求的注解@RequestMapping 访问,后面网址/consumer/dept/add 访问的是 consumer,url 找8001,相当于80没有业务逻辑,业务逻辑要找8001,而8001的提高就是/dept/addRequestMap 请求参数,请求的是 dept,返回的是布尔类型Booleanclass。然后同理分别是 get  查操作,REST 请求地址是/dept/get/,返回Dept.class,最终达成按id查找部门,第三个list同理,泛型的警告可以不处理。现在就是 consumer 的 url 访问地址,对外暴露的是客户端,实际执行的是8001

3、DeptController_Consumer.java代码

package com.lss.springcloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

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 com.lss.springcloud.entities.Dept;

@RestController

public class DeptController_Consumer {

private static final String REST_URL_PREFIX= "http:// localhost:8001";

@Autowired

private RestTemplate restTemplate;

/*

使用:

使用 restTemplate 访问 restful 接口非常的简单粗暴无脑。

(url,requestMap, ResponseBean.class)这三个参数分别代表REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。

*/

@RequestMapping(value="/consumer/dept/add")

public boolean add(Dept dept) {

return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);

}

@RequestMapping(value="/consumer/dept/get/{id}")

public Dept get(@PathVariable("id")  Long id) {

return restTemplate.getForObject(REST_URL_PREFIX+"/dept/get/"+id,Dept.class);

}

@SuppressWarnings("unchecked")

@RequestMapping(value="/consumer/dept/list")

public List<Dept> list() {

return restTemplate.getForObject(REST_URL_PREFIX+"/dept/list",List.class);

}

}

 

六、DeptConsumer80_App主启动类

首先包下新建 DeptConsumer80_App

DeptConsumer80_App.java 内容是:

package com.jack.springcloud;

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class DeptConsumer80_App {

public static void main(String[] args) {

SpringApplication.run(DeptConsumer80_App.class,args);

}

}

此时有一个微服务提供者8001,还有一个微服务消费者80,将他们启动起来。

 

七、测试

接下来进行两个查询一个插入的测试:

1http://localhost/consumer/dept/get/2

网页查询如下:

image.png

此时网页链接不再是8001,也不是80端口,而是带有consumer,证明是被外部来访问的。

2http://localhost/consumer/dept/list

查看全部,查询如下:

image.png

3http://localhost/consumer/dept/add?dname=AI

当AI为bigData2018时,查询为 true。

image.png

因为最终http响应转化成的对象类型是布尔型,言下之意是数据库里插入成功就返回true,也可以连接数据库打开表会发现增加了数据 big Data2018,如http://localhost/consumer/dept/list查看。

总结:微服务化的核心就是将传统的一站式应用,根据业务拆分成一个一个的服务,言下之意就是这套系统有了一个部门微服务的提供者和部门微服务的消费者,彻底地去耦合,每个微服务提供单个业务功能的服务,一个服务做件事 ,从技术角度看就是一种小而独立的处理过程,类似进程概念,这里就要提到微服务和微服务架构,微服务就像这里启动的微服务,而后续随着功能增加基于 cloud 技术去搭建就是微服务架构,类似进程可以从任务管理器中体现,每一个微服务耗费内存都很大,所以一个微服务可以做一个进程,能够自行单独启动或销毁,拥有自己独立的数据库。