赞
踩
新建消费者model,开启eureka服务注册中心,将对应的服务注册进去,然后消费者model调用服务中心注册的服务提供者的服务
我上文中的服务提供者,只是注册了,并没有对应的服务提供,现在下面得加入对应的服务。
openfeign是springcloud在feign的基础上支持springmvc的注解,如@RequestMapping等等。openfeign的@feignclient可以解析springmvc的@requestMapping注解下的接口,并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。
在eureka8001模块加入controller包,加入控制层代码:
项目结构如下:
controller代码如下:
package com.cloud.eureka8001.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class TestController { @RequestMapping(value = "/get",method = {RequestMethod.GET}) public String hello(){ return "hello eureka"; } }
<?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>cloud-zjie</artifactId> <groupId>com.cloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka80-consumer</artifactId> <dependencies> <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> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
server:
port: 80
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka/
spring:
application:
name: Ribbion-consumer80
启动类代码(重要)
启动类要加新注解和服务注册,服务提供不一样,注解:@EnableDiscoveryClient 是开启服务发现客户端,顾名思义就是服务调用
@EnableFeignClients 启用openfeign服务调用功能
代码如下:
package com.cloud.eureka80; import com.cloud.eureka80.rules.MySelfRules; 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.ribbon.RibbonClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients public class Eurekaconsumer80 { public static void main(String[] args) { SpringApplication.run(Eurekaconsumer80.class,args); } }
创建server层用接口,进行restful api调用,代码如下:
package com.cloud.eureka80.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
@Component
@FeignClient(value = "PROVIDENCE-8001")
public interface TestService {
@GetMapping("/get")
public String hello();
}
最后新建controller层对这个接口进行调用,代码如下:
package com.cloud.eureka80.controller; import com.cloud.eureka80.service.TestService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import javax.annotation.Resource; @RestController @Slf4j public class TestController { @Autowired private TestService testService; @GetMapping("/consumer/get") public String get(){ log.info("用户正在使用消费者端口"); return testService.hello(); } }
最后实现结果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。