您现在的位置是:首页 >技术杂谈 >Spring Cloud 微服务备忘网站首页技术杂谈

Spring Cloud 微服务备忘

changxiang 2023-05-15 00:00:02
简介Spring Cloud 微服务备忘

nacos

1.下载,解压,执行: startup.bat -m standalone   即可用localhost:8848/nacos  进行访问。

2.新建Maven 项目,pom里定义boot cloud alibaba的版本号

引入:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <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>

在项目里加入一个 springboot的模块项目,加入依赖:

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

再新建启动类和一个Controller 。

3.建立一个发现服务:新建一个子模块,依赖为:

<dependencies>
    <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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
</dependencies>

配置为:

server.port=8083
server.servlet.context-path=/


spring.application.name=nacos-info
spring.cloud.nacos.server-addr=www.xxxx.com:8848
spring.cloud.nacos.discovery.server-addr=www.xxxxx.com:8848

启动类上加:

@SpringBootApplication
@EnableDiscoveryClient

注意:如果nacosserver是远程的,本地调试也要把8848的服务启动起来,否则会报错:连接类的错。

4.服务调用,使用OpenFeign:

首先,在调用服务的项目上添加依赖:注意加:spring-cloud-loadbalancer

 

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--解决:Did you forget to include spring-cloud-starter-loadbalancer?-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>

其次,写一个feign的service:

@FeignClient("nacos-info")//这是服务名
public interface InfoFeignService {
    @RequestMapping("/info/get")
    public String get(@RequestParam("name") String name);
}

再者:写一个调用服务的Action:

@Resource InfoFeignService infoFeignService;
@RequestMapping("/info")
public String getInfo(String key){
    return infoFeignService.get(key);
}

最后,项目启动类上加:

@EnableFeignClients(basePackages = "org.xxxx.feign")

即可调用服务。

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。