Nacos 提供了一组简单易用的特性集,可快速实现动态服务发现、服务配置、服务元数据及流量管理。
更敏捷和容易地构建、交付和管理微服务平台。
关键特性:
服务发现和服务健康监测
动态配置服务
动态 DNS 服务
服务及其元数据管理
1.启动服务
下载 地址
解压
双击 startup.cmd
2.服务端provider
(1)添加依赖
(2)添加配置
server.port=8010
spring.application.name=service-provider
spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.service=${spring.application.name}
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
(3)测试方法
package com.xyz.provider1.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class demoController {
@RequestMapping("/hello")
public String Hello(){
return "hello, provider";
}
}
再添加provider1,端口8011,测试方法输出
"hello,another provider"
3.客户端customer
(1)添加依赖
(2)添加配置
server.port=8015
spring.application.name=service-comsumer
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.service=${spring.application.name}
#service-provider.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.file-extension=properties
(3)测试方法
package com.xyz.comsumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RefreshScope
@RestController
public class HelloController {
@Autowired
private LoadBalancerClient loadBalancer;
@Autowired
private DiscoveryClient discoveryClient;
private String serviceName = "service-provider";
/**
* 获取所有服务
*/
@RequestMapping("/services")
public Object services() {
return discoveryClient.getInstances(serviceName);
}
/**
* 从所有服务中选择一个服务(轮询)
*/
@RequestMapping("/discover")
public Object discover() {
return loadBalancer.choose(serviceName).getUri().toString();
}
@RequestMapping("/hello")
public String hello() {
ServiceInstance serviceInstance = loadBalancer.choose(serviceName);
String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class);
callServiceResult += ",throw service";
return callServiceResult;
}
}
4.网关Zuul
(1)添加依赖
(2)添加配置
server.port=8090
spring.application.name=service-zuul
spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.service=${spring.application.name}
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
zuul.routes.api.path=/api/**
zuul.routes.api.serviceId=service-provider
(3)启动类
package com.xyz.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@SpringBootApplication
public class ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
}
}
4.测试
启动provider,provider1,customer
http://localhost:8848/nacos/ ,用户名nacos,密码nacos
http://localhost:8090/api/hello
交替返回
hello,provider或hello,another provider
5.修改客户端,添加统一配置
(1)修改添加依赖
(2)修改配置 application.properties
server.port=8015
spring.application.name=service-comsumer
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.cloud.nacos.discovery.enabled=true
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
spring.cloud.nacos.discovery.service=${spring.application.name}
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
spring.cloud.nacos.config.file-extension=properties
(3)测试方法
添加注解@RefreshScope,开启配置的动态更新
package com.xyz.comsumer.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RefreshScope
@RestController
public class HelloController {
@Value("${hello-string}")
private String helloString;
@RequestMapping("/configTest")
public String configTest() {
return helloString;
}
}
登录http://localhost:8848/nacos
添加配置service-comsumer.properties
hello-string=customer
重新启动customer
http://localhost:8015/configTest
输出
customer
修改配置发布后,再次请求,会输出新设定的值
注:
使用nacos服务配置,如果resource下,带有bootstrap.properties,且配置写入application.properties,这时会报错
***************************APPLICATION FAILED TO START***************************
Description:
Application failed to connect to Nacos server: ""
Action:
Please check your Nacos server config
解决方法:将bootstrap.properties删掉,或将配置写入bootstrap.properties
原因:可以查看下他们的区别 查看
参考阅读
发表评论