继续使用之前我们搭建好的注册中心eureka-server:Spring Cloud (一)、搭建服务注册中心
这里我们需要构建一个服务提供者(hello-service)和一个服务消费者(fegin-consumer):
一、构建服务提供者
1、创建一个Spring Boot工程,取名为hello-service。
2、编写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.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>hello-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>hello-service</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>
3、在应用主类上添加@EnableDiscoveryClient注解,开启服务发现的功能:
@EnableDiscoveryClient
@SpringBootApplication
public class HelloServiceApplication {
public static void main(String[] args) {
SpringApplication.run(HelloServiceApplication.class, args);
}
}
4、编写application.properties配置文件:
#指定服务名称 spring.application.name=hello-service #指定注册中心地址 eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
5、编写一个HelloController类,提供一个/hello接口供服务消费者进行调用消费
@RestController
public class HelloController {
private Logger logger = Logger.getLogger(String.valueOf(HelloController.class));
@RequestMapping("/hello")
public String hello(){
logger.info("=========调用了/hello接口=========");
return "hello world!";
}
}
二、构建服务消费者
1、创建一个Spring Boot工程,取名为fegin-consume:
2、编写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.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>fegin-consume</artifactId> <version>0.0.1-SNAPSHOT</version> <name>fegin-consume</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.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</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>
3、在应用主类上添加@EnableFeginClients注解开启Spring Cloud Fegin的支持功能;添加@EnableDiscoveryClient开启服务发现的功能:
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class FeginConsumeApplication {
public static void main(String[] args) {
SpringApplication.run(FeginConsumeApplication.class, args);
}
}
4、编写application.properties配置文件:
#指定服务名称 spring.application.name=feign-consumer #端口号 server.port=9001 #指定服务注册中心地址,多个注册中心以,隔开 eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
5、定义一个HelloService接口,通过@FeginClient注解指定服务名称来绑定服务,使用Spring MVC的注解(@RequestMapping)来绑定具体该服务提供的REST接口。
@FeignClient("hello-service")
@Service
public interface HelloService {
@RequestMapping("/hello")
String hello();
}
6、创建一个ConsumerController来实现会Fegin客户端(HelloService接口)的调用:
@RestController
public class ConsumerController {
private Logger logger = Logger.getLogger(String.valueOf(ConsumerController.class));
@Autowired
HelloService helloService;
@RequestMapping("/feign-consumer")
public String helloConsumer(){
logger.info("=========helloConsumer=========");
return helloService.hello();
}
}
三、测试验证
分别启动服务注册中心、两个hello-service(8081和8082)和fegin-consume,此时Eureka信息面板中可以看到:
发送几次请求到http://localhost:9001/feign-consumer,可以看到返回了hello world!,我们再来看hello-service的两个控制台发现交替输出了=========调用了/hello接口=========信息,表明通过轮询的方式实现了客户端负载均衡。