.创建注册中心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 -DskipTests5.构建镜像并启动
将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