nacos2.0案例(2)注册服务器应用
4.0.0
com.hhd
hhd-hplat
pom
1.0-SNAPSHOT
UTF-8
UTF-8
3.8.1
1.1.1
2.3.8.RELEASE
2.3.8.RELEASE
Hoxton.SR9
2.2.5.RELEASE
2.9.2
1.9.6
192.168.34.2
1.8.18.RELEASE
1.8
192.168.33.103
hhd-hplat-provider
hhd-hplat-consumer
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
${com-alibaba-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
org.apache.maven.plugins
maven-compiler-plugin
${maven-compiler-plugin.version}
1.8
UTF-8
compile
备注:
SpringCloud 的版本选Hoxton.SR9 如果是Hoxton.SR10的话集成feign会报错。
hhd-hplat
com.hhd
1.0-SNAPSHOT
4.0.0
hhd-hplat-provider
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.nacos
nacos-client
com.alibaba.nacos
nacos-client
2.0.0
package com.hpalt.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
@Configuration
@RequestMapping(produces = "application/json; charset=utf-8")
public class EchoController {
@Value("${server.port}")
private Integer port ; // 在一台主机上启动多个服务提供者,它的端口肯定不同
/**
* reply message
* @param message
* @return
*/
@GetMapping("/echo/{message}")
public ResponseEntity echo(@PathVariable("message")String message){
return ResponseEntity.ok(String.format("hello,%s,我是%s",message,port)) ;
}
}
@SpringBootApplication
@EnableDiscoveryClient // 开启服务的发现和注册
public class EchoApp {
public static void main(String[] args) {
SpringApplication.run(EchoApp.class ,args) ;
}
}
server:
port: 8081
spring:
application:
name: nacos-provider
spring:
cloud: #nacos注册中心地址的配置法
nacos:
discovery:
server-addr: localhost:8848 # nacos 注册中心的地址
enabled: true # 关闭/启动nacos的注册中心功能
#ip: provider-xxx # 配置nacos的ip 地址
management:
endpoints:
web:
exposure:
include: "*"
http://localhost:8081/echo/zhangsan
hello,zhangsan,我是8081
表示启动成功。
nacos_2_202103201705.png
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RRn8X6Vv-1617446077060)("./nacos2/nacos_2_202103201705.png")]
表示服务注册成功
hhd-hplat
com.hhd
1.0-SNAPSHOT
4.0.0
hhd-hplat-consumer
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
com.alibaba.nacos
nacos-client
com.alibaba.nacos
nacos-client
2.0.0
org.springframework.boot
spring-boot-starter-actuator
package com.hplat.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@RestController
public class TestController {
@Autowired
private LoadBalancerClient loadBalancerClient;
@Autowired
private DiscoveryClient discoveryClient ;// 快速服务的发现
@GetMapping("/discovery/{serviceName}")
public ResponseEntity> discovery(@PathVariable("serviceName") String serviceName){
/**
* 通过服务的ID / 名称得到服务的实例列表
*/
List instances = discoveryClient.getInstances(serviceName);
if(instances==null || instances.isEmpty()){
return ResponseEntity.notFound().build() ;
}
List services = new ArrayList<>(instances.size());
instances.forEach(instance->{
services.add(instance.getHost()+":"+instance.getPort());
});
return ResponseEntity.ok(services) ;
}
//@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate() ;
}
@Autowired
private RestTemplate restTemplate ;
@GetMapping("/rpcv1/{message}")
public ResponseEntity rpcV1(@PathVariable("message") String message){
ResponseEntity responseEntity = restTemplate.getForEntity(
"http://172.26.98.96:8081/echo/{message}",
String.class,
message
);
if(responseEntity.getStatusCode()== HttpStatus.OK){
return ResponseEntity.ok(String.format("远程调用成功,结果为%s",responseEntity.getBody())) ;
}
return ResponseEntity.badRequest().body("远程调用失败") ;
}
@GetMapping("/rpcv2/{message}")
public ResponseEntity rpcV2(@PathVariable("message") String message){
List instances = discoveryClient.getInstances("nacos-provider");
if(instances==null || instances.isEmpty()){
return ResponseEntity.badRequest().body("当前服务没有服务的提供者") ;
}
ServiceInstance serviceInstance = instances.get(0);
String instance = serviceInstance.getHost()+":"+serviceInstance.getPort() ;
ResponseEntity responseEntity = restTemplate.getForEntity(
String.format("http://%s/echo/{message}",instance),
String.class,
message
);
if(responseEntity.getStatusCode()==HttpStatus.OK){
return ResponseEntity.ok(String.format("远程调用成功,结果为%s",responseEntity.getBody())) ;
}
return ResponseEntity.badRequest().body("远程调用失败") ;
}
@GetMapping("/rpcv3/{message}")
public ResponseEntity rpcV3(@PathVariable("message") String message){
List instances = discoveryClient.getInstances("nacos-provider");
if(instances==null || instances.isEmpty()){
return ResponseEntity.badRequest().body("当前服务没有服务的提供者") ;
}
ServiceInstance serviceInstance = loadbalance(instances);
System.out.println(serviceInstance);
String instance = serviceInstance.getHost()+":"+serviceInstance.getPort() ;
ResponseEntity responseEntity = restTemplate.getForEntity(
String.format("http://%s/echo/{message}",instance),
String.class,
message
);
if(responseEntity.getStatusCode()==HttpStatus.OK){
return ResponseEntity.ok(String.format("远程调用成功,结果为%s",responseEntity.getBody())) ;
}
return ResponseEntity.badRequest().body("远程调用失败") ;
}
/**
* 从一个服务的实例列表里面选择一个服务的实例
* @param instances
* 实例列表
* @return
* 具体的实例
*/
private ServiceInstance loadbalance(List instances) {
Random random = new Random(System.currentTimeMillis());
return instances.get(random.nextInt(instances.size())) ;
}
@GetMapping("/rpcv4/{message}")
public ResponseEntity rpcV4(@PathVariable("message") String message){
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
String instance = serviceInstance.getHost()+":"+serviceInstance.getPort() ;
ResponseEntity responseEntity = restTemplate.getForEntity(
//"http://nacos-provider/echo/{message}",
String.format("http://%s/echo/{message}",instance),
String.class,
message
);
if(responseEntity.getStatusCode()==HttpStatus.OK){
return ResponseEntity.ok(String.format("远程调用成功,结果为%s",responseEntity.getBody())) ;
}
return ResponseEntity.badRequest().body("远程调用失败") ;
}
}
@SpringBootApplication
@EnableDiscoveryClient // 服务的发现和注册
@RestController
public class TestApp {
public static void main(String[] args) {
SpringApplication.run(TestApp.class ,args) ;
}
}
server:
port: 8082
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery:
server-addr: localhost:8848
enabed: true
http://localhost:8082/rpcv1/zhangsan
http://localhost:8082/rpcv2/zhangsan
http://localhost:8082/rpcv3/zhangsan
http://localhost:8082/rpcv4/zhangsan