温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

springcloud服务提供与调用多注意点

发布时间:2021-12-02 15:50:08 来源:亿速云 阅读:140 作者:柒染 栏目:软件技术

本篇文章为大家展示了springcloud服务提供与调用多注意点,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

下面介绍一下如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用的案例。

案例中有三个角色:服务注册中心、服务提供者、服务消费者,其中服务注册中心就是eureka单机版启动既可,流程是首先启动注册中心,服务提供者生产服务并注册到服务中心中,消费者从服务中心中获取服务并执行。

服务提供

我们假设服务提供者有一个hello方法,可以根据传入的参数,提供输出“hello ,this is first messge”的服务

1、pom包配置

创建一个springboot项目,pom.xml中添加如下配置:

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

2、配置文件

application.properties配置如下:

spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3、启动类

启动类中添加@EnableDiscoveryClient注解

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
 
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}

4、controller

提供hello服务

@RestController
public class HelloController {
    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+",this is first messge";
    }
}

添加@EnableDiscoveryClient注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。

springcloud服务提供与调用多注意点

到此服务提供者配置就完成了。

服务调用

1、pom包配置

和服务提供者一致

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

2、配置文件

application.properties配置如下:

spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3、启动类

启动类添加@EnableDiscoveryClient和@EnableFeignClients注解。

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
 
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
 
}

@EnableDiscoveryClient :启用服务注册与发现

@EnableFeignClients:启用feign进行远程调用

Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡

4、feign调用实现

@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);
}

name:远程服务名,及spring.application.name配置的名称

此类中的方法和远程服务中contoller中的方法名和参数需保持一致。

5、web层调用远程服务

将HelloRemote注入到controller层,像普通方法一样去调用即可。

@RestController
public class ConsumerController {
 
    @Autowired
    HelloRemote HelloRemote;
    @RequestMapping("/hello/{name}")
    public String index(@PathVariable("name") String name) {
        return HelloRemote.hello(name);
    }
 
}

到此,最简单的一个服务注册与调用的例子就完成了。

上述内容就是springcloud服务提供与调用多注意点,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI