直接提供服務,入門案例沒有特別要設置的地方,注意下端口,因爲要啓動多個服務,可能會衝突web
server: port: 8000
服務消費者的依賴在這個單獨的demo中其實無關緊要,親測不添加,也能夠實現demo服務提供能spring
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class MovieApplication { @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(MovieApplication.class, args); } }
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.xujie.pojo.User; @RestController public class UserController { @Autowired private RestTemplate restTemplate; @GetMapping("/getUser") public User getUser() { return this.restTemplate.getForObject("http://localhost:8000/getUser", User.class); } }
此時能夠經過訪問消費者,間接調用服務提供者的服務,springboot
<!-- springcloud版本聲明 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- 引入eureka依賴 --> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies>
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer //聲明這是一個Eureka服務器 public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
server: port: 8761 #聲明端口號 eureka: client: register-with-eureka: false #默認是true,將本身註冊到eureka上 fetch-registry: false #是否從eureka上獲取信息,因爲本案例是單機,無需從別的eureka上獲取註冊信息 service-url: defaultZone: http://localhost:8761/eureka #設置與eureka交互的地址,查詢服務和註冊服務都須要依賴這個地址,默認是:http://localhost:8761/eureka
<!-- springcloud版本聲明 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Brixton.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!-- eureka的依賴 --> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies>
spring: application: name: provider #註冊到Eureka Server上的應用名稱 eureka: client: service-url: defaultZone: http://localhost:8761/eureka #註冊的位置 instance: prefer-ip-address: true #將本身的ip註冊到EuekaServer上
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient //也能夠用EnableDiscoveryClient代替,前者兼容性更大,後者僅能兼容Eureka public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
此時能夠正常啓動並將服務註冊到了eureka中
啓動Eureka和服務提供者訪問:http://localhost:8761,界面以下:
服務器
在這裏demo以前,修改本地hosts文件,爲了區分本地的兩個eureka節點,分別經過:http://peer1:8761和http://peer2:8762訪問app
eureka1的配置:ide
server: port: 8761 #聲明端口號 eureka: instance: hostname: peer1 appname: peer1 client: #register-with-eureka: false #默認是true,將本身註冊到eureka上,這裏設置eureka的高可用,因此須要將本身註冊到eureka上 #fetch-registry: false #是否從eureka上獲取信息,因爲本案例是單機,無需從別的eureka上獲取註冊信息,這裏設置eureka的高可用,因此須要在eureka上獲取服務 service-url: defaultZone: http://peer2:8762/eureka #設置與eureka交互的地址,查詢服務和註冊服務都須要依賴這個地址,默認是:http://localhost:8761/eureka
eureka2的配置:fetch
server: port: 8762 #聲明端口號 eureka: instance: hostname: peer2 appname: peer2 client: #register-with-eureka: false #默認是true,將本身註冊到eureka上,這裏設置eureka的高可用,因此須要將本身註冊到eureka上 #fetch-registry: false #是否從eureka上獲取信息,因爲本案例是單機,無需從別的eureka上獲取註冊信息,這裏設置eureka的高可用,因此須要在eureka上獲取服務 service-url: defaultZone: http://peer1:8761/eureka #設置與eureka交互的地址,查詢服務和註冊服務都須要依賴這個地址,默認是:http://localhost:8761/eureka
此時啓動兩個服務,界面以下
這是peer1:
this
下面這個是peer2:編碼