SpringCloud2.0之服務提供者搭建
阿新 • • 發佈:2018-11-12
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.toov5</groupId> <artifactId>member</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.1.RELEASE</version> </parent> <!-- 管理依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- SpringBoot整合Web元件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- SpringBoot整合eureka客戶端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <!-- 注意: 這裡必須要新增, 否者各種依賴有問題 --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/libs-milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> </project>
yml檔案:
###服務啟動埠號 server: port: 8000 ###服務名稱(服務註冊到eureka名稱) spring: application: name: app-toov5-member ###服務註冊到eureka地址 eureka: client: service-url: ##當前會員註冊到eureka服務 地址+埠號 defaultZone: http://127.0.0.1:8100/eureka ###因為該應用為註冊中心,不會註冊自己 register-with-eureka: true ###是否需要從eureka上獲取註冊資訊 fetch-registry: true
controller類
package com.toov5.api.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MemberApiController { @RequestMapping("/getMember") public String getMember() {return "會員服務"; } }
啟動類
package com.toov5.api.app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient //註冊到eureka public class AppMember { public static void main(String[] args) { SpringApplication.run(AppMember.class, args); } }
先啟動eureka,再啟動服務:
上面Eureka會幫助生成地址~
通過獲取服務名字 然後獲取訪問地址