docker-compose编排springcloud微服务

Posted qiuri2008

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker-compose编排springcloud微服务相关的知识,希望对你有一定的参考价值。

.创建注册中心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微服务的主要内容,如果未能解决你的问题,请参考以下文章

Docker-compose快速编排

docker-compose概述与编排部署

Docker-compose简介和部署编排

Docker-compose简介和部署编排

docker-compose编排启动LNMP

Docker-compose 容器 快速单机编排