SpringCloudAlibabaDubbo实践+Docker编排(转载请注明来源:cnblogs coder-fang)
本实例包括Nacos服务发现,配置中⼼,sentinel 容断简单使⽤,Spring cloud alibaba 的dubbo实例及所有服务的docker部署。
创建三个Maven独⽴⼯程,接⼝API,provier,consumer
  DemoAPI提供服务的通⽤接⼝,创建为普通Maven项⽬,接⼝打包到中⼼仓库供其它⼯程使⽤,POM⽂件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spcloud-dubbo</groupId>
<artifactId>demo-api</artifactId>
<version>1.0</version>
<!-- 发布到仓库 -->
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>nexusserver:6060/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>nexusserver:6060/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
</project>
View Code
  Demo代码,只提供⼀个测试接⼝
package  spcloud.demoapi;
springcloud和springbootpublic interface IApiService {
String Hello(String name);
}
创建Provider⼯程(Spring boot 项⽬),POM⽂件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>com.spcouddubbo.demo</groupId>
<artifactId>provider</artifactId>
<version>1.0</version>
<name>provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>spcloud-dubbo</groupId>
<artifactId>demo-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.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>
View Code
配置⽂件与代码如下:
  Bootstrap.yaml
spring:
application:
name: provider
main:
allow-bean-definition-overriding: true
cloud:
nacos:
discovery:
enabled: true
register-enabled: true
server-addr: nacosserver:8848
config:
server-addr: nacosserver:8848
View Code
  Application.yaml
dubbo:
scan:
base-packages: com.spcouddubbo.demo.provider
protocols:
dubbo:
name: dubbo
port: -1
cloud:
subscribed-services:
demo:
user: nouser
View Code
  接⼝实现类(此类会使⽤demo.user的配置,如果⽆法获取配置中⼼数据,则使⽤本地配置)
package com.spcouddubbo.demo.provider;
import com.alibaba.fig.annotation.NacosValue;
import org.fig.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.t.config.annotation.RefreshScope;
import spcloud.demoapi.IApiService;
@Service
@RefreshScope
public class APIImplment implements IApiService {
@Value("${demo.user}")
String user;
@Override
public String Hello(String s) {
return "hello 1 "+s+" "+user;
}
}
View Code
  在 ProviderApplication 类上加⼊注解:@EnableDiscoveryClient
  执⾏ mvn package 打包,⽣成  provider-1.0.jar
创建Consumer⼯程(Spring boot WEB项⽬),POM⽂件如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"        xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.
xsd"> <modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>com.spclouddubbo.demo</groupId>
<artifactId>consumer</artifactId>
<version>1.0</version>
<name>consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>spcloud-dubbo</groupId>
<artifactId>demo-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.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>
View Code
配置与代码如下:
  Bootstrap.yaml
spring:
application:
name: comsumer
main:
allow-bean-definition-overriding: true
cloud:
nacos:
discovery:
enabled: true
register-enabled: true
server-addr: nacosserver:8848
config:
server-addr: nacosserver:8848
View Code
  Application.yaml
dubbo:
cloud:
subscribed-services: provider
consumer:
check: false
  主要代码如下:
package com.sumer;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.fig.annotation.Reference;
import org.apache.dubbo.rpc.RpcException;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.*;
import spcloud.demoapi.IApiService;
@SpringBootApplication
@RestController
@EnableDiscoveryClient
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
@Reference
IApiService service;
@ResponseBody
@GetMapping("/")
@SentinelResource(fallbackClass = ProviderError.class,defaultFallback = "errorService")
public String Getname( String name)
{
return  service.Hello(name);
}
}
View Code
  服务失效回调代码:
package com.sumer;
public class ProviderError {
public static  String errorService() {
return "Call Provider services error";
}
}
  执⾏ mvn package 打包,⽣成consumer-1.0.jar
服务部署(以单主机为例,分别在两个⽬录中进⾏Nacos与应⽤服务的部署):
 创建桥接⽹络  docker network create -d bridge localnet
 部署nacos服务:
  docker 下拉 mysql:5.6, nacos/nacos-server
  ⽬录结构如图:
  l
version: "3.1"
services:
db:
image: mysql:5.6
restart: always
container_name: nacosdb
hostname: nacosdb
volumes:
- ./mysql:/var/lib/mysql
networks:
- localnet
nacos:
image: nacos/nacos-server
container_name: nacos-standalone-mysql
hostname: nacoshost
env_file:
- ./v
volumes:
- ./logs/:/home/nacos/logs
- ./custom.properties:/home/nacos/init.d/custom.properties
ports:
- "8848:8848"
- "9555:9555"
restart: "no"
depends_on:
- db
networks:
- localnet
links:
- db
networks:
localnet:
external: true

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。