Spring cloud 的服务的通讯是基于http 的。Spring cloud有两种服务调用方式一种是ribbon+restTemplate,另一种是feign。ribbon
ribbon是一个负载均衡客户端。Feign默认集成了ribbon。本教程使用feign,代码基于第一章项目基础上讲解
然后创建一个model工程 service-producer作为服务提供者:
在pom文件中引入
<?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>com.demo</groupId>
<artifactId>service-producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-producer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.demo</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在启动类上面添加 @EnableEurekaClient开启服务注册和发现
@SpringBootApplication
@EnableEurekaClient
public class ServiceProducerApplication {
public static void main(String[] args) {
SpringApplication.run( ServiceProducerApplication.class, args );
}
配置文件如下
#端口
server:
port: 8882
#服务名称
spring:
application:
name: service-producer
#注册中心地址
eureka:
client:
serviceUrl:
defaultZone: http://dush:8881/eureka/
在创建一个对外接口便于我们待会验证
package com.demo.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/producer/")
public class ProducerController {
@Value("${server.port}")
String port;
@Value("${spring.application.name}")
String applicationName;
@GetMapping("/hello")
public String hello() {
return "hello " + applicationName + "! ,my port is:" + port;
}
}
至此,服务端服务创建完成,
下面创建一个model工程 service-producer作为服务提供者:service-consumer
pom文件中引入
<?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>com.demo</groupId>
<artifactId>service-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>service-consumer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>com.demo</groupId>
<artifactId>spring-cloud-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
在启动类上加入
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceConsumerApplication {
public static void main(String[] args) {
SpringApplication.run( ServiceConsumerApplication.class, args );
}
}
配置文件加入一下配置
server:
port: 8883
spring:
profiles:
# 本地配置文件
active: dev
application:
name: service-consumer
eureka:
client:
serviceUrl:
defaultZone: http://dush:8881/eureka/
创建一个接口,接口中调用service-producer服务
package com.demo.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
@FeignClient(value = "service-producer")
public interface ConsumerFeignService {
@GetMapping(value = "/producer/hello")
String gethello();
}
在这个ConsumerFeignService 接口中主要使用@FeignClient这个注解value 的值就是服务名称
在创建一个controller,正常注入ConsumerFeignService 注入,这里我们返回本身服务的名称和端口,以及调用service-producer的hello接口返回的数据
package com.demo.controller;
import com.demo.service.ConsumerFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/producer/")
public class ConsumerController {
@Autowired
private ConsumerFeignService consumerFeignService;
@Value("${server.port}")
String port;
@Value("${spring.application.name}")
String applicationName;
@GetMapping("getHello")
public String getHello() {
return applicationName +" "+ port + " rpc==== >" +consumerFeignService.gethello();
}
}
好了,到这里一个简单的客户端和服务端的注册发现完成,我们依次启动
server-eureka,service-producer,service-consumer
我们访问eureka首页http://localhost:8881/可以看到
服务以及启动注册成功
在访问service-producer的hello接口http://localhost:8882/producer/hello可以看到如图返回了service-producer端口号和服务名称:
这时候我们在访问service-consumer的getHello接口http://localhost:8883/producer/getHello,可以看到接口返回了service-consumer的服务名称和端口号,以及通过rpc调用service-producer的hello接口返回的信息
到此服务以及可以正常的被注册发现调用,下面展示一下Feign的负载均衡
修改service-producer的启动方式,取消single instance only的勾选,一个服务可以多次启动
修改配置文件的端口号8881改为8884,启动服务
再次访问eureka首页http://localhost:8881/可以看到service-producer有两个节点
这时候我们在访问service-consumer的getHello接口http://localhost:8883/producer/getHello
第一次
第二次
可以看到每次调用的服务都变了,实现了负载均衡的效果
微服务的功能还有很多很多,这里只是个大体的服务发现,后面还会逐渐讲解gateway网关,文件中心等模块