dubbo微服务

一.启动nacos和redis

1.虚拟机查看是否开启nacos和redis

docker ps

2.查看是否安装nacos和redis

docker ps -a

3.启动nacos和redis

docker start nacos
docker start redis-6379
docker ps

二.创建三个idea的maven项目

1.第一个项目dubboapidemo

2.1.1向pom.xml里添加依赖

XML 复制代码
<properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
        <jwt.version>0.7.0</jwt.version>
        <fastjson.version>1.2.60</fastjson.version>
        <spring-cloud-alibaba.version>2.0.3.RELEASE</spring-cloud-alibaba.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>
    <dependencies>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <scope>compile</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>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>

    <!--2.添加当前项目所依赖的插件: -->
    <build>
        <plugins>
            <!--1.添加tomcat插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8080</port>
                    <path>/</path>
                </configuration>
            </plugin>
            <!--2.添加Spring Boot 的打包插件 -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.10.RELEASE</version>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.1.2添加实体类Score

java 复制代码
package com.jr.entry;

import lombok.Data;
import java.io.Serializable;

@Data
public class Score implements Serializable {
    private String name;
    private Double score;
}

2.1.3添加ScoreService接口

java 复制代码
package com.jr.service;

import com.jr.entry.Score;
import java.util.List;

public interface ScoreService {

    List<Score> getinfo();

}

2.1.4添加枚举ResultCode

java 复制代码
package com.jr.util;
public enum ResultCode {
    SUCCESS(0, "请求成功"),
    ERROR(1, "请求失败"),
    ;

    private int code;
    private String message;

    ResultCode(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}

2.1.5添加工具类Result

java 复制代码
package com.jr.util;

import lombok.Data;
import java.util.HashMap;
import java.util.Map;

@Data
public class Result {

    private Integer code;
    private String message;
    private Map<String, Object> map = new HashMap<>();

    private Result() {
    }

    public static Result ok() {
        Result r = new Result();
        r.setCode(ResultCode.SUCCESS.getCode());
        r.setMessage(ResultCode.SUCCESS.getMessage());
        return r;
    }

    public static Result error() {
        Result r = new Result();
        r.setCode(ResultCode.ERROR.getCode());
        r.setMessage(ResultCode.ERROR.getMessage());
        return r;
    }

    public Result put(String key, Object value) {
        map.put(key, value);
        return this;
    }

    public Object get(String key) {
        return map.get(key);
    }
}

2.1.6把项目打包成jar包

点开idea右边的maven

然后打开左边项目的target,下面就已经出现打包后的jar包了。

2.第二个项目dubboconsumer

2.2.1添加依赖

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jr</groupId>
    <artifactId>dubboconsumer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
        <jwt.version>0.7.0</jwt.version>
        <fastjson.version>1.2.60</fastjson.version>
        <spring-cloud-alibaba.version>2.0.3.RELEASE</spring-cloud-alibaba.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--json-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</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-nacos-config</artifactId>
        </dependency>
        <!--引入api工程依赖 -->
        <dependency>
            <groupId>com.jr</groupId>
            <artifactId>dubboapidemo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--引入dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </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>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>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

这里是第一个项目的信息,根据你的来,这里爆红可能是第一个项目没有进行打包操作。注意这两个项目的groupid一样才能这么互相导依赖。

2.2.2创建SessionConfig配置类

java 复制代码
package com.jr.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

@Configuration
public class SessionConfig {

    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setCookieName("JSESSIONID");
        serializer.setCookiePath("/");
        serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
        return serializer;
    }

    @Bean
    public RedisSerializer<Object> redisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }
}

2.2.3创建实体类User

java 复制代码
package com.jr.entry;

import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Data
public class User {

    private String id;
    private String name;
    private String password;
    private List<Score> scoreList;
}

2.2.4创建UserService接口

java 复制代码
package com.jr.service;

import com.jr.entry.User;
import org.springframework.stereotype.Component;

@Component
public interface UserService {

    public User getinfo();
}

2.2.5创建UserServiceImpl实现类

java 复制代码
package com.jr.service.impl;

import com.jr.entry.Score;
import com.jr.entry.User;
import com.jr.service.ScoreService;
import com.jr.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Reference
    private ScoreService scoreService;

    @Override
    public User getinfo() {
        User user=new User();
        user.setId("111");
        user.setName("zhangsan");
        user.setPassword("zs123");
        List<Score> list=scoreService.getinfo();
        user.setScoreList(list);
        return user;
    }
}

2.2.6创建控制器UserController

java 复制代码
package com.jr.controller;

import com.jr.entry.User;
import com.jr.service.UserService;
import com.jr.util.Result;
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.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public Result getinfo(){
        User user = userService.getinfo();
        return Result.ok().put("data",user);
    }
}

2.2.7创建启动类

java 复制代码
package com.jr;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@SpringBootApplication
@EnableRedisHttpSession
public class SpringBootMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMain.class,args);
    }
}

2.2.8创建application.properties配置文件

java 复制代码
spring.application.name=dubbocon

2.2.9创建application.yml配置文件

java 复制代码
server:
  port: 111

spring:
  session:
    store-type: redis

dubbo:
  registry:
    address: nacos://192.168.130.39:8848
  cloud:
    subscribed-services: dubbopro   #是p工程的服务名
  consumer:
    check: false

2.2.10创建bootstrap.yml配置文件

java 复制代码
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.130.39:8848
      config:
        server-addr: 192.168.130.39:8848
        file-extension: yaml
        namespace: 8920c585-274d-477f-b55f-9324be8da9bd
        shared-configs:
          - data-id: dz14redisssesiton.yaml
            group: DEFAULT_GROUP
    inetutils:
      preferred-networks: 192.168.116

3.第三个项目dubboprovider

2.3.1向pom.xml添加依赖

XML 复制代码
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.7.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
        <jwt.version>0.7.0</jwt.version>
        <fastjson.version>1.2.60</fastjson.version>
        <spring-cloud-alibaba.version>2.0.3.RELEASE</spring-cloud-alibaba.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--json-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</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-nacos-config</artifactId>
        </dependency>
        <!--引入api工程依赖 -->
        <dependency>
            <groupId>com.jr</groupId>
            <artifactId>dubboapidemo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--引入dubbo依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-dubbo</artifactId>
        </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>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>
                <configuration>
                    <classifier>exec</classifier>
                </configuration>
            </plugin>
        </plugins>
    </build>

2.3.2创建SessionConfig配置类

java 复制代码
package com.jr.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

@Configuration
public class SessionConfig {

    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer serializer = new DefaultCookieSerializer();
        serializer.setCookieName("JSESSIONID");
        serializer.setCookiePath("/");
        serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
        return serializer;
    }

    @Bean
    public RedisSerializer<Object> redisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }
}

2.3.3创建ScoreServiceImpl接口实现类

@Service主键要用dubbo下面的,不能用Spring的注解

java 复制代码
package com.jr.service.impl;

import com.jr.entry.Score;
import com.jr.service.ScoreService;
import org.apache.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ScoreServiceImpl implements ScoreService {
    @Override
    public List<Score> getinfo() {
        List<Score> list=new ArrayList<>();
        for(int i=0;i<3;i++){
            Score score=new Score();
            score.setName("admin");
            score.setScore(Math.random()*100);
            list.add(score);
        }
        return list;
    }
}

2.3.4创建启动类

java 复制代码
package com.jr;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.stereotype.Component;

@SpringBootApplication
@EnableRedisHttpSession
@ComponentScan("com.jr.service")
public class SpringBootMain {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootMain.class,args);
    }
}

2.3.5创建application.properties配置文件

java 复制代码
spring.application.name=dubbopro

2.3.6编写application.yml配置文件

java 复制代码
server:
  port: 222

spring:
  session:
    store-type: redis

dubbo:
  scan:
    base-packages: com.jr.service  #指定把哪一个包下面的Servlet交给dubbo去管理。
  protocol:
    name: dubbo
    port: -1
  registry:
    address: nacos://192.168.130.39:8848
  cloud:
    subscribed-services: ""

2.3.7编写bootstrap.yml配置文件

和第二个项目一样

java 复制代码
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.130.39:8848
      config:
        server-addr: 192.168.130.39:8848
        file-extension: yaml
        namespace: 8920c585-274d-477f-b55f-9324be8da9bd
        shared-configs:
          - data-id: dz14redisssesiton.yaml
            group: DEFAULT_GROUP
    inetutils:
      preferred-networks: 192.168.116

三.同时启动项目,用postman工具进行测试

启动第二个项目和第三个项目。启动时,要先启动consumer工程,后启动provider工程,避免出现启动失败的错误。

启动完后,我们打开postman工具进行测试。

四. 自己的idea项目

1.第一个项目dubboapidemo

2.第二个项目dubboconsumer

3.第三个项目dubboprovider

相关推荐
哔哥哔特商务网9 小时前
一文探究48V新型电气架构下的汽车连接器
架构·汽车
岁岁岁平安9 小时前
springboot实战(15)(注解@JsonFormat(pattern=“?“)、@JsonIgnore)
java·spring boot·后端·idea
007php0079 小时前
GoZero 上传文件File到阿里云 OSS 报错及优化方案
服务器·开发语言·数据库·python·阿里云·架构·golang
码上有前11 小时前
解析后端框架学习:从单体应用到微服务架构的进阶之路
学习·微服务·架构
gjh120813 小时前
什么是微服务?
微服务
supercool714 小时前
SpringBoot(9)-Dubbo+Zookeeper
spring boot·dubbo·java-zookeeper
我梦Leo15 小时前
【IDEA】解决总是自动导入全部类(.*)问题
idea
货拉拉技术17 小时前
多元消息融合分发平台
javascript·后端·架构
问窗18 小时前
微服务中Spring boot的包扫描范围
java·spring boot·微服务
冷心笑看丽美人18 小时前
Spring 框架七大模块(Java EE 学习笔记03)
学习·spring·架构·java-ee