spring ribbon | 张洪铭的个人博客
张洪铭的个人博客

spring ribbon

创建子项目:user-service
创建application.properties

1
2
3
4
5
6
7
server.port=8084
spring.application.name=user-service
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
# 自定义实例ID格式
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

UserApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package spring.cloud.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by zhanghongming on 2020/6/27.
*/
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}

创建User

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package spring.cloud.ribbon.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* Created by zhanghongming on 2020/6/27.
*/
@AllArgsConstructor
@Data
public class User {
public User(){}
private int id;
private String name;
}

创建UserController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package spring.cloud.ribbon.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import spring.cloud.ribbon.dto.User;
/**
* Created by zhanghongming on 2020/6/27.
*/
@RestController
public class UserController {
@GetMapping("/user/get")
public User getUser(int id) {
return new User(id, "zhm");
}
}

启动

此处输入图片的描述

创建子项目:ribbon
创建application.properties

1
2
3
4
5
6
7
server.port=8089
spring.application.name=ribbon
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
# 自定义实例ID格式
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

创建RibbonApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package spring.cloud.ribbon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by zhanghongming on 2020/6/27.
*/
@SpringBootApplication
public class RibbonApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonApplication.class, args);
}
}

创建RibbonController

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
package spring.cloud.ribbon.controller;
/**
* Created by zhanghongming on 2020/6/27.
*/
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 spring.cloud.ribbon.dto.User;
@RestController
public class RibbonController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/get")
public Object get() {
User user = restTemplate.getForEntity("http://user-service/user/get?id=1", User.class).getBody();
return user;
}
}

创建RestConfiguration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package spring.cloud.ribbon.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* Created by zhanghongming on 2020/6/27.
*/
@Configuration
public class RestConfiguration {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

启动

调用接口http://localhost:8089/get/,返回
{“id”:1,”name”:”zhm”}

坚持原创技术分享,您的支持将鼓励我继续创作!