天天看点

springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证

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

首先使用idea创建一个项目

springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证
springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证
springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证
springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证

点击next,finish完成

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

springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证
server.port=8976

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://localhost:8976/eureka
           

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

package eureka.server.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}
           

运行改类

springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证

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

springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证

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

二.向eureka注册服务

创建一个eureka-provider项目

springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证
springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证
springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证

新建两个包

springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证

创建一个Service接口

package eureka.provider.eurekaprovider.service;

public interface MyService {
    String findSomeThing();
}
           

创建一个实现类

package eureka.provider.eurekaprovider.service.impl;

import eureka.provider.eurekaprovider.service.MyService;
import org.springframework.stereotype.Service;

@Service
public class MyServiceImpl implements MyService {


    @Override
    public String findSomeThing() {
        return "hello eureka,nice to meet you";
    }
}
           

创建一个controller以备之后调用

package eureka.provider.eurekaprovider.controller;

import eureka.provider.eurekaprovider.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyServiceController {

    @Autowired
    private MyService myService;

    @RequestMapping("/findSomething/{a}/{b}")
    public String findSomething(@PathVariable("a")Integer a,@PathVariable("b")Integer b){
        System.out.println(a+b);
        return myService.findSomeThing();
    }
}
           

找到配置文件application.properties

server.port=8189

eureka.client.service-url.defaultZone=http://localhost:8976/eureka
           

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

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

package eureka.provider.eurekaprovider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class EurekaProviderApplication {

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

}
           

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

springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证

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

springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证

找到配置文件

server.port=8189

#改变应用名称
spring.application.name=eureka-provider
#把鼠标悬浮显示主机名改成端口
eureka.instance.prefer-ip-address=true

eureka.client.service-url.defaultZone=http://localhost:8976/eureka
           

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

springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证

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

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

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
</dependency>
           

然后找到配置文件添加

server.port=8976

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false


spring.security.user.name=user
spring.security.user.password=123456


eureka.client.service-url.defaultZone=http://user:[email protected]:8976/eureka
           
springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证

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

springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证
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工程里添加一个配置类

springcloud的一些使用(非常详细)一.快速搭建并启动一个eureka服务器二.向eureka注册服务三.如何给eureka服务器添加http认证
package eureka.server.eurekaserver.config;

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;

@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
        super.configure(http);
    }
}
           

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

继续阅读