.创建注册中心Eureka

package com.dan.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
} *Application.properties:* server.port=8761
eureka.instance.prefer-ip-address=true
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false #注册地址
eureka.client.serviceUrl.defaultZone=http://eureka:8761/eureka/

2.创建服务提供者 provider

ProviderApplication :

package com.hzcf;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
@MapperScan("com.hzcf.mapper")//扫描:该包下相应的class,主要是MyBatis的持久化类.
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
} Controller:
package com.hzcf.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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.hzcf.model.User;
import com.hzcf.service.UserService; @RestController
public class UserController {
@Autowired
private UserService demoService;
@GetMapping("/findById/{id}")
@ResponseBody
public User findById(@PathVariable Long id){
User user = demoService.findOne(id);
return user;
}
} Service:
package com.hzcf.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.hzcf.mapper.UserMapper;
import com.hzcf.model.User; @Service
public class UserService { @Autowired
private UserMapper userMapper; public User findOne(Long id) {
return userMapper.findOne(id);
}
}
Mapper:
package com.hzcf.mapper;
import com.hzcf.model.User;
public interface UserMapper {
public User findOne(Long id);
} <?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.hzcf.mapper.UserMapper" >
<resultMap id="BaseResultMap" type="com.hzcf.model.User" >
<id column="id" property="id" jdbcType="BIGINT" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
</resultMap>
<select id="findOne" parameterType="long" resultMap="BaseResultMap">
select * from user where id = #{id}
</select>
</mapper> Model:
package com.hzcf.model; public class User { private long id; private String name; private String password; public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} } Application.properties #端口
server.port=9999
###datasource
########################################################
spring.datasource.url = jdbc:mysql://47.94.11.55:3306/test
spring.datasource.username = root
spring.datasource.password = 824824
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10 ####mybatis config ########
mybatis.mapper-location= classpath:com/hzcf/mapper/*.xml spring.application.name=provider
# 注册中心
eureka.client.serviceUrl.defaultZone=http://eureka:8761/eureka/

3.创建服务消费者 customer(用feign调用生产者:)

package com.hzcf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
} } Controller:
package com.hzcf.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.RestController;
import com.hzcf.model.User;
@RestController
public class CustomerController { @Autowired
private CustomerFeignClient customerFeignClient;
@GetMapping("/customer/{id}")
public User findById(@PathVariable Long id) {
return this.customerFeignClient.findById(id);
} } FeignClient:
package com.hzcf.controller;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import com.hzcf.model.User;
@FeignClient("provider")
public interface CustomerFeignClient { @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
public User findById(@PathVariable("id") Long id);
}

4.执行命令 生成jar包 
clean install -DskipTests

5.构建镜像并启动

将jar包上传到服务器,并分别创建Dockerfile文件

Eureka Dockerfile:

#基于哪个镜像
FROM lwieske/java-8
#将本地文件夹挂载到当前容器
VOLUME /tmp
ADD eureka-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
#声明暴露的端口
EXPOSE 8761
#配置容器启动后执行的命令
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ] Provider Dockerfile: #基于哪个镜像
FROM lwieske/java-8
#将本地文件夹挂载到当前容器
VOLUME /tmp
#赋值文件到容器
ADD provider-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
#声明暴露的端口
EXPOSE 9999
#配置容器启动后执行的命令
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ] Customer Dockerfile: #基于哪个镜像
FROM lwieske/java-8
#将本地文件夹挂载到当前容器
VOLUME /tmp
#赋值文件到容器
ADD customer-0.0.1-SNAPSHOT.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
#声明暴露的端口
EXPOSE 8763
#配置容器启动后执行的命令
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ] 创建docker-compose.yml
version: '2'
services:
eureka:
build: ./eureka #指定Dockerfile所在路径
ports:
- "8761:8761" provider:
build: ./provider
ports:
- "9999:9999"
links:
- "eureka" customer:
build: ./customer
ports:
- "8763:8763"
links:
- "eureka"
- "provider"

6.服务器结构图如下

  

7.启动:

docker-compose up

  

查看生成的镜像:

docekr images

8.测试

访问注册中心:http://47.94.11.55:8761/

访问消费者:http://47.94.11.55:8763/customer/1

源码链接:https://download.csdn.net/download/qq_35314762/10620879

docker-compose编排springcloud微服务的更多相关文章

  1. 十一、Docker搭建部署SpringCloud微服务项目Demo

    环境介绍 技术选型:SpringCloud&SpringCloud Alibaba&Docker 微服务模块划分: 员工模块:ems-employees 部门模块:ems-depart ...

  2. 小D课堂 - 新版本微服务springcloud+Docker教程_3-04 SpringCloud微服务核心组件Eureka介绍和闭源后影响

    笔记 4.SpringCloud微服务核心组件Eureka介绍和闭源后影响     简介:         SpringCloud体系介绍             官方地址:http://projec ...

  3. 08 . Jenkins之SpringCloud微服务+Vue+Docker持续集成

    简介 大致流程 /* 1.开发人员每天把代码提交到Gitlab代码仓库 2.jenkins从gitlab中拉取项目源码,编译并打包成war包,然后构建Docker镜像,将镜像上传到Harbor私有仓库 ...

  4. Devops 开发运维高级篇之Jenkins+Docker+SpringCloud微服务持续集成(上)

    Devops 开发运维高级篇之Jenkins+Docker+SpringCloud微服务持续集成(上) Jenkins+Docker+SpringCloud持续集成流程说明 大致流程说明: 1) 开发 ...

  5. Devops 开发运维高级篇之Jenkins+Docker+SpringCloud微服务持续集成——部署方案优化

    Devops 开发运维高级篇之Jenkins+Docker+SpringCloud微服务持续集成--部署方案优化 之前我们做的方案部署都是只能选择一个微服务部署并只有一台生产服务器,每个微服务只有一个 ...

  6. Docker、kubernetes、微服务、SpringBoot/Cloud...好乱!到底要不要学?

    Docker.微服务日益火热的今天,相信标题上这些名词大家都不陌生.但也相信有很多同学并不够清楚他们的概念,不理解它们的关系,也可能有这样的疑惑:不知道跟我有没有关系?要不要学习?怎么去学习?学哪些东 ...

  7. QCon技术干货:个推基于Docker和Kubernetes的微服务实践

    2016年伊始,Docker无比兴盛,如今Kubernetes万人瞩目.在这个无比需要创新与速度的时代,由容器.微服务.DevOps构成的云原生席卷整个IT界.在近期举办的QCon全球软件开发大会上, ...

  8. springcloud微服务架构搭建

    SpringCloud微服务框架搭建 一.微服务架构 1.1什么是分布式 不同模块部署在不同服务器上 作用:分布式解决网站高并发带来问题 1.2什么是集群 多台服务器部署相同应用构成一个集群 作用:通 ...

  9. 使用Docker compose编排Laravel应用

    前言 Laravel官方开发环境推荐的是Homestead(其实就是一个封装好的Vagrant box),我感觉这个比较重,于是自己用Docker compose编排了一套开发环境,在这里分享下. 环 ...

随机推荐

  1. Oracle表的查询(一)

    表查询关键字.字段.表名不加引号时不区分大小写引号定义的内容区分大小写运算中有null值时,结果为null*nvl(字段,赋值):如果字段值为null,则取后面一个值*like 关键字:%表示若干个字 ...

  2. 10.2.0.5环境dg测试logminer挖掘日志分析

    起因:客户需求,数据库正常每天总的日志切换是20以内,有一天日志切换总数,达到30,客户建议使用Logminer进行日志挖掘分析,到底什么应用导致的问题. 说明:使用logminer进行日志挖掘,只能 ...

  3. MySQL内置功能

    视图 # 创建视图 create view course2teacher as select * from course inner join teacher on course.teacher_id ...

  4. Python之路,第十二篇:Python入门与基础12

    python3 函数3 装饰器 decorator   *** 概念:装饰器是一个函数,主要作用是用来包装另一个函数或类: 包装的目的:是在不改变原函数名的情况下,改变被包装函数(对象)的行为. 装饰 ...

  5. TFLearn 与 Tensorflow 一起使用

    好用的不是一点点..=-=.. import tensorflow as tf import tflearn import tflearn.datasets.mnist as mnist # Usin ...

  6. [LeetCode&Python] Problem 563. Binary Tree Tilt

    Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the ab ...

  7. [LeetCode&Python] Problem 383. Ransom Note

    Given an arbitrary ransom note string and another string containing letters from all the magazines, ...

  8. currentStyle&getComputedStyle获取属性

    方法如下: function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; // IE 中的方法 } ...

  9. mvc core2.1 Identity.EntityFramework Core 登录 (三)

    Controllers->AccountController.cs 新建 [HttpGet] [AllowAnonymous] public async Task<IActionResul ...

  10. ajax及其工作原理

    1.关于ajax的名字 ajax 的全称是Asynchronous JavaScript and XML,其中,Asynchronous 是异步的意思,它有别于传统web开发中采用的同步的方式. 2. ...