工程Maven统一管理,搭建:
(一):服务接口【SSO-Search-Nacos-SC-alibaba-api 创建存放服务接口maven工程】
package com.nacos.springcloud.alibaba.api;
public interface NacosService {
String sayContext(String content);
/**
* 查找所有的服务名称
* @param serviceName
* @return
*/
String nameService(String serviceName) ;
}
(二)服务提供者:创建接口实现类maven工程【SSO-Search-Nacos-SC-alibaba-provider】
核心application.properties配置
server.port=8800
spring.application.name=spring-cloud-nacos-upmn-provider
dubbo.scan.base-packages=com.nacos.springcloud.alibaba.provider.service
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.registry.address=spring-cloud://localhost
spring.cloud.nacos.discovery.server-addr=192.168.227.143:8848,192.168.227.142:8848,192.168.227.141:8848
pom.xml配置如下
=================================================================
<parent>
<artifactId>SSO-Search-Nacos-SC-Alibaba</artifactId>
<groupId>com.nacos.springcloud.alibaba</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.nacos.springcloud.alibaba.provider</groupId>
<artifactId>SSO-Search-Nacos-SC-alibaba-provider</artifactId>
<name>SSO-Search-Nacos-SC-alibaba-provider</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring-cloud.version>Greenwich.SR2</spring-cloud.version>
</properties>
<dependencies>
<!-- spring cloud 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>2.1.2.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Spring Cloud与Dubbo整合依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.nacos.springcloud.alibaba.api</groupId>
<artifactId>SSO-Search-Nacos-SC-alibaba-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- spring-cloud-ablibaba整合nacos实现注册发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.1.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.26</version>
</dependency>
<!-- springboot的mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!-- MySql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
==================================================================
逻辑代码
package com.nacos.springcloud.alibaba.provider.service;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.nacos.springcloud.alibaba.api.NacosService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import java.util.List;
@Service(version = "1.0.0",
interfaceClass = NacosService.class,
cluster = "failfast",
loadbalance = "roundrobin")
public class NocasServiceImpl implements NacosService {
@Value("${spring.application.name}")
private String applicationName;
/*@NacosInjected
@Autowired
;*/
@Override
public String sayContext(String content) {
return String.format("[%s]: |-dubbo-| %s ",applicationName,content);
}
@Override
public String nameService(String serviceName) {
try {
Thread.sleep(10*1000); // 10*1000 手动触发超时,测试服务降级
} catch (InterruptedException e) {
e.printStackTrace();
}
return String.format("[%s]: |-dubbo-| %s ",serviceName,"服务名称");
}
}
启动类代码
package com.nacos.springcloud.alibaba.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 提供者
*/
@SpringBootApplication
public class NacosSpringCloudDubboProvider {
public static void main( String[] args )
{
SpringApplication.run(NacosSpringCloudDubboProvider.class,args);
}
}
(三)消费者:创建消费者实现类maven工程【SSO-Search-Nacos-SC-alibaba-consumer】
=消费者maven的pom.xml配置===
<parent>
<artifactId>SSO-Search-Nacos-SC-Alibaba</artifactId>
<groupId>com.nacos.springcloud.alibaba</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.nacos.springcloud.alibaba.consumer</groupId>
<artifactId>SSO-Search-Nacos-SC-alibaba-consumer</artifactId>
<name>SSO-Search-Nacos-SC-alibaba-consumer</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.nacos.springcloud.alibaba.api</groupId>
<artifactId>SSO-Search-Nacos-SC-alibaba-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- spring-cloud-ablibaba整合nacos实现注册发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-discovery</artifactId>
<version>2.1.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-context</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.1.2.RELEASE</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
==============================================================
application.properties配置文件如下:
server.port=8811
spring.application.name=spring-cloud-nacos-upmn-consumer
dubbo.cloud.subscribed-services=spring-cloud-nacos-upmn-provider #----为提供的的spring.application.name名称值
dubbo.scan.base-packages=com.nacos.springcloud.alibaba.consumer.service
spring.cloud.nacos.discovery.server-addr=192.168.227.143:8848,192.168.227.142:8848,192.168.227.141:8848
(1)服务为降级类:
package com.nacos.springcloud.alibaba.consumer.mock;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.nacos.springcloud.alibaba.api.NacosService;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public class NacosServiceMock implements NacosService {
@Override
public String sayContext(String content) {
return "-1";
}
@Override
public String nameService(String serviceName) {
List<Instance> resultMock=new ArrayList<Instance>();
Instance instance = new Instance();
instance.setClusterName("nacos-cluster");
instance.setHealthy(false);
instance.setInstanceId(UUID.randomUUID().toString());
instance.setServiceName(serviceName);
resultMock.add(instance);
return resultMock.toString();
}
}
(2)业务逻辑消费者:
package com.nacos.springcloud.alibaba.consumer.service;
import com.nacos.springcloud.alibaba.api.NacosService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
@Service
public class RemoteNacoseService implements NacosService{
//private static final Logger logger = LoggerFactory.getLogger(RemoteNacoseService.class); // 拦截器里面去做
/**
* 设置为mocke="true"会默认去与服务接口同目录下的服务降级类,如果该类不存在,则报日下错误
* Caused by: java.lang.IllegalStateException:
Not found class com.nacos.springcloud.alibaba.api.NacosServiceMock, cause:
com.nacos.springcloud.alibaba.api.NacosServiceMock
【该处mock直接配置服务降级类的全路径】
*/
@Reference(version = "1.0.0",
mock = "com.nacos.springcloud.alibaba.consumer.mock.NacosServiceMock",
interfaceClass = NacosService.class,
cluster = "failfast") // 快速失败,去查询集群中其他机器
private NacosService nacosService;
@Override
public String sayContext(String content) {
return nacosService.sayContext(content);
}
@Override
public String nameService(String serviceName) {
return nacosService.nameService(serviceName);
}
}
controller类
package com.nacos.springcloud.alibaba.consumer.controller;
import com.nacos.springcloud.alibaba.consumer.service.RemoteNacoseService;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class NacosRemoteController {
@Autowired
private RemoteNacoseService remoteNacoseService;
@RequestMapping("/content")
public String findContent(){
return remoteNacoseService.sayContext("begin-springcloud-dubbo-send");
}
@GetMapping("/discovery")
public String instances(@RequestParam String serviceName){
try{
return remoteNacoseService.nameService(serviceName);
}catch (RpcException e){
return "远程rpc服务调用异常";
}
}
}
启动类:
package com.nacos.springcloud.alibaba.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 消费者
*
*/
@SpringBootApplication
public class NacosConsumerApplication {
public static void main( String[] args ) {
SpringApplication.run(NacosConsumerApplication.class,args);
}
}
启动服务提供者及消费者,注册到Alibaba-Nacos注册中心:
分别访问如下地址:
访问该地址,触发服务降级,看看服务降级配置是否生效:
http://localhost:8811/discovery?serviceName=example
至此,完美SpringCloud与Dubbo及Nacos注册中心整合完成…