Spring Cloud快速使用教程(一)

在研究spring cloud,下面是快速搭建方法

我使用的是IDEA 中文汉化版,大家可自行汉化
1、先创建一个工程

 

2、拉下来创建注册组件

同样的选择springboot工程

为eureka注册中心添加注解开启服务

 

配置eureka注册中心配置文件 application.yml (注意缩进)

server:
# 配置服务端口
port: 8081
eureka:
client:
service-url:
# 配置eureka服务器地址
defaultZone: http://127.0.0.1:${server.port}/eureka
#是否需要将自己注册到注册中心(注册中心集群需要设置为true)
register-with-eureka: false
#是否需要搜索服务信息 因为自己是注册中心所以为false
fetch-registry: false

运行ServiceEurekaApplication文件启动项目, 访问注册中心

http://localhost:8081

 

 3、创建二个微服务 serviceA,serviceB

创建springboot模块

配置微服务的入口文件 @EnableEurekaClient   (service-a和service-b的都要加)

 

 配置application.yml
service-a:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server:
  # 服务端口号
  port: 8082
spring:
  application:
    # 服务名称 - 服务之间使用名称进行通讯
    name: service-objcat-a
eureka:
  client:
    service-url:
      # 填写注册中心服务器地址
      defaultZone: http://localhost:8081/eureka
    # 是否需要将自己注册到注册中心
    register-with-eureka: true
    # 是否需要搜索服务信息
    fetch-registry: true
  instance:
    # 使用ip地址注册到注册中心
    prefer-ip-address: true
    # 注册中心列表中显示的状态参数
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

service-b:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server:
  # 服务端口号
  port: 8083
spring:
  application:
    # 服务名称 - 服务之间使用名称进行通讯
    name: service-objcat-b
eureka:
  client:
    service-url:
      # 填写注册中心服务器地址
      defaultZone: http://localhost:8081/eureka
    # 是否需要将自己注册到注册中心
    register-with-eureka: true
    # 是否需要搜索服务信息
    fetch-registry: true
  instance:
    # 使用ip地址注册到注册中心
    prefer-ip-address: true
    # 注册中心列表中显示的状态参数
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

开启多个微服务。单个一个个开就很麻烦,我们使用RunDashboard就会方便很多

在工程目录下找.idea文件夹下的workspace.xml,在其中增加如下组件

1
2
3
4
5
6
7
<component name="RunDashboard">
  <option name="configurationTypes">
    <set>
      <option value="SpringBootApplicationConfigurationType" />
    </set>
  </option>
</component>

分别运行注册中心及微服务模块

出现端口号表示启动成功

  编写测试接口

1
2
3
4
5
6
7
8
9
10
11
12
package com.example.servicea.controller;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
public class IndexA {
    @RequestMapping("testA")
    public String test(){
        return "Hello A";
    }
}

重启服务

 访问下面地址即可访问到

http://localhost:8082/testA

Hello A

使用微服务b调用服务a的接口
这时我们就需要用到eurka(注册中心)feign客户端了
首先我们在service-b中创建interface

在微服务b中, 创建一个ServiceAFeignClient接口:

其中应用名可以在eureka中找到
http://localhost:8081

ServiceAFeignClient:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.seaverb.controller;
 
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
 
// 填入注册中心中的应用名, 也就是要调用的微服务的应用名
// 在eureka页面中可以找到
@FeignClient("SERVICE-OBJCAT-A")
public interface ServiceAFeignClient {<br>   //路由名"testA"对应控制器IndexA的方法的路由名   例:此外对应是的IndexA  @requestMapping("testA")
    @RequestMapping("testA")
    public String IndexA();
     
    @RequestMapping("testA2")
    public String IndexA2(@RequestParam("id") String id);
 
    @PostMapping("testA3")
    public String IndexA3(@RequestParam("title") String title,@RequestParam("id") Integer id);
 
}

在Index.java 我增加了一个GET方式及POST方式接收微服务器之间传参方法可供参考

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
package com.example.seavera.controller;
 
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.*;
 
@RestController
// 添加注解声明是注册中心客户端
@EnableEurekaClient
// 实现不同子服务调用
@EnableFeignClients
public class IndexA {
    @RequestMapping("testA")
    public String test(){
        return "Hello A";
    }
 
    /**
     * 微服务GET接收传参实例
     * @param id
     * @return
     */
    @RequestMapping("testA2")
    public String test2(@RequestParam("id") String id){
        return "Hello A2 "+ id;
    }
    /**
     * 微服务POST接收传参实例
     * @param title
     * @param id
     * @return
     */
    @PostMapping(value = "testA3")
    public String test3(@RequestParam("title") String title,@RequestParam("id") Integer id){
        return "Hello A3 "+ title;
    }
}

在服务b中添加控制器

 

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
package com.example.seaverb.controller;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.*;
 
@RestController
// 添加注解声明是注册中心客户端
@EnableEurekaClient
// 实现不同子服务调用
@EnableFeignClients
public class IndexB {
    @Autowired
    private ServiceAFeignClient serviceAFeignClient;
 
    @RequestMapping("call")
    public String call(){
        String test = "This is B";
        String result = serviceAFeignClient.IndexA();
        return "b to a 访问结果 ---" + result;
    }
 
    /**
     * 微服务之间GET传参数 http://localhost:8083/call2?id=11
     * 双方 @RequestParam("id") 必须指定相同的value值。只要不对应都会报错。
     * @param id
     * @return
     */
    @RequestMapping("call2")
    public String call2(@RequestParam("id") String id){
        String test = "This is B";
        String result = serviceAFeignClient.IndexA2(id);
        return "b to a 访问结果 ---" + result;
    }
 
    /**
     * 微服务之间POST传参数
     * @param title
     * @param id
     * @return
     */
    @PostMapping("call3")
    public String call3(@RequestParam("title") String title,@RequestParam("id") Integer id){
        String result = serviceAFeignClient.IndexA3(title,id);
        return "b to a 访问结果 ---" + result;
    }
}

解决@Autowired实例报错

重新运行服务b 在浏览器上访问试试吧

http://localhost:8083/call

可以看到             b to a 访问结果 ---Hello A


使用GET传参访问
http://localhost:8083/call2?id=888

可以看到             b to a 访问结果 ---Hello A2 888

使用POST传参

1
2
3
PS: 在springcloud中一个子服务调用另一个子服务默认超时时间是1s, 也就是说要是被调用的子服务返回超过一秒就会出现错误, 针对此问题需要修改调用服务的yml文件.
 
举例: 在本案例中, service-a是被调用者, service-b是调用者, 则在service-b的yml文件中加入
1
2
3
4
5
ribbon:
  #建立连接超时时间
  ReadTimeout: 5000
  #读取资源超时间
  ConnectTimeout: 5000

service-b完整配置如下:

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
server:
  # 服务端口号
  port: 8083
spring:
  application:
    # 服务名称 - 服务之间使用名称进行通讯
    name: service-objcat-b
eureka:
  client:
    service-url:
      # 填写注册中心服务器地址
      defaultZone: http://localhost:8081/eureka
    # 是否需要将自己注册到注册中心
    register-with-eureka: true
    # 是否需要搜索服务信息
    fetch-registry: true
  instance:
    # 使用ip地址注册到注册中心
    prefer-ip-address: true
    # 注册中心列表中显示的状态参数
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
ribbon:
  #建立连接超时时间
  ReadTimeout: 5000
  #读取资源超时间
  ConnectTimeout: 5000

 

spring cloud(二)简单快速的实现负载均衡的功能 

posted @   智昕  阅读(1493)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示