springcloud的一些使用(非常详细)

    springcloud 专栏收录该内容
    1 篇文章 0 订阅

    一.快速搭建并启动一个eureka服务器

    首先使用idea创建一个项目

    点击next,finish完成

    然后在项目中找到springboot的配置文件application.properties文件,配置如下内容

    1. server.port=8976
    2. eureka.client.register-with-eureka=false
    3. eureka.client.fetch-registry=false
    4. eureka.client.service-url.defaultZone=http://localhost:8976/eureka

    找到启动类并添加注解@EnableEurekaServer

    1. package eureka.server.eurekaserver;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    5. @SpringBootApplication
    6. @EnableEurekaServer
    7. public class EurekaServerApplication {
    8. public static void main(String[] args) {
    9. SpringApplication.run(EurekaServerApplication.class, args);
    10. }
    11. }

    运行改类

    在浏览器中输入http://localhost:8976即可看到服务注册情况

    以上我们完成了简单的eureka服务搭建,那接下来我们怎么注册服务呢

    二.向eureka注册服务

    创建一个eureka-provider项目

    新建两个包

    创建一个Service接口

    1. package eureka.provider.eurekaprovider.service;
    2. public interface MyService {
    3. String findSomeThing();
    4. }

    创建一个实现类

    1. package eureka.provider.eurekaprovider.service.impl;
    2. import eureka.provider.eurekaprovider.service.MyService;
    3. import org.springframework.stereotype.Service;
    4. @Service
    5. public class MyServiceImpl implements MyService {
    6. @Override
    7. public String findSomeThing() {
    8. return "hello eureka,nice to meet you";
    9. }
    10. }

    创建一个controller以备之后调用

    1. package eureka.provider.eurekaprovider.controller;
    2. import eureka.provider.eurekaprovider.service.MyService;
    3. import org.springframework.beans.factory.annotation.Autowired;
    4. import org.springframework.web.bind.annotation.PathVariable;
    5. import org.springframework.web.bind.annotation.RequestMapping;
    6. import org.springframework.web.bind.annotation.RestController;
    7. @RestController
    8. public class MyServiceController {
    9. @Autowired
    10. private MyService myService;
    11. @RequestMapping("/findSomething/{a}/{b}")
    12. public String findSomething(@PathVariable("a")Integer a,@PathVariable("b")Integer b){
    13. System.out.println(a+b);
    14. return myService.findSomeThing();
    15. }
    16. }

    找到配置文件application.properties

    1. server.port=8189
    2. eureka.client.service-url.defaultZone=http://localhost:8976/eureka

    上面的eureka.client.service-url.defaultZone配置和之前一步骤中的配置相同,表示注册到刚刚启动的eureka服务器上

    找到启动类,并添加注解@EnableEurekaClient,然后启动应用

    1. package eureka.provider.eurekaprovider;
    2. import org.springframework.boot.SpringApplication;
    3. import org.springframework.boot.autoconfigure.SpringBootApplication;
    4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    5. @SpringBootApplication
    6. @EnableEurekaClient
    7. public class EurekaProviderApplication {
    8. public static void main(String[] args) {
    9. SpringApplication.run(EurekaProviderApplication.class, args);
    10. }
    11. }

    刷新http://localhost:8976/连接,大家可以看到服务已经被注册上了

    但是以上有两个问题,一个就是apllication的名字是unknow,这个可以设置,另外鼠标放到连接处左下角显示的是主机名和端口,能不能显示成ip呢,这个也是可以设置的

    找到配置文件

    1. server.port=8189
    2. #改变应用名称
    3. spring.application.name=eureka-provider
    4. #把鼠标悬浮显示主机名改成端口
    5. eureka.instance.prefer-ip-address=true
    6. eureka.client.service-url.defaultZone=http://localhost:8976/eureka

    添加以上两处配置,启动再看下结果

    三.如何给eureka服务器添加http认证

    首先要在eureka-server工程里添加jar包spring-boot-starter-security

    1. <dependency>
    2. <groupId>org.springframework.boot</groupId>
    3. <artifactId>spring-boot-starter-security</artifactId>
    4. </dependency>

    然后找到配置文件添加

    1. server.port=8976
    2. eureka.client.register-with-eureka=false
    3. eureka.client.fetch-registry=false
    4. spring.security.user.name=user
    5. spring.security.user.password=123456
    6. eureka.client.service-url.defaultZone=http://user:123456@localhost:8976/eureka

    然后同样需要在eureka-provider工程里的配置文件中修改

    com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

    分别启动这两个工程,会发现启动eureka-provider工程时报以上错误,搜索资料发现新版(Spring Cloud 2.0 以上)的security默认启用了csrf检验,要在eurekaServer端配置security的csrf检验为false

    所以在eureka-server工程里添加一个配置类

    1. package eureka.server.eurekaserver.config;
    2. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    3. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
    5. @EnableWebSecurity
    6. public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    7. @Override
    8. protected void configure(HttpSecurity http) throws Exception {
    9. http.csrf().disable();
    10. super.configure(http);
    11. }
    12. }

    再次启动就可以正常访问了

    • 0
      点赞
    • 0
      评论
    • 0
      收藏
    • 一键三连
      一键三连
    • 扫一扫,分享海报

    相关推荐
    ©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

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

    余额充值

    举报

    选择你想要举报的内容(必选)
    • 内容涉黄
    • 政治相关
    • 内容抄袭
    • 涉嫌广告
    • 内容侵权
    • 侮辱谩骂
    • 样式问题
    • 其他
    新手
    引导
    客服 举报 返回
    顶部