Service的提供与调用
上节讲到Spring Cloud 注册中心Eureka的使用,下面讲Service的提供与调用。
服务Service的提供方 Service Provider
两个注解:@EnableDiscoveryClient
和@EnableFeignClients
启动类:
package io.ymq.example.eureka.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@RestController
public class EurekaProviderApplication {
@RequestMapping("/home")
public String home() {
return "home";
}
public static void main(String[] args) {
SpringApplication.run(EurekaProviderApplication.class, args);
}
}
这里写了一个Rest请求的路径/home
同样的,加入一些依赖dependence
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
此外,往pom中加入dependencyManagement的配置
<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>
application.yml
配置:
eureka:
enabled: true
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
spring:
application:
name: eureka-provider
server:
port: 8081
这里依次启动注册中心,eureka-provider服务。
用浏览器打开http://localhost:8888
可以看到我们新增的服务已经在注册中心Eureka中成功注册了。
然后我们打开http://localhost:8081/home就可以看到:
那服务如何在另外的服务中调用呢?
服务Service的调用方 Service Consumer
同样的,新建另外一个项目。
两个注解:@EnableDiscoveryClient
和@EnableFeignClients
启动类:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SpringCloudDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudDemoApplication.class, args);
}
}
同样的,加入一些依赖dependence
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<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>
此外,往pom中加入dependencyManagement的配置
<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>
application.yml
配置:
eureka:
enabled: true
client:
serviceUrl:
defaultZone: http://localhost:8888/eureka/
spring:
application:
name: eureka-demo
server:
port: 8082
新建一个接口,为了调用远程服务(上面注册的那个eureka-provider)
package com.example.demo;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
@FeignClient(name= "eureka-provider")
public interface HomeRemote {
@RequestMapping(value = "/home")
public String home();
}
@FeignClient(name= "eureka-provider")
这个注解就是调用远程服务使用的,name属性对应spring:application:name
Controller:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConsumerController {
@Autowired
HomeRemote homeRemote;
@RequestMapping("/home")
public String home() {
return homeRemote.home();
}
}
然后依次启动注册中心,eureka-provider服务,服务调用方。
用浏览器打开http://localhost:8888
两个服务都注册成功了。
然后依次打开http://localhost:8081/home
http://localhost:8082/home
都能看到
home
在页面上。