轉(zhuǎn)載請標(biāo)明出處: http://www.reibang.com/p/4fe0c858b913
本文出自Owater的博客
服務(wù)提供者 (eureka client)
創(chuàng)建Module,命名為service-a
pom.xml如下:
<?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.owater</groupId>
<artifactId>service-a</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-a</name>
<description>Demo project for Spring Boot</description>
<parent>
<artifactId>docker-springcloud-parent</artifactId>
<groupId>com.owater</groupId>
<version>0.0.1</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<configuration>
<imageName>${project.name}:${project.version}</imageName>
<dockerDirectory>${project.basedir}/src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
</build>
</project>
Application添加@EnableEurekaClient 啟用eureka client
@SpringBootApplication
@EnableEurekaClient
public class ServiceAApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceAApplication.class, args);
}
}
application.yml文件還需要配置eureka注冊地址椎眯,配置如下:
server:
port: 8080
spring:
application:
name: service-a
eureka:
instance:
hostname: localhost
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:7000/eureka/
這里的spring.application.name是當(dāng)前該服務(wù)的名稱,各個服務(wù)之間的訪問通信是通過這個name得问,而并非是IP地址
添加個controller
@RestController
public class HomeController {
@Value("${server.port}")
String port;
@GetMapping("hi")
public String index(@RequestParam String name) {
return "hi "+name+", port:" +port;
}
public String hiError(String name) {
return "hi,"+name+",error lalalala!";
}
}
源碼參考:https://github.com/Owater/docker_springcloud/tree/master/service-a
Spring Cloud Eureka 官方文檔
轉(zhuǎn)載請標(biāo)明出處: http://www.reibang.com/p/4fe0c858b913
本文出自Owater的博客