Docker+Kubernetes(k8s)微服务容器化实践
Docker+Kubernetes(k8s)微服务容器化实践课程连接:https://coding.imooc.com/learn/list/198.html
第1章 初识微服务
1-1 微服务-导学
容器技术的成熟为微服务的落地提供了基础,轻量化的容器是微服务运行的最佳环境。
微服务只有在容器(Docker)环境下,再结合有效服务编排框架(K8s),才能使持续集成快速交付变成可能。
课程内容
- 开发: 业务场景架构图
- 部署:
- 服务编排:
自动化持续集成和部署
从代码提价到服务部署自动化
1-2 软件架构的进化 (11:40)
1-3 什么是微服务
1-4 画出微服务架构图
1-5 微服务架构的优势和不足
第2章 微服务带来的问题及解决方案分析
2-1 微服务架构带来的问题
2-2 微服务间如何通讯
通信模式:
通信协议:
2-3 服务发现、部署更新和扩容
1. 服务发现:
我们的服务对外表现的形式一般是ip+端口号,服务发现的本质就是让服务调用者/客户端如何知道服务提供的ip和端口号
一般单体服务发现(Eginx)
但不是自动的发现,nginx转发ip和端口需要手动配置
微服务中的服务发现
2. 服务部署
单体项目部署
微服务部署
服务编排
2-4 springboot&springcloud(上)
2-5 springboot&springcloud(下) (12:17)
第3章 微服务开发
3-1 微服务业务分析
3-2 Thirft安装和验证 (08:20)
3-3 Python开发信息服务 (24:28)
3-4 开发用户服务(上) (15:21)
3-5 开发用户服务(下)
mapper接口
package com.imooc.user.mapper;
import com.imooc.thrift.user.UserInfo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
/**
* Created by Michael on 2017/10/28.
*/
@Mapper
public interface UserMapper {
@Select("select id,username, password, real_name as realName," +
"mobile, email from pe_user where id=#{id}")
UserInfo getUserById(@Param("id")int id);
@Select("select id,username, password, real_name as realName," +
"mobile, email from pe_user where username=#{username}")
UserInfo getUserByName(@Param("username")String username);
@Insert("insert into pe_user (username, password, real_name, mobile, email)" +
"values (#{u.username}, #{u.password}, #{u.realName}, #{u.mobile}, #{u.email})")
void registerUser(@Param("u") UserInfo userInfo);
@Select("select u.id,u.username,u.password,u.real_name as realName," +
"u.mobile,u.email,t.intro,t.stars from pe_user u," +
"pe_teacher t where u.id=#{id} " +
"and u.id=t.user_id")
UserInfo getTeacherById(@Param("id")int id);
}
Services使用
package com.imooc.user.service;
import com.imooc.thrift.user.UserInfo;
import com.imooc.thrift.user.UserService;
import com.imooc.user.mapper.UserMapper;
import org.apache.thrift.TException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by Michael on 2017/10/28.
*/
@Service
public class UserSerivceImpl implements UserService.Iface {
@Autowired
private UserMapper userMapper;
@Override
public UserInfo getUserById(int id) throws TException {
return userMapper.getUserById(id);
}
@Override
public UserInfo getTeacherById(int id) throws TException {
return userMapper.getTeacherById(id);
}
@Override
public UserInfo getUserByName(String username) throws TException {
return userMapper.getUserByName(username);
}
@Override
public void regiserUser(UserInfo userInfo) throws TException {
userMapper.registerUser(userInfo);
}
}
3-6 开发用户EdgeService_A
使用Docker启动Redis
SpringBoot 整合Redis
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
package com.imooc.user.redis;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Redis缓存配置类
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
//缓存管理器
@Bean
public CacheManager cacheManager(RedisTemplate redisTemplate) {
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
//设置缓存过期时间
cacheManager.setDefaultExpiration(10000);
return cacheManager;
}
@Bean
public JedisConnectionFactory redisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory();
factory.setHostName(host);
factory.setPort(port);
factory.setTimeout(timeout);
return factory;
}
@Bean
public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory){
StringRedisTemplate template = new StringRedisTemplate(factory);
setSerializer(template);//设置序列化工具
template.afterPropertiesSet();
return template;
}
private void setSerializer(StringRedisTemplate template){
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
template.setValueSerializer(jackson2JsonRedisSerializer);
}
}
package com.imooc.user.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* Created by Michael on 2017/10/30.
*/
@Component
public class RedisClient {
@Autowired
private RedisTemplate redisTemplate;
public <T> T get(String key) {
return (T)redisTemplate.opsForValue().get(key);
}
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
public void set(String key, Object value, int timeout) {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
public void expire(String key, int timeout) {
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
}
}
//2. 生成token
String token = genToken();
//3. 缓存用户
redisClient.set(token, toDTO(userInfo), 3600);
return new LoginResponse(token);
@RequestMapping(value="/authentication", method = RequestMethod.POST)
@ResponseBody
public UserDTO authentication(@RequestHeader("token") String token) {
return redisClient.get(token);
}
3-7 开发用户EdgeService_B (28:54)
3-8 开发用户EdgeService_C (25:24)
3-9 开发用户EdgeService_D (12:53)
3-10 dubbo入门操练(上) (13:45)
3-11 dubbo入门操练(下) (13:48)
3-12 开发课程服务 (28:31)
3-13 开发课程EdgeService (21:11)
3-14 APIGatewayZuul (14:56)
第4章 服务编排前奏
4-1 服务docker化(上)
去Docker官网下载一个基础的java镜像
Dockerfile 文件
FROM openjdk:7-jre
MAINTAINER xxx xxx@imooc.com
COPY target/user-thrift-service-1.0-SNAPSHOT.jar /user-thrift-service.jar
ENTRYPOINT [ "java", "-jar", "/user-thrift-service.jar" ]
写脚本方:直接打包后编译成Docker镜像
4-2 服务docker化(下) (20:30)
4-3 docker下的服务通讯(上)
docker-compose.yml
version: '3'
services:
message-service:
image: message-service:latest
user-service:
image: user-service:latest
command:
- "--mysql.address=192.168.1.8"
user-edge-service:
image: user-edge-service:latest
links:
- user-service
- message-service
command:
- "--redis.address=192.168.1.8"
course-service:
image: course-service:latest
links:
- user-service
command:
- "--mysql.address=192.168.1.8"
- "--zookeeper.address=192.168.1.8"
course-edge-service:
image: course-edge-service:latest
links:
- user-edge-service
command:
- "--zookeeper.address=192.168.1.8"
api-gateway-zuul:
image: api-gateway-zuul:latest
links:
- user-edge-service
- course-edge-service
ports:
- 8080:8080
4-4 docker下的服务通讯(下) (11:24)
4-5 镜像仓库 (23:44)
4-6 三大平台扬帆起航 (02:49)
第5章 服务编排-Mesos
5-1 了解Mesos (17:45)
5-2 画出Mesos集群架构图 (07:04)
5-3 集群环境搭建_A (16:20)
5-4 集群环境搭建_B (14:58)
5-5 集群环境搭建_C (13:27)
5-6 调整微服务适应Mesos (11:55)
5-7 微服务部署_A (11:41)
5-8 微服务部署_B (15:52)
5-9 微服务部署_C (15:17)
第6章 服务编排-DockerSwarm
6-1 了解Swarm (16:41)
6-2 集群环境搭建(上) (18:18)
6-3 集群环境搭建(下) (17:46)
6-4 调整微服务及服务配置 (16:36)
6-5 微服务部署 (25:30)
第7章 服务编排-Kubernetes
7-1 了解kubernetes(上)
7-2 了解kubernetes(下)
7-3 环境搭建前奏
GItHub: kubernetes入门,包括kubernetes概念,架构设计,集群环境搭建,认证授权等。
7-4 预先准备环境 (15:08)
7-5 基础集群部署(上) (12:58)
7-6 基础集群部署(下) (16:05)
7-7 小试牛刀 (27:22)
7-8 kube-proxy和kube-dns (21:17)
7-9 理解认证、授权 (13:22)
7-10 为集群添加认证授权(上) (13:58)
7-11 为集群添加认证授权(下) (26:53)
7-12 再试牛刀 (11:41)
7-13 部署我们的微服务 (23:42)
第8章 CICD和DevOps
8-1 了解CICD和DevOps
DevOps是CICD的延申,CICD是DevOps的基础核心, DevOps是CICD的落地实现
CI(持续集成) 目前比较流行的持续集成工具: Jenkins
持续集体的CICD的具体步骤:
8-2 准备GitLab和Jenkins (18:09)
8-3 CICD实践(上) (18:42)
8-4 CICD实践(下) (19:56)
第9章 课程总结
9-1 -课程总结 (05:10)