当前位置: 代码迷 >> 综合 >> springboot2.4.5 springcloud alibaba dubbo nacos集成
  详细解决方案

springboot2.4.5 springcloud alibaba dubbo nacos集成

热度:118   发布时间:2023-10-17 16:14:28.0

遇到的坑

不支持jdk11,仅jdk8

注册中心配置

 

 

项目结构如下

 

springboot2.4.5 springcloud alibaba dubbo nacos集成

api代码

package com.example.demo.api;public interface HelloService {String sayHello(String name);
}
plugins {id 'java'
}repositories {//1.优先查找本地maven库,性能最好mavenLocal()//2.其次查找aliyun maven库maven {url 'https://maven.aliyun.com/repository/public/'}maven {url 'https://maven.aliyun.com/repository/spring/'}mavenLocal() //1.优先查找本地maven库,性能最好maven {//2.其次查找aliyun maven库url 'http://maven.aliyun.com/nexus/content/groups/public/'}mavenCentral()maven { url 'https://repo.spring.io/milestone' }maven { url 'https://repo.spring.io/snapshot' }
}group = 'com.chenye'
version = '1.0.0'
//sourceCompatibility = '11'
/** This file was generated by the Gradle 'init' task.*/rootProject.name = 'api'

 

provider代码

plugins {id 'org.springframework.boot' version '2.4.5'id 'io.spring.dependency-management' version '1.0.11.RELEASE'id 'java'
}group = 'com.example'
version = '0.0.1-SNAPSHOT'
//sourceCompatibility = '11'configurations {developmentOnlyruntimeClasspath {extendsFrom developmentOnly}compileOnly {extendsFrom annotationProcessor}
}repositories {//1.优先查找本地maven库,性能最好mavenLocal()//2.其次查找aliyun maven库maven {url 'https://maven.aliyun.com/repository/public/'}maven {url 'https://maven.aliyun.com/repository/spring/'}mavenLocal() //1.优先查找本地maven库,性能最好maven {//2.其次查找aliyun maven库url 'http://maven.aliyun.com/nexus/content/groups/public/'}mavenCentral()maven { url 'https://repo.spring.io/milestone' }maven { url 'https://repo.spring.io/snapshot' }
}ext {set('springCloudAlibabaVersion', "2.2.1.RELEASE")
}dependencies {implementation project(':api')implementation('org.apache.commons:commons-lang3:3.6')implementation 'org.springframework.boot:spring-boot-starter-web'//implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-config'implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery'implementation 'com.alibaba.cloud:spring-cloud-starter-dubbo'//implementation 'com.alibaba.cloud:spring-cloud-starter-alibaba-sentinel'//implementation project('com.chenye:api:1.0.0')//implementation 'com.chenye:api:1.0.0'//implementation group: 'org.apache.dubbo', name: 'dubbo-registry-nacos', version: '3.0.0.preview'//implementation group: 'org.apache.dubbo', name: 'dubbo-registry-nacos', version: '2.7.11'compileOnly 'org.projectlombok:lombok'developmentOnly 'org.springframework.boot:spring-boot-devtools'annotationProcessor 'org.projectlombok:lombok'testImplementation('org.springframework.boot:spring-boot-starter-test') {exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'}
}
ext {springCloudAlibabaVersion = '2.2.5.RELEASE'springCloudVersion = '2020.0.2'
}
dependencyManagement {imports {mavenBom "com.alibaba.cloud:spring-cloud-alibaba-dependencies:${springCloudAlibabaVersion}"mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"}
}
test {useJUnitPlatform()
}
server:port: 8081
spring:application:name: demo-provider1main:allow-bean-definition-overriding: truecloud:nacos:discovery:server-addr: 127.0.0.1:8848
#      config:
#        server-addr: 127.0.0.1:8848management:endpoints:web:exposure:include: '*'dubbo:application:id: demo-provider1name: demo-provider1#version: 1.0.0scan:base-packages: com.example.demo.dubbo#  protocol:#    port: 12348protocols:dubbo:name: dubboport: -1#    rest:#      name: rest#      port: 9090#      server: nettyregistry:#   The Spring Cloud Dubbo's registry extension# address: spring-cloud://localhost#   The traditional Dubbo's registry#address: zookeeper://127.0.0.1:2181address: nacos://127.0.0.1:8848?username=nacos&password=nacoschenye#address: spring-cloud://localhost
#feign:
#  hystrix:
#    enabled: true
package com.example.demo.dubbo;import com.example.demo.api.HelloService;
import org.apache.dubbo.config.annotation.DubboService;//@org.apache.dubbo.config.annotation.Service(version = "1.0.0", protocol = { "dubbo", "rest" })
//@DubboService(version = "1.0.0", protocol = { "dubbo", "rest" })
//@DubboService(version = "1.0.0", protocol = "dubbo")
@DubboService
public class HelloServiceImpl implements HelloService {@Overridepublic String sayHello(String name) {return "Hello " + name;}
}

 

 

consumer代码 

package com.example.demo.controller;import com.example.demo.api.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class SayHelloController {//@DubboReference(version = "1.0.0", url = "dubbo://192.168.31.66:20880")//@DubboReference(version = "1.0.0", protocol = "dubbo", check = false)@DubboReferenceHelloService helloService;@GetMapping("/hello")public ResponseEntity hello() {String a = helloService.sayHello("chenye");return ResponseEntity.ok(a);}@GetMapping("/test")public ResponseEntity test() {return ResponseEntity.ok("test");}
}
package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;/*** @author chenye*/
@EnableDiscoveryClient
@SpringBootApplication//(scanBasePackages = {"com.example.demo"})
public class DubboConsumerServiceConsumer {public static void main(String[] args) {SpringApplication.run(DubboConsumerServiceConsumer.class, args);}}
server:port: 8082
spring:application:name: demo-consumer1main:allow-bean-definition-overriding: true# default disable allcloud:nacos:
#      config:
#        server-addr: 127.0.0.1:8848?username=nacos&password=nacoschenyediscovery:enabled: trueregister-enabled: trueserver-addr: 127.0.0.1:8848username: nacospassword: nacoschenyezookeeper:enabled: falseconnect-string: 127.0.0.1:2181dubbo:scan:base-packages: com.example.demo.controllerapplication:#environment: v1#version: 1.0.0id: demo-consumer1name: demo-consumer1#  protocol:
#    port: 20811registry:#   The Spring Cloud Dubbo's registry extension##  the default value of dubbo-provider-services is "*", that means to subscribe all providers,##  thus it's optimized if subscriber specifies the required providers.#address: spring-cloud://localhost#address: spring-cloud://localhost#   The traditional Dubbo's registry also is supported#address: zookeeper://127.0.0.1:2181address: nacos://127.0.0.1:8848?username=nacos&password=nacoschenyecheck: falsecloud:# The subscribed services in consumer side#subscribed-services: ${provider.application.name}subscribed-services: demo-provider1consumer:check: false#version: 1.0.0

settings.gradle

pluginManagement {repositories {maven { url 'https://repo.spring.io/milestone' }maven { url 'https://repo.spring.io/snapshot' }gradlePluginPortal()}resolutionStrategy {eachPlugin {if (requested.id.id == 'org.springframework.boot') {useModule("org.springframework.boot:spring-boot-gradle-plugin:${requested.version}")}}}
}
rootProject.name = 'consumer'
includeFlat('api')

 

 

 

 

 

源码地址

https://gitee.com/stylesmile/springcloud-alibaba-study/tree/master/spring-cloud-alibaba-dubbo-demo

  相关解决方案