您好,登錄后才能下訂單哦!
文章首發(fā)于公眾號《程序員果果》
地址:https://mp.weixin.qq.com/s/E_gJFbRaWOE-mnVR1lsYfQ
Docker的出現(xiàn)讓容器化技術(shù)得以普及,更快的部署和維護(hù)與Spring Cloud的結(jié)合,能讓我們不再像以前一樣為了某一個模塊的增加而服務(wù)器上大動干戈,還需要考慮環(huán)境的問題。在這一篇中會講到 SpringCloud 項(xiàng)目 Docker 化 。
創(chuàng)建一個springcloud項(xiàng)目 ,包含eureka-server、service-hi、service-ribbon。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gf</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.gf</groupId>
<artifactId>chapter02</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://eureka-server:8761/eureka/
spring:
application:
name: eureka-server
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gf</groupId>
<artifactId>service-hi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-hi</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.gf</groupId>
<artifactId>chapter02</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
server:
port: 8763
spring:
application:
name: service-hi
eureka:
client:
service-url:
defaultZone: http://eureka-server:8761/eureka/
@EnableEurekaClient
@SpringBootApplication
@RestController
public class ServiceHiApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceHiApplication.class, args);
}
@Value( "${server.port}" )
private String port;
@GetMapping("/hi")
public String hi() {
return "hello , port is " + port;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gf</groupId>
<artifactId>service-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-ribbon</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.gf</groupId>
<artifactId>chapter02</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
server:
port: 8764
spring:
application:
name: service-ribbon
eureka:
client:
service-url:
defaultZone: http://eureka-server:8761/eureka/
@Service
public class HelloService {
@Autowired
private RestTemplate restTemplate;
public String hiService() {
return restTemplate.getForObject( "http://SERVICE-HI/hi" , String.class );
}
}
@RestController
public class HelloControler {
@Autowired
private HelloService helloService;
@GetMapping(value = "/hi")
public String hi() {
return helloService.hiService();
}
}
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class ServiceRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
編寫Dockerfile ,把項(xiàng)目構(gòu)建成鏡像,需要把 項(xiàng)目jar包 復(fù)制到 鏡像中,而且鏡像中要有java的運(yùn)行環(huán)境,所以現(xiàn)在給每個項(xiàng)目都創(chuàng)建一個Dockerfile,內(nèi)容如下:
eureka-server 項(xiàng)目的 Dockerfile
FROM 192.168.31.143:9090/jdk/openjdk:8-jre
MAINTAINER gf gf@163.com
COPY target/eureka-server-0.0.1-SNAPSHOT.jar /eureka-server-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java" , "-jar" , "/eureka-server-0.0.1-SNAPSHOT.jar"]
service-hi 項(xiàng)目的 Dockerfile
FROM openjdk:8-jre
MAINTAINER gf 782969359@qq.com
COPY target/service-hi-0.0.1-SNAPSHOT.jar /service-hi-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java" , "-jar" , "/service-hi-0.0.1-SNAPSHOT.jar"]
service-ribbon 項(xiàng)目的 Dockerfile
FROM openjdk:8-jre
MAINTAINER gf gf@163.com
COPY target/service-ribbon-0.0.1-SNAPSHOT.jar /service-ribbon-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java" , "-jar" , "/service-ribbon-0.0.1-SNAPSHOT.jar"]
分別在各個項(xiàng)目根目錄下,通過 mvn packge 命令來,把項(xiàng)目打成jar包 ,并通過 docker build 命令來構(gòu)建鏡像:
mvn package -Dmaven.test.skip=true
docker build -t 項(xiàng)目名:版本號
例如:
docker build -t eureka-server:latest
我們通過 docker image 命令,查看我們本地的鏡像,發(fā)現(xiàn)鏡像已經(jīng)構(gòu)建到本地了:
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
service-ribbon latest 81b61a359e23 39 hours ago 487MB
service-hi latest f893c76be99d 39 hours ago 487MB
eureka-server latest 41997de8006f 40 hours ago 487MB
openjdk 8-jre e01608fba686 12 days ago 442MB
根據(jù)這三個鏡像運(yùn)行容器 ,來檢查服務(wù)容器化后 ,是否訪問正常,值得注意的是 注冊中心的地址為 http://eureka-server:8761/eureka/ ,我們使用容器名作為地址 ,這就需要,我們在啟動eureka-server 的容器時,指定指定一個名稱eureka-server ,否則會找不到注冊中心。
docker run -it -d -p 8761:8761 --name eureka-server 41997de8006f
docker run -it -d --name service-hi f893c76be99d
docker run -it -d -p 8764:8764 --name service-ribbon 81b61a359e23
訪問 127.0.0.1:8761 注冊中心正常。
訪問 127.0.0.1:8764/hi 服務(wù)正常。
歡迎關(guān)注我的公眾號《程序員果果》,關(guān)注有驚喜~~
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。