走進Spring Cloud之三 eureka Producer(服務提供者)(Greenwich版本)
阿新 • • 發佈:2018-12-20
service-producer(eureka client)
前面我們已經成功的建立了叢集版的註冊中心,接下來我們就可以把我們的服務註冊到我們叢集註冊中心提供給消費者使用。
新建moudel(service-producer)
pom.xml
修改pom.xml新增eureka-client依賴
<?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">
<parent>
<artifactId>scexample</artifactId>
<groupId>com.pubutech</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion >4.0.0</modelVersion>
<artifactId>service-producer</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</ artifactId>
</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>
application.yml
resource目錄下新建application.yml配置檔案,配置一下資訊:
server:
port: 8090
spring:
application:
name: service-producer
eureka:
client:
service-url:
#設定與Eureka Server互動的地址,查詢服務和註冊服務都需要依賴這個地址。預設是http://localhost:8761/eureka ;多個地址可使用 , 分隔。
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
ServiceProducerApplication.java
新建ServiceProducerApplication(SpringBoot Application)
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient
public class ServiceProducerApplication{
public static void main(String[] args) {
SpringApplication.run(ServiceProducerApplication.class, args);
}
}
新增@EnableDiscoveryClient註解後,專案就具有了服務註冊的功能。啟動工程後,就可以在註冊中心的頁面看到SERVICE-PRODUCER服務。
ProducerController.java
接下來定義我們可以提供的服務,新建ProducerController.java
@RestController
public class ProducerController {
@RequestMapping("/hello")
public String hello(@RequestParam String name) {
return "hello "+name+",this is new world";
}
}
啟動service-producer 再次訪問 localhost:8761