项目结果如下图:
SSO-Search-Dubbo-Nacos-api 为服务接口
package com.search.dubbo.springcloud.nacos.api;
/**
* Hello world!
*
*/
public interface IHelloService {
String sayHello(String name);
}
SSO-Search-Dubbo-Nacos-provider 为服务提供者
maven构建:
pom.xml配置如下
SSO-SpringCloud-Dubbo
com.search.dubbo.springcloud
1.0-SNAPSHOT
4.0.0
com.search.dubbo.springcloud.nacos.provider
SSO-Search-Dubbo-Nacos-provider
SSO-Search-Dubbo-Nacos-provider
UTF-8
1.8
1.8
com.search.dubbo.springcloud.nacos
SSO-Search-Dubbo-Nacos-api
1.0-SNAPSHOT
org.apache.dubbo
dubbo
2.7.5
com.alibaba.boot
nacos-discovery-spring-boot-starter
0.2.4
com.alibaba.spring
spring-context-support
com.alibaba.spring
spring-context-support
1.0.5
org.apache.dubbo
dubbo-spring-boot-starter
2.7.5
org.apache.dubbo
dubbo
com.alibaba
druid
1.0.26
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
org.mybatis
mybatis
3.5.2
mysql
mysql-connector-java
5.1.46
runtime
org.apache.maven.plugins
maven-surefire-plugin
true
true
org.springframework.boot
spring-boot-maven-plugin
true
=============================application.properties配置文件============================
# server.context-path=/sso
# server.servlet.context-path=/sso
server.error.path=/error
server.port=8822
# session \u8FC7\u671F\u65F6\u95F4\uFF0C\u5355\u4F4D\uFF1A\u79D2
server.servlet.session.timeout=6000
server.tomcat.max-threads=800
server.tomcat.uri-encoding=UTF-8
logging.path=../logs
logging.file=nacos-provider.log
logging.level.root=info
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.basename=messages
sprint.thymeleaf.content-type=text/html
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
#spring.redis.cluster.nodes=192.168.227.128:6379,192.168.227.128:6380,192.168.227.129:6381,192.168.227.129:6382,192.168.227.130:6383,192.168.227.130:6384
## \u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09
#spring.redis.cluster.timeout=2000
#spring.redis.cluster.max-redirects=100
#spring.redis.cluster.maxIdle=200
#spring.redis.cluster.maxTotal=1000
#spring.redis.cluster.maxWaitMillis=2000
#spring.session.store-type = redis
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/security?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,logback
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#mybatis.mapperLocations=classpath:mapper/*.xml
#mybatis.typeAliasesPackage=org.jy.sso.cas.data.server.entity
#mybatis.configuration.mapUnderscoreToCamelCase=true
dubbo.application.name=spring-boot-dubbo-nacos-provider
dubbo.registry.address=nacos://192.168.227.143:8848,192.168.227.142:8848,192.168.227.141:8848
dubbo.protocol.name=dubbo
dubbo.registry.protocol=20880
=====================================服务实现==================================
package com.search.dubbo.springcloud.nacos.provider.service;
import com.search.dubbo.springcloud.nacos.api.IHelloService;
import org.apache.dubbo.config.annotation.Service;
@Service(version = "1.0.0",group = "HelloGroup",interfaceClass = IHelloService.class)
public class HelloServiceImpl implements IHelloService {
@Override
public String sayHello(String name) {
return "Hello World: "+name;
}
}
=======================================================================
服务启动
package com.search.dubbo.springcloud.nacos.provider;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@DubboComponentScan
public class NacosProviderApplication {
public static void main(String[] args){
SpringApplication.run(NacosProviderApplication.class,args);
}
}
启动服务提供者:查看是否nacos注册中心是否有该服务
服务消费者:
SSO-Search-Dubbo-Nacos-consumer
=============================pom.xml配置==================================
SSO-SpringCloud-Dubbo
com.search.dubbo.springcloud
1.0-SNAPSHOT
4.0.0
com.search.dubbo.springcloud.nacos.consumer
SSO-Search-Dubbo-Nacos-consumer
SSO-Search-Dubbo-Nacos-consumer
UTF-8
1.8
1.8
com.search.dubbo.springcloud.nacos
SSO-Search-Dubbo-Nacos-api
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter
2.2.1.RELEASE
org.springframework.boot
spring-boot-starter-web
2.1.3.RELEASE
org.apache.dubbo
dubbo
2.7.5
com.alibaba.boot
nacos-discovery-spring-boot-starter
0.2.4
com.alibaba.spring
spring-context-support
com.alibaba.spring
spring-context-support
1.0.5
org.apache.dubbo
dubbo-spring-boot-starter
2.7.5
org.apache.dubbo
dubbo
com.alibaba
druid
1.0.26
mysql
mysql-connector-java
5.1.46
runtime
org.apache.maven.plugins
maven-surefire-plugin
true
true
org.springframework.boot
spring-boot-maven-plugin
true
=====================application.properties===============================
# server.context-path=/sso
# server.servlet.context-path=/sso
server.error.path=/error
server.port=8811
# session \u8FC7\u671F\u65F6\u95F4\uFF0C\u5355\u4F4D\uFF1A\u79D2
server.servlet.session.timeout=6000
server.tomcat.max-threads=800
server.tomcat.uri-encoding=UTF-8
logging.path=../logs
logging.file=nacos-provider.log
logging.level.root=info
spring.thymeleaf.cache=false
spring.thymeleaf.mode=HTML
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.basename=messages
sprint.thymeleaf.content-type=text/html
spring.thymeleaf.check-template=true
spring.thymeleaf.check-template-location=true
#spring.redis.cluster.nodes=192.168.227.128:6379,192.168.227.128:6380,192.168.227.129:6381,192.168.227.129:6382,192.168.227.130:6383,192.168.227.130:6384
## \u8FDE\u63A5\u6C60\u6700\u5927\u8FDE\u63A5\u6570\uFF08\u4F7F\u7528\u8D1F\u503C\u8868\u793A\u6CA1\u6709\u9650\u5236\uFF09
#spring.redis.cluster.timeout=2000
#spring.redis.cluster.max-redirects=100
#spring.redis.cluster.maxIdle=200
#spring.redis.cluster.maxTotal=1000
#spring.redis.cluster.maxWaitMillis=2000
#spring.session.store-type = redis
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/security?characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.initialSize=5
spring.datasource.minIdle=5
spring.datasource.maxActive=20
spring.datasource.maxWait=60000
spring.datasource.timeBetweenEvictionRunsMillis=60000
spring.datasource.minEvictableIdleTimeMillis=300000
spring.datasource.validationQuery=SELECT 1 FROM DUAL
spring.datasource.testWhileIdle=true
spring.datasource.testOnBorrow=false
spring.datasource.testOnReturn=false
spring.datasource.poolPreparedStatements=true
spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
spring.datasource.filters=stat,wall,logback
spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
#mybatis.mapperLocations=classpath:mapper/*.xml
#mybatis.typeAliasesPackage=org.jy.sso.cas.data.server.entity
#mybatis.configuration.mapUnderscoreToCamelCase=true
dubbo.application.name=spring-boot-dubbo-nacos-consumer
dubbo.registry.address=nacos://192.168.227.143:8848,192.168.227.142:8848,192.168.227.141:8848
dubbo.protocol.name=dubbo
dubbo.registry.protocol=20880
=====================消费者启动类==============================
package com.search.dubbo.springcloud.nacos.consumer;
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 服务消费者
*
*/
@SpringBootApplication
@DubboComponentScan // dubbo相关注解扫描
public class NacosConsumerApplication {
public static void main( String[] args )
{
SpringApplication.run(NacosConsumerApplication.class,args);
}
}
====================controller===================
package com.search.dubbo.springcloud.nacos.consumer.controller;
import com.search.dubbo.springcloud.nacos.consumer.service.RemoteIHelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IHelloController {
@Autowired
private RemoteIHelloService remoteIHelloService;
@RequestMapping("/greet")
public String sayHello(){
return remoteIHelloService.sayHello("Dubbo");
}
}
====================================接口实现类================================
package com.search.dubbo.springcloud.nacos.consumer.service;
import com.search.dubbo.springcloud.nacos.api.IHelloService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;
@Service // 这个为spring的注解
public class RemoteIHelloService implements IHelloService{
@Reference(version = "1.0.0",group = "HelloGroup",interfaceClass = IHelloService.class)
private IHelloService iHelloService;
@Override
public String sayHello(String name) {
return iHelloService.sayHello(name);
}
}
启动消费者服务,查看是否已经注册到服务中
http://localhost:8811/greet 访问服务:
使用nacos作为dubbo服务注册中心整合成功!!!!!!!!