(尊重版权,内容学习来源于蚂蚁课堂)
本文开始记录SpringCloudAlibaba,使用SpringBoot2.0+,注册中心使用nacos(windows版本)
官网 :https://nacos.io/zh-cn/docs/what-is-nacos.html
工程初步结构:
本文使用到的Module: nacos-users(服务消费者) nacos-service-server-data(服务生产者)
1.创建工程
根pom.xml
4.0.0
springboot-nacos
springboot-nacos
pom
1.0-SNAPSHOT
nacos-service
nacos-app
nacos-users
nacos-auth
nacos-common
nacos-gateway
org.springframework.boot
spring-boot-starter-parent
2.1.1.RELEASE
2.1.1.RELEASE
2.1.1
Greenwich.RELEASE
1.2.47
1.16.22
1.8
true
com.alibaba
fastjson
${fast.json.version}
org.projectlombok
lombok
${lombok.version}
true
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
com.netflix.archaius
archaius-core
0.7.6
com.google.guava
guava
2.创建空module nacos-service nacos-service-api
创建api模块 nacos-service-api-data
nacos-service-api pom配置:
nacos-service
springboot-nacos
1.0-SNAPSHOT
4.0.0
nacos-service-api
pom
nacos-service-api-data
nacos-service-api-rbac
org.springframework.cloud
spring-cloud-starter-openfeign
2.1.1.RELEASE
3.nacos-service-api-data 编写简单的接口
package com.google.api;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "nacos-service-server-data")
public interface HelloWorldService {
@GetMapping("/helloWorld")
String helloWorld(@RequestParam("age") Integer age);
}
4.创建空module nacos-service-server
pom配置文件
nacos-service
springboot-nacos
1.0-SNAPSHOT
4.0.0
nacos-service-server
pom
nacos-service-server-data
org.springframework.cloud
spring-cloud-starter-alibaba-nacos-discovery
0.9.0.RELEASE
true
org.springframework.boot
spring-boot-starter-web
5.创建服务工程module nacos-service-server-data
pom配置文件
nacos-service-server
springboot-nacos
1.0-SNAPSHOT
4.0.0
nacos-service-server-data
springboot-nacos
nacos-service-api-data
1.0-SNAPSHOT
编写接口实现类:
package com.google.data.impl;
import com.google.api.HelloWorldService;
import org.springframework.web.bind.annotation.RestController;
/**
* @author wk
* @Description:
* @date 2020/3/3 13:12
**/
@RestController
public class HelloWorldServiceImpl implements HelloWorldService {
@Override
public String helloWorld(Integer age) {
return "hello world!" + " " + age;
}
}
服务启动类:
package com.google;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @author wk
* @Description:
* @date 2020/3/2 16:06
**/
@SpringBootApplication
public class DataServiceApplication {
public static void main(String[] args) {
SpringApplication.run(DataServiceApplication.class, args);
}
}
yml配置文件:
spring:
cloud:
nacos:
discovery:
###服务注册地址
server-addr: 127.0.0.1:8848
application:
name: nacos-service-server-data
server:
port: 8081
logging:
path: F:/idea_workspace/springboot-nacos/logs/${spring.application.name}
6.创建服务调用方-测试服务nacos-users
pom配置
springboot-nacos
springboot-nacos
1.0-SNAPSHOT
4.0.0
nacos-users
org.springframework.cloud
spring-cloud-starter-alibaba-nacos-discovery
0.9.0.RELEASE
true
org.springframework.boot
spring-boot-starter-web
springboot-nacos
nacos-service-api-data
1.0-SNAPSHOT
compile
org.springframework.cloud
spring-cloud-starter-openfeign
2.1.1.RELEASE
yml配置:
spring:
cloud:
nacos:
discovery:
###服务注册地址
server-addr: 127.0.0.1:8848
application:
name: nacos-users
server:
port: 8091
logging:
path: F:/idea_workspace/springboot-nacos/logs/${spring.application.name}
启动类:
package com.google;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author wk
* @Description:
* @date 2020/3/2 20:49
**/
@SpringBootApplication
@EnableFeignClients
public class NacosCustUacApplication {
public static void main(String[] args) {
SpringApplication.run(NacosCustUacApplication.class, args);
}
}
调用feign服务类:
package com.google.service;
import com.google.api.HelloWorldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author wk
* @Description:
* @date 2020/3/3 13:21
**/
@RestController
public class HelloService {
@Autowired
private HelloWorldService helloWorldService;
/**
* 调用feign服务
*
* @return
*/
@GetMapping("/hello")
public String hello() {
String s = helloWorldService.helloWorld(99);
return "feignclient调用 " + s;
}
}
7.测试运行:
启动nacos,启动服务。
启动成功后,可在http://localhost:8848/nacos/#/serviceManagement?dataId=&group=&appName=&namespace=
页面看到两个服务
调用 /helloWorld
踩坑记录:
1.java.lang.ClassNotFoundException: com.netflix.config.CachedDynamicIntProperty
调用feign服务报错,Springboot2.1.1版本
参考解决方法https://blog.csdn.net/shenhonglei1234/article/details/101213519
2.nacos com.netflix.client.ClientException: Load balancer does not have available server for client: nacos-service-server-data访问报错@FeignClient("nacos-service-server-data")这样写就报上面那个错了@FeignClient(value = "nacos-service-server-data")这样写就没问题。。。参考:https://blog.csdn.net/weixin_34259232/article/details/92390854