eureka服务调用
eurekaopenfeign 服务调用
·
eureka服务调用,接上文的服务注册(使用OpenFeign进行服务调用),接我的一篇eureka注册文章继续创作。链接:https://blog.csdn.net/weixin_48421942/article/details/123163198
-
新建消费者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";
}
}
- 添加好需要调用的服务后,新建消费者80模块,我结构和我上图一样,上图是建好的项目结构。80模块对应的pom文件代码,yml配置代码如下:
<?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();
}
}
最后实现结果:
总结
- 服务提供者8001,新建restful api接口提供服务,注册到eureka中心
- 服务消费者80,导入openfeign依赖,启动类加上@EnableDiscoveryClient
@EnableFeignClients注解,标记这是一个服务消费者模块 - 建立service层利用接口和注解@FeignClient(value = “PROVIDENCE-8001”)这里的value中的值,就说服务提供者8001的spring application name 上篇文章编写的,这里的意思是调用这个PROVIDENCE-8001中的接口,通过restful api接口地址进行调用
- 最后建一个controller进行调用就行了
更多推荐
已为社区贡献1条内容
所有评论(0)