本博客參考http://how2j.cn/k/springcloud/springcloud-eureka-client/2039.html#nowhere後編寫
如果不知道怎麼創建註冊中心請移步 springCloud在父子項目基礎上創建服務註冊中心
1.創建子項目product-data-service
2.pom.xml內容如下
spring-cloud-starter-netflix-eureka-client 表示這是個 eureka 客戶端。
spring-boot-starter-web: 表示這是個web服務,會提供控制層
<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>cn.how2j.springcloud</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>productDataservice</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
3.創建ProductController.java代碼如下
package cn.how2j.springcloud.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.how2j.springcloud.pojo.Product;
import cn.how2j.springcloud.service.ProductService;
@RestController
public class ProductController {
@Autowired ProductService productService;
@RequestMapping("/products")
public Object products() {
List<Product> ps = productService.listProducts();
return ps;
}
}
4.創建Product.java代碼如下
package cn.how2j.springcloud.pojo;
public class Product {
private int id;
private String name;
private int price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public Product() {
}
public Product(int id, String name, int price) {
super();
this.id = id;
this.name = name;
this.price = price;
}
}
5.創建ProductService.java代碼如下
package cn.how2j.springcloud.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import cn.how2j.springcloud.pojo.Product;
@Service
public class ProductService {
@Value("${server.port}")
String port;
public List<Product> listProducts(){
List<Product> ps = new ArrayList<>();
ps.add(new Product(1,"product a from port:"+port, 50));
ps.add(new Product(2,"product b from port:"+port, 150));
ps.add(new Product(3,"product c from port:"+port, 250));
return ps;
}
}
6.創建ProductDataServiceApplication.java代碼如下
啓動第二個項目的時候別忘了修改端口號
package cn.how2j.springcloud;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.NetUtil;
import cn.hutool.core.util.NumberUtil;
/**向註冊中心中註冊
* @author xiaojian
*
*/
@SpringBootApplication
@EnableEurekaClient
public class ProductDataServiceApplication {
public static void main(String[] args) {
int port = 0;
int defaultPort = 8002;
Future<Integer> future = ThreadUtil.execAsync(() ->{
int p = 0;
System.out.println("請於5秒鐘內輸入端口號, 推薦 8001 、 8002 或者 8003,超過5秒將默認使用 " + defaultPort);
Scanner scanner = new Scanner(System.in);
while(true) {
String strPort = scanner.nextLine();
if(!NumberUtil.isInteger(strPort)) {
System.err.println("只能是數字");
continue;
}
else {
p = Convert.toInt(strPort);
scanner.close();
break;
}
}
return p;
});
try{
port=future.get(5,TimeUnit.SECONDS);
}
catch (InterruptedException | ExecutionException | TimeoutException e){
port = defaultPort;
}
if(!NetUtil.isUsableLocalPort(port)) {
System.err.printf("端口%d被佔用了,無法啓動%n", port );
System.exit(1);
}
new SpringApplicationBuilder(ProductDataServiceApplication.class).properties("server.port=" + port).run(args);
}
}
7.創建application.yml代碼如下
http://localhost:8761/eureka/ 和註冊中心對應
# server:
# port: 因爲會啓動多個 product-data-service, 所以端口號由用戶自動設置,推薦 8001,8002,8003
spring:
application:
name: product-data-service
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
8.操作步驟:先啓動註冊中心,然後啓動product-data-service項目(端口號爲8001),修改product-data-service端口號(端口號爲8002)後再啓動一次,這是訪問http://127.0.0.1:8761/會發現有端口號爲8001和8002的兩個服務
可以如此訪問: http://127.0.0.1:8001/products ,http://127.0.0.1:8002/products,並看到如圖所示的數據。
但是這種方式是通過 http 協議 訪問微服務本身,和註冊中心沒有關係,也觀察不到集羣的效果,接下來我們就會講如何用微服務,訪問另一個微服務。