SpringCloud 中Eureka中RestTemplate进行服务调用

项目结构如下图所示:

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

让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:[email protected]: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();
    }
}

成功访问页面

欢迎关注摘星族

发布了56 篇原创文章 · 获赞 22 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/weixin_41986096/article/details/93752340