新建模块Producer,pom.xml如下:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.example</groupId>
<artifactId>SpringCloudAlibabaDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>Producer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Producer</name>
<description>Producer</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
配置application.properties:
server.port=7000
spring.application.name=producer
spring.cloud.nacos.discovery.server-addr=localhost:8848
新建Controller:
@RestController
public class HelloProducer {
@Value("${server.port}")
private int port;
@RequestMapping("/hello")
public String hello() {
return "hello,Producer,port:" + port;
}
}
在SpringCloudAlibabaDemo的pom.xml加入
<modules>
<module>Consumer</module>
<module>Producer</module>
</modules>
在Consumer加入:
@Configuration
public class MyConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
修改HelloConsumer:
@RestController
@RequestMapping("/consumer")
public class HelloConsumer {
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@Value("${server.port}")
private int port;
@RequestMapping("/hello")
public String hello() {
return "hello,Consumer,port:" + port;
}
@RequestMapping("/rpc")
public String rpc() {
List<ServiceInstance> producer = discoveryClient.getInstances("producer");
ServiceInstance serviceInstance = null;
if (producer != null && !producer.isEmpty()) {
serviceInstance = producer.get(0);
}
if (serviceInstance != null) {
return restTemplate.getForObject("http://"+serviceInstance.getHost()+":"+serviceInstance.getPort()+"/hello",String.class);
} else {
throw new RuntimeException("未找到Producer服务");
}
}
}
访问http://localhost:7001/consumer/rpc,看到hello,Producer,port:7000。在这里是通过DiscoveryClient解析出服务的ip和端口后通过RestTemplate调用。
在2021.0.4.0版本的springCloudAlibaba中,spring-cloud-starter-alibaba-nacos-discovery
依赖引入了spring-cloud-loadbalancer
作为负载均衡,RestTemplate要使用负载均衡,只需添加@LoadBalanced:
@Configuration
public class MyConfig {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
同时添加依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
<version>3.1.4</version>
</dependency>
修改HelloConsumer:
@RequestMapping("/rpc1")
public String rpc1() {
return restTemplate.getForObject("http://producer/hello",String.class);
}
访问http://localhost:7001/consumer/rpc1,看到hello,Producer,port:7000。
在启动一个端口是7002的Producer,访问http://localhost:7001/consumer/rpc1,看到负载均衡成功。
标签:producer,Producer,SpringCloud,hello,RestTemplate,port,Alibaba,public From: https://www.cnblogs.com/shigongp/p/17399175.html