使用環境是 STS + maven
1 創建父級 項目,springcloud-demo1
new -> maven project ->
按照要求進行配置即可。然后刪除 src目錄,因為父級項目只是配置項目依賴和版本控制使用。
1.2 修改 pom 文件
<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.huitong</groupId> <artifactId>springcloud-demo1</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.SR4</spring-cloud.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.13.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <modules> </modules> </project>
此時也要注意springcloud 和 springboot 版本之間有一定的關聯性,最好使用官網推薦的對應版本。否則可能會出現版本不兼容的問題。我就是這么趟的坑過來的
2 創建 eureka server端項目,spring-cloud-sureka-server
在 springcloud-demo1 項目上右擊, new -> maven module
2.2 修改 pom 文件,添加 eureka-server 依賴, spring-cloud-starter-eureka-server
<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> <parent> <groupId>com.huitong</groupId> <artifactId>springcloud-demo1</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>spring-cloud-eureka-server</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.3 添加配置文件 application.yml
server: port: 8010 spring: application: name: eureka-server eureka: instance: hostname: 127.0.0.1 server: # 關閉自我保護,當服務出現故障自動剔除。清理間隔(單位毫秒,默認是60*1000) enableSelfPreservation: false client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
eureka 配置說明 可見 http://www.cnblogs.com/li3807/p/7282492.html
在 server 端 service-url 指定暴露出的注冊服務地址。讓客戶端可以將應用注冊到該地址上。
2.4 添加啟動類,StartEurekaServer
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class StartEurekaServer { public static void main(String[] args) { SpringApplication.run(StartEurekaServer.class, args); } }
3 新建一個服務提供端 module,spring-cloud-eureka-client-demo1,跟 eureka 方法一樣,在springcloud-demo1項目上右擊, new -> maven module
3.2 修改 pom 文件
<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> <parent> <groupId>com.huitong</groupId> <artifactId>springcloud-demo1</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>spring-cloud-eureka-client-demo1</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> </dependencies> </project>
3.3 添加配置文件 application.yml
server: port: 8020 spring: application: name: eureka-client-demo1 eureka: instance: preferIpAddress: true hostname: 127.0.0.1 instanceId: ${spring.cloud.client.ipAddress:127.0.0.1}:${server.port} client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://${eureka.instance.hostname}:${eureka.port:8010}/eureka/
3.4 編寫啟動類,SpringCloudDemo1
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient public class SpringCloudDemo1 { public static void main(String[] args) { SpringApplication.run(SpringCloudDemo1.class, args); } }
3.5 簡單的實體類,Student
import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor @Builder public class Student { private String name; private Integer age; }
3.6 Controller
import org.apache.commons.lang3.ObjectUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class StudentController { @RequestMapping(value="/student/demo1", method=RequestMethod.GET) public Student getStu1() { Student result = Student.builder().name("allen").age(23).build(); if (ObjectUtils.allNotNull(result)) { System.out.println(result); } return result; } }
此時訪問頁面 :http://localhost:8010/
說明:由於springcloud 版本低或者什么原因,在編寫配置時,使用的是 橫線命名法 啟動的時候一直給我報錯,com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
后來查閱大量資料發現將配置屬性全部改為 駝峰命名法 就好了