搭建SpringCloud註冊服務提供者和服務消費者之Eureka
阿新 • • 發佈:2018-12-05
一.註冊服務提供者
根據上節講述的服務註冊之Eureka註冊中心,這節講述服務提供者和服務消費者provider
1.1、先新建一個Maven專案,會員提供者
1.2、 引入依賴
<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>
1.3 、配置application.yml
###服務啟動埠號 server: port: 8000 ###服務名稱(服務註冊到eureka名稱) spring: application: name: app-thinkingcao-member ###在此指定服務註冊中心地址,服務註冊到eureka地址 eureka: client: service-url: defaultZone: http://localhost:8888/eureka ###啟動註冊操作,該值預設為true。若設定為fasle將不會啟動註冊操作。是否需要去檢索尋找服務,預設是###true register-with-eureka: true ###是否需要從eureka上獲取註冊資訊 fetch-registry: true
1.4 編寫MemberController
package com.thinkingcao.modules.controller; import java.util.ArrayList; import java.util.List; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MemberController { @RequestMapping("/getUserList") public List<String> getUserList() { List<String> listUser = new ArrayList<String>(); listUser.add("zhangsan"); listUser.add("lisi"); listUser.add("caowencao"); return listUser; } }
1.5、 編寫member-provider啟動類,然後啟動member-provider會員服務,(注意:在啟動member-provider服務之前,必須先啟動eureka—server服務,否則報錯,因為要將member-provider服務註冊到eureka—server註冊中心)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* <pre>
* @author cao_wencao
* @date 2018年11月14日 上午10:16:42
* </pre>
*/
@SpringBootApplication
@EnableEurekaClient
public class ProviderApplication {
/**
* <pre>
* @author cao_wencao
* @param args
* </pre>
*/
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
1.6、 訪問Eureka註冊中心,URL地址 : http://localhost:8888/ , 會員服務已經註冊到Eureka註冊中心了
二.註冊服務消費者
2.1、新建一個Maven專案,訂單消費者
2.2、引入依賴pom.xml
<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>
2.3、配置application.yml
###服務啟動埠號
server:
port: 8001
###服務名稱(服務註冊到eureka名稱)
spring:
application:
name: app-thinkingcao-order
###服務註冊到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka
###因為該應用為註冊中心,不會註冊自己
register-with-eureka: true
###是否需要從eureka上獲取註冊資訊
fetch-registry: true
2.4、 編寫OrderController
package com.thinkingcao.modules.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* <pre>
* @author cao_wencao
* @date 2018年11月28日 下午2:43:19
* </pre>
*/
@RestController
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@RequestMapping("/getOrder")
public String getOrder() {
// order 使用rpc 遠端呼叫技術 呼叫 會員服務
String memberUrl = "http://app-thinkingcao-member/getUserList";
String result = restTemplate.getForObject(memberUrl, String.class);
System.out.println("會員服務呼叫訂單服務,result:" + result);
return result;
}
}
2.5、 編寫order-consumer啟動類,然後啟動order-consumer 訂單服務(啟動順序: 註冊中心——會員提供者——訂單消費者)
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* <pre>
* @author cao_wencao
* @date 2018年11月14日 上午10:16:42
* </pre>
*/
@SpringBootApplication
@EnableEurekaClient
public class ConsumerApplication {
/**
* <pre>
*
* @author cao_wencao
* @param args
* </pre>
*/
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
// @LoadBalanced就能讓這個RestTemplate在請求時擁有客戶端負載均衡的能力
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
2.6、 Console截圖:
2.7、 訪問Eureka註冊中心,URL地址 : http://localhost:8888/ ,訂單服務已經註冊到Eureka註冊中心了
2.8、 訪問訂單服務,測試訂單服務通過註冊中心調取會員服務獲取的資料,URL:http://localhost:8001/getorder
2.9、檢視Console控制檯
專案原始碼:https://github.com/Thinkingcao/SpringCloudBucket.git