發現不少招聘都須要會springcloud,因此最近在學習springcloud。
必需要從零開始用IDEA搭建一下這個springcloud框架,本文是採用Eureka做爲服務註冊與發現的組件。java
如今咱們須要對服務中心eureka-server進行一些相關的配置了。
打開它的pom.xml文件,咱們看到是這樣的:web
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zhouxiaoxi</groupId> <artifactId>eureka-server</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-server</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-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>${spring-cloud.version}</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>
而後咱們須要啓動一個服務註冊中心,這須要在springboot工程的啓動application類上加一個註解@EnableEurekaServer:spring
package com.zhouxiaoxi.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
先將配置文件改成yml,而後進行相關的配置,以下:apache
#端口號 server: port: 8080 eureka: instance: hostname: localhost client: #服務註冊中心也會將本身做爲客戶端來嘗試註冊本身,爲true(默認)時自動生效。 registerWithEureka: false #檢索服務選項,當設置爲True(默認值)時,會進行服務檢索,註冊中心不負責檢索服務。 fetchRegistry: false #是一個默認的註冊中心地址。配置該選項後,能夠在服務中心進行註冊。 serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ spring: application: name: eurka-server
覈對一下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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.zhouxiaoxi</groupId> <artifactId>eureka-client</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-client</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</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>
咱們這邊也須要對啓動類進行相關的註解添加:
由於咱們這邊用的是eureka搭建的服務中心,因此咱們這邊仍是用@EnableEurekaClient註解就能夠了springboot
package com.zhouxiaoxi.eurekaclient; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient //@EnableDiscoveryClient註解是基於spring-cloud-commons依賴,至關於一個公共的服務發現; //@EnableEurekaClient註解是基於spring-cloud-netflix依賴,只能爲eureka做用; public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); } }
須要對配置文件進行相關的配置:app
server: port: 8081 spring: application: name: eureka-client eureka: client: serviceUrl: #註明本身的服務註冊中心的地址 defaultZone: http://localhost:8080/eureka/
新建一個controller,而後寫一個hello方法:框架
package com.zhouxiaoxi.eurekaclient.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Value("${server.port}") String port; @RequestMapping("/hello") public String home(@RequestParam(value = "name") String name) { return "hello " + name + " ,i am from port:" + port; } }
啓動該工程以後,打開剛纔的瀏覽器頁面刷新後會發現一個服務已經註冊在服務中了,服務名爲EUREKA-CLIENT ,端口爲8081:maven
這時打開 http://localhost:8081/hello?name=world,你會在瀏覽器上看到 :spring-boot
hello world ,i am from port:8081
至此,咱們的一個較爲完整的項目就搭建起來了。
恩,先這樣吧,後續再更新。。。