SpringCloud 服务通信方法

eureka 服务已经启动  http://localhost:8761/

注: 在启动文件加上 注解

@EnableEurekaServer 
application.yml 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
eureka:
  instance:
    hostname: localhost
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 相互注册实现高可用
    register-with-eureka: false  # true 将自己注册到eureka
    fetch-registry: false
  server:
    enable-self-preservation: false
spring:
  application:
    name: eureka # 服务名称,注册之后的名称
server:
  port: 8761

  

clienta 服务注册eureka

启动文件加入注解: 

@EnableEurekaClient
application.yml 配置文件 端口8000
1
2
3
4
5
6
7
8
9
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/  #,http://localhost:8762/eureka/,http://localhost:8763/eureka/
#  instance:
#    hostname: clientName
spring:
  application:
    name: clienta
1
2
3
4
5
6
7
@RestController
public class TestController {
    @GetMapping("/msg")
    public String msg() {
        return "this is clienta msg!!!";
    }
}

 访问  http://localhost:8000/msg     能返回结果

 

client 服务注册eureka 同clienta

在clien中调用clienta中方法

第一种方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@RestController
public class HelloController {
 
 
    @Autowired
    LoadBalancerClient loadBalancerClient;
 
    @GetMapping("/test")
    public String test() {
        // 获取服务实例
        ServiceInstance serviceInstance = loadBalancerClient.choose("CLIENTA");
        // 根据实例获取host port
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        // 拼接url
        String url = String.format("http://%s:%s%s", host, port, "/msg");
        RestTemplate restTemplate = new RestTemplate();
        String res = restTemplate.getForObject(url, String.class);
        return res;
    }
}

 第二种: 配置config

1
2
3
4
5
6
7
8
9
@Component
public class RestTemplateConfig {
 
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
@RestController
public class HelloController {
 
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("/test")
    public String test() {
        String res = restTemplate.getForObject("http://CLIENTA/msg", String.class);
        return res;
    }
}

 

 启动client 端口 8081

访问: http://localhost:8081/test


以上为基于RestTemplate 通信方式.

使用Feign通信

eureka clienta 项目不变化
修改 client 项目:
1. 添加依赖
1
2
3
4
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

 2. 启动类添加注解

1
@EnableFeignClients

 3. 创建一个接口 并增加注解

1
2
3
4
5
6
@FeignClient(name = "CLIENTA"// name是服务的名字
public interface ClientAClient {
 
    @GetMapping("/msg"// 基于路由匹配, 方法名可以不同
    String msg();
}

 4. 在controller中直接使用

1
2
3
4
5
6
7
8
9
@RestController
public class HelloController {
    @Autowired
    private ClientAClient clientAClient;
    @GetMapping("/test")
    public String test() {
        return clientAClient.msg();
    }
}

 

 附pom.xml文件 注意版本对应 http://spring.io/projects/spring-cloud  官网

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?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.example</groupId>
    <artifactId>client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
 
    <name>client</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR2</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-commons</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </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>

  

 

posted @   qukaige  阅读(880)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示