博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
docker-compose编排springcloud微服务
阅读量:6080 次
发布时间:2019-06-20

本文共 6582 字,大约阅读时间需要 21 分钟。

.创建注册中心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 @SpringBootApplicationpublic class EurekaServerApplication {    public static void main(String[] args) {        SpringApplication.run(EurekaServerApplication.class, args);    }}*Application.properties:*server.port=8761eureka.instance.prefer-ip-address=trueeureka.client.register-with-eureka=falseeureka.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;@RestControllerpublic 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;@Servicepublic 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);} 
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/testspring.datasource.username = rootspring.datasource.password = 824824spring.datasource.driverClassName = com.mysql.jdbc.Driverspring.datasource.max-active=20spring.datasource.max-idle=8spring.datasource.min-idle=8spring.datasource.initial-size=10####mybatis config ########mybatis.mapper-location= classpath:com/hzcf/mapper/*.xmlspring.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@EnableFeignClientspublic 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;@RestControllerpublic 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  /tmpADD eureka-0.0.1-SNAPSHOT.jar app.jarRUN 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.jarRUN 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.jarRUN 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.ymlversion: '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

转载地址:http://kxhgx.baihongyu.com/

你可能感兴趣的文章
结合当前公司发展情况,技术团队情况,设计一个适合的技术团队绩效考核机制...
查看>>
python-45: opener 的使用
查看>>
cad图纸转换完成的pdf格式模糊应该如何操作?
查看>>
Struts2与Struts1区别
查看>>
网站内容禁止复制解决办法
查看>>
Qt多线程
查看>>
我的友情链接
查看>>
想说一点东西。。。。
查看>>
css知多少(8)——float上篇
查看>>
NLB网路负载均衡管理器详解
查看>>
水平添加滚动条
查看>>
PHP中”单例模式“实例讲解
查看>>
VS2008查看dll导出函数
查看>>
VM EBS R12迁移,启动APTier . AutoConfig错误
查看>>
atitit.细节决定成败的适合情形与缺点
查看>>
iOS - Library 库
查看>>
MATLAB 读取DICOM格式文件
查看>>
spring事务管理(Transaction)
查看>>
django.contrib.auth登陆注销学习
查看>>
js执行本地exe文件的3种方法
查看>>