本文共 3740 字,大约阅读时间需要 12 分钟。
服务提供方 (Service Provider)
是指提供可复用和可调用服务的应用方com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery
server: port: 9001spring: application: name: nacos-payment-provider cloud: nacos: discovery: server-addr: 172.28.129.83:8848 #配置Nacos地址management: endpoints: web: exposure: include: '*'
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClientpublic class PaymentApplication9001 { public static void main(String[] args) { SpringApplication.run(PaymentApplication9001.class,args); }}
import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class PaymentController{ @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/nacos/{id}") public String getPayment(@PathVariable("id") Integer id) { return "nacos registry, serverPort: "+ serverPort+"\t id"+id; }}
6.测试
服务消费方 (Service Consumer)
是指会发起对某个服务调用的应用方server: port: 83spring: application: name: nacos-order-consumer cloud: nacos: discovery: server-addr: 172.28.129.83:8848#消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)service-url: nacos-user-service: http://nacos-payment-provider
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@SpringBootApplication@EnableDiscoveryClientpublic class OrderNacosApplication83 { public static void main(String[] args) { SpringApplication.run(OrderNacosApplication83.class,args); }}
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;@Configurationpublic class ApplicationContextConfig { @Bean @LoadBalanced //负载均衡 public RestTemplate restTemplate(){ return new RestTemplate(); }}
新建controller包,新建类OrderNacosController
import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Value;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 javax.annotation.Resource;@RestController@Slf4jpublic class OrderNacosController{ @Resource private RestTemplate restTemplate; //从配置文件中读取出来 @Value("${service-url.nacos-user-service}") private String serverURL; @GetMapping(value = "/consumer/payment/nacos/{id}") public String paymentInfo(@PathVariable("id") Long id) { return restTemplate.getForObject(serverURL+"/payment/nacos/"+id,String.class); }}
为什么Nacos支持负载均衡,因为他整合了ribbon
转载地址:http://jhfk.baihongyu.com/