10萬+IT人都在關注的史上最全面的企業級微服務快速開發學習腳踩:5分鐘快速搭建一個標準的Spring Cloud服務註冊中心
前言:springcloud項目是基於springboot框架,我們演示的都創建springboot工程
一、創建一個普通的Spring Boot工程
首先我們通過spring官網創建一個普通的Spring Boot工程,官網地址 https://start.spring.io/ 如下圖所示,springboot版本此處選擇1.5.19(不太穩定),創建好工程下載下來導入到開發工具裏,修改pom.xml文件裏springboot的版本號爲 1.5.6.RELEASE
添加Eureka依賴
工程創建成功並導入開發工具之後,修改工程名爲chenyun-cloud-eureka 向該工程的pom.xml文件中添加eureka-server的依賴,目前eureka的穩定版本是Dalston.SR3
,添加完依賴之後,pom.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<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>org.sang</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
啓動服務註冊中心
啓動服務註冊中心的方式很簡單,就是在Spring Cloud的入口類上添加一個@EnableEurekaServer
註解,如下
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
配置服務註冊中心
最後我們再做一點簡單的配置就可以了,配置就寫在Spring Cloud的配置文件application.yml中,寫法如下:
spring:
application:
name: eureka-center
server:
port: 8880
eureka:
instance:
hostname: 10.220.146.3
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
配置說明:
1.server.port=8880 表示設置該服務註冊中心的端口號
2.eureka.instance.hostname=10.220.146.3表示配置該服務註冊中心的hostname
3.eureka.client.register-with-eureka=false,由於我們目前創建的應用是一個服務註冊中心,而不是普通的應用,默認情況下,這個應用會向註冊中心(也是它自己)註冊它自己,設置爲false表示禁止這種默認行爲
4.eureka.client.fetch-registry=false,表示不去檢索其他服務,因爲服務註冊中心本身的職責就是維護服務實例,它也不需要去檢索其他服務
測試
現在我們就可以啓動這個Spring Cloud服務,服務啓動成功之後,在瀏覽器中輸入:http://localhost:8880就能夠看到如下頁面
看到上面這個頁面之後,表示服務註冊中心已經搭建好了。現在服務註冊中心有了之後,我們可以向這個服務註冊中心註冊一個服務提供者了。
我們用同樣的方式創建一個spring boot 工程,命名爲 chenyun-cloud-hello-service。現在我們添加一個Controller,在該Controller中提供一個訪問入口,如下:
@RestController
public class HelloController {
private final Logger logger = Logger.getLogger(HelloController.class);
@Autowired
private DiscoveryClient client;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@ResponseBody
public String index() {
List<ServiceInstance> instances = client.getInstances("HELLO-SERVICE");
for (int i = 0; i < instances.size(); i++) {
logger.info("/hello服務的host:" + instances.get(i).getHost() + ",service_id:" + instances.get(i).getServiceId());
}
return "Hello World";
}
}
這裏創建服務之後,在日誌中將服務相關的信息就可以打印出來了
激活Eureka中的DiscoveryClient
在Spring Cloud的入口函數處,通過添加@EnableDiscoveryClient註解來激活Eureka中的DiscoveryClient實現(因爲我們在HelloController中注入了DiscoveryClient)。
@EnableDiscoveryClient
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
配置服務名稱和註冊中心地址
最後的最後,我們在application.yml文件中配置一下服務名和註冊中心地址即可,如下:
spring:
application:
name: hello-service
server:
port: 8881
eureka:
instance:
instance-id: ${spring.application.name}:${server.port}
prefer-ip-address: true
client:
service-url:
defaultZone: http://10.220.146.3:8880/eureka/
測試
我們就可以測試了,直接運行這個Spring Cloud工程,運行成功之後,我們刷新剛纔的http://localhost:8880,就可以看到有一個服務已經註冊成功了。如下:
更多JavaEE資料請關注下面公衆號,歡迎廣大開發者朋友一起交流。筆者電話(微信):18629374628