首先,我们需要模拟一个服务调用的场景。方便后面学习微服务架构!
我们新建一个项目,对外提供查询用户的服务。
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.4.RELEASEversion>
<relativePath/>
parent>
<groupId>com.bruceliu.springcloud0310groupId>
<artifactId>springcloud-producer-demoartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.0.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.28version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
service:
build>
project>
添加一个对外查询的接口:
/**
* @author bruceliu
* @create 2019-05-01 16:04
* @description
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
@ResponseBody
@GetMapping("/test")
public User hello() {
User user = this.userService.queryById(2);
return user;
}
@GetMapping("/all")
@ResponseBody
public List<User> all(ModelMap model) {
// 查询用户
List<User> users = this.userService.queryUsers();
// 放入模型
model.addAttribute("users", users);
// 返回模板名称(就是classpath:/templates/目录下的html文件名)
return users;
}
public interface UserService {
public User queryById(Integer id);
public void deleteById(Integer id);
public List<User> queryUsers();
}
/**
* @author bruceliu
* @create 2019-05-01 14:52
* @description
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User queryById(Integer id) {
return userMapper.queryById(id);
}
@Override
public void deleteById(Integer id) {
userMapper.deleteById(id);
}
@Override
public List<User> queryUsers() {
return userMapper.queryUsers();
}
}
mapper:
/**
* @author bruceliu
* @create 2019-05-01 14:50
* @description
*/
@Mapper
public interface UserMapper {
public User queryById(Integer id);
public void deleteById(Integer id);
public List<User> queryUsers();
}
实体类
package com.bruceliu.bean;
import lombok.Data;
import java.io.Serializable;
/**
* @author bruceliu
* @create 2019-05-01 11:16
* @description
*/
@Data
public class User implements Serializable{
private int id;
private String username;
private String password;
private int age;
private int sex;
private String birthday;
private String created;
private String updated;
private String note;
}
属性文件,properties:
server.port=8080
# mybatis 别名扫描
mybatis.type-aliases-package=com.bruceliu.bean
# mapper.xml文件位置,如果没有映射文件,请注释掉
mybatis.mapper-locations=classpath:mappers/*.xml
# 连接四大参数
spring.datasource.url=jdbc:mysql://127.0.0.1/ssmdb?
useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
# 可省略,SpringBoot自动推断
spring.datasource.driverClassName=com.mysql.jdbc.Driver
#初始化连接数
spring.datasource.druid.initial-size=1
#最小空闲连接
spring.datasource.druid.min-idle=1
#最大活动连接
spring.datasource.druid.max-active=20
#获取连接时测试是否可用
spring.datasource.druid.test-on-borrow=true
#监控页面启动
spring.datasource.druid.stat-view-servlet.allow=true
#开发阶段关闭thymeleaf的模板缓存
spring.thymeleaf.cache=false
启动类
@SpringBootApplication
public class ProducerAPP {
public static void main(String[] args) {
//启动SpringBoot
//SpringApplication.run(APP.class,args);
SpringApplication app = new
SpringApplication(ProducerAPP.class);
app.setBannerMode(Banner.Mode.CONSOLE);//关闭Banner
app.run(args);
}
}
启动项目,访问接口: http://127.0.0.1/all
与上面类似,这里不再赘述,需要注意的是,我们是调用服务,因此不需要mybatis
相关依赖了。 pom:
<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.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.4.RELEASEversion>
<relativePath/>
parent>
<groupId>com.bruceliu.consumergroupId>
<artifactId>springcloud-demo-consumerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springcloud-demo-consumername>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.squareup.okhttp3groupId>
<artifactId>okhttpartifactId>
<version>3.9.0version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
首先在启动类中注册 RestTemplate :
/**
* @author bruceliu
* @create 2019-05-02 15:50
* @description
*/
@Configuration
public class RestTemplateConfig {
// 这次我们使用了OkHttp客户端,只需要注入工厂即可
@Bean
public RestTemplate restTemplate(){
return new RestTemplate(new
OkHttp3ClientHttpRequestFactory());
}
}
编写controller:
/**
* @author bruceliu
* @create 2019-05-02 15:52
* @description
*/
@RestController
@RequestMapping("consumer")
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/test")
public List<User> consumerTest(){
String url="http://127.0.0.1/all";
return this.restTemplate.getForObject(url,List.class);
}
}
因为我们没有配置端口,那么默认就是8080,我们访问:http://127.0.0.1:81/consumer/test
简单回顾一下,刚才我们写了什么:
- 在consumer中,我们把url地址硬编码到了代码中,不方便后期维护
- consumer需要记忆user-service的地址,如果出现变更,可能得不到通知,地址将失效
- consumer不清楚user-service的状态,服务宕机也不知道
- user-service只有1台服务,不具备高可用性
- 即便user-service形成集群,consumer还需自己实现负载均衡
其实上面说的问题,概括一下就是分布式服务必然要面临的问题:
- 如何自动注册和发现
- 如何实现状态监管
- 如何实现动态路由
希望大家关注我一波,防止以后迷路,有需要的可以加我Q讨论互相学习java ,学习路线探讨,经验分享与java Q:2415773436