SpringCloud 中Eureka中RestTemplate进行服务调用

本文详细介绍了使用Eureka实现服务治理的过程,包括服务注册、安全配置、服务提供与消费等关键步骤,展示了如何通过RestTemplate进行服务调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目结构如下图所示:

 

在服务治理当中,没有固定的提供方和消费方,只是以上述为例

让yyc-demo 去注册中心调用yyc-test提供的服务。

这次将上两次学习进行整理:

首先是服务注册中心,服务提供者可以将服务注册到注册中心供服务调用方获取.

注册中心中进行安全认证需要引入jar包

<!--设置登录密码需要用到Spring Security-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

在配置中需要设置:

#设置eurekaServer的登录密码
spring:
  security:
    user:
      name: admin  # 用户名
      password: admin   # 用户密码

注册中心需要关闭CSRF验证,否则客户端将不能进行注册服务

package com.zhaixingzu.yyc.registry.security;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


/**
 * @author Herbert
 * @date 2019/06/24
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
   @Override
   protected void configure(HttpSecurity http) {
      try {
         http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/actuator/**").permitAll()
                .anyRequest()
                .authenticated().and().httpBasic();
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}

注册中心应该注意eureka交互地址的正确性

具体详见 - 注册中心:

Eureka服务治理

服务提供yyc-test中,新建controller,写服务接口

package com.zhaixingzu.yyc.test.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Herbert on 2019/6/24.
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/show")
    public String show(){
        return "hello";
    }

}

在bootstrap.yml 文件中配置

server:
  port: 8800

spring:
  application:
    name: yyc-test

eureka:
  client:
      serviceUrl:
            defaultZone: http://admin:admin@127.0.0.1:9900/eureka/  #设置与eureka交互的地址

具体详见 - 服务提供:

Eureka服务注册

服务消费方 yyc-demo 进行服务调用

第一种通过RestTemplate来发送http请求

我们需要在DemoApplication中加入

package com.zhaixingzu.yyc.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

/**
 *
 * @author Herbert
 * @date 2019年06月20日
 */
@EnableEurekaClient
@SpringBootApplication

public class DemoApplication {

   public static void main(String[] args) {
      SpringApplication.run(DemoApplication.class, args);
   }

   @Bean // 自动扫描
   @LoadBalanced //这个注解的意思是在启动时先加载注册中心的域名列表
   public RestTemplate restTemplate() //这个方法用来发http请求
   {
      RestTemplate restTemplate=new RestTemplate();
      return restTemplate;
   }
}

 

让它自动扫描加载注册中心域名列表,在

DemoController中去调用注册中心的服务

 

package com.zhaixingzu.yyc.demo.controller;

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;
import org.springframework.web.client.RestTemplate;

/**
 * Created by Herbert on 2019/6/25.
 */
@RestController
@RequestMapping("/demo")
public class DemoController {


    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/show")
    public String show(){
        return restTemplate.getForEntity("http://yyc-test/test/show",String.class).getBody();
    }
}

 

成功访问页面

欢迎关注摘星族

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨迹嘿嘿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值