依赖版本:
spring-boot:2.3.12.RELEASE
spring-cloud-alibaba:2.2.7.RELEASE
spring-cloud:Hoxton.SR12
nacos:2.0.3
Linux Nacos 快速启动_nacos linux快速启动-CSDN博客
源码地址
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.12.RELEASE
com.ocean
dubbo-cloud
pom
1.0-SNAPSHOT
dubbo-order
dubbo-user
UTF-8
UTF-8
1.8
Hoxton.SR12
2.2.7.RELEASE
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
${spring-cloud-alibaba.version}
pom
import
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-starter
com.alibaba.cloud
spring-cloud-starter-dubbo
spring-context-support
com.alibaba.spring
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.projectlombok
lombok
true
com.alibaba.spring
spring-context-support
1.0.11
dubbo-cloud
com.ocean
1.0-SNAPSHOT
4.0.0
dubbo-user
pom
dubbo-user-api
dubbo-user-provider
8
8
dubbo-user
com.ocean
1.0-SNAPSHOT
4.0.0
dubbo-user-api
UTF-8
UTF-8
1.8
com.ocean
dubbo-user-provider
1.0-SNAPSHOT
org.springframework.boot
spring-boot-maven-plugin
dubbo-user
com.ocean
1.0-SNAPSHOT
4.0.0
jar
dubbo-user-provider
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
true
目录
代码
UserProviderService.java
package com.ocean.provider;
public interface UserProviderService {
String getUserName();
}
目录
代码
UserApi.java
package com.ocean.controller;
import com.ocean.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class UserApi {
@Resource
private UserService userService;
@GetMapping(value = "/api/user/getName")
public String getOrderInfo() {
return userService.getName();
}
}
UserProviderServiceImpl.java
package com.ocean.provider;
import com.ocean.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import javax.annotation.Resource;
@DubboService
public class UserProviderServiceImpl implements UserProviderService {
@Resource
private UserService userService;
@Override
public String getUserName() {
return userService.getName();
}
}
UserService.java
package com.ocean.service;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class UserService {
public String getName() {
return UUID.randomUUID().toString();
}
}
UserApplication.java
package com.ocean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
application.yml
server:
port: 6668
spring:
application:
name: user
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
main:
allow-bean-definition-overriding: true
dubbo:
application:
qos-enable: false
id: user
scan:
base-packages: com.ocean.provider
protocols:
dubbo:
name: dubbo
port: -1
registry:
address: spring-cloud://127.0.0.1
dubbo-cloud
com.ocean
1.0-SNAPSHOT
4.0.0
dubbo-order
pom
dubbo-order-api
8
8
dubbo-order
com.ocean
1.0-SNAPSHOT
4.0.0
dubbo-order-api
UTF-8
UTF-8
1.8
com.ocean
dubbo-user-provider
1.0-SNAPSHOT
org.springframework.boot
spring-boot-maven-plugin
OrderApi.java
package com.ocean.controller;
import com.ocean.service.OrderService;
import com.ocean.vo.OrderVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class OrderApi {
@Resource
private OrderService orderService;
@GetMapping(value = "/api/order/{id}")
public OrderVO getOrderInfo(@PathVariable("id") String id) {
return orderService.getOrderInfo(id);
}
}
OrderService.java
package com.ocean.service;
import com.ocean.provider.UserProviderService;
import com.ocean.vo.OrderVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
@DubboReference
private UserProviderService userProviderService;
public OrderVO getOrderInfo(String id) {
return new OrderVO(id, userProviderService.getUserName());
}
}
OrderVO.java
package com.ocean.vo;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@ToString
@Getter
@Setter
public class OrderVO {
private String orderId;
private String userName;
public OrderVO(String orderId, String userName) {
this.orderId = orderId;
this.userName = userName;
}
}
OrderApplication.java
package com.ocean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class, args);
}
}
application.yml
server:
port: 6667
spring:
application:
name: order
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
main:
allow-bean-definition-overriding: true
dubbo:
cloud:
subscribed-services: user
application:
qos-enable: false
id: order
scan:
base-packages: com.ocean.provider
protocols:
dubbo:
name: dubbo
port: -1
registry:
address: spring-cloud://127.0.0.1
https://download.csdn.net/download/qq_34253002/88522267?spm=1001.2014.3001.5501