学习eureka注册中心,这一篇就够了

一、Eureka

1、新建父项目,编写父pom

新创建项目作为父项目,父pom中如下编写(创建一个springboot的聚合项目,其他的当然也可以,但是没有这个中看起来爽)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zhaojiang</groupId>
  <artifactId>spring-cloud-2020</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>spring-cloud-2020</name>
  <packaging>pom</packaging>
  <properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <springboot-version>2.0.6.RELEASE</springboot-version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <!-- SpringBoot的依赖管理-->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${springboot-version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!--Spring Cloud 依赖管理-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Finchley.SR2</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <modules>
    <!--配置子工程中的name名称,统一使用父pom的groupid,统一管理子工程-->
    <module>eureka-client-user-service</module>
    <module>eureka-server</module>
    <module>eureka-client-article-service</module>
  </modules>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>${java.version}</source>
          <target>${java.version}</target>
          <encoding>${project.build.sourceEncoding}</encoding>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

2、编写注册中心

2.1 pom配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--添加parent,由父项目统一管理版本-->
    <parent>
        <groupId>com.zhaojiang</groupId>
        <artifactId>spring-cloud-2020</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>eureka-server</artifactId>
    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <!--eureka-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

2.2 启动文件编写

//启动文件上假如@EnableEurekaServer表示开启注册中心
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

2.3 application.properties编写

spring.application.name=eureka-server
server.port=8761
#由于该应用为注册中心,所以设置为false,代表不向注册中心注册中心
eureka.client.register-with-eureka=false
#由于注册中心的职责就是维护服务实例,他并不需要去检索服务,所以也设置为false
eureka.client.fetch-registry=false

2.4 访问注册中心

http://localhost:8761/

3、编写服务提供者

3.1 pom配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.zhaojiang</groupId>
        <artifactId>spring-cloud-2020</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>eureka-client-user-service</artifactId>
    <name>eureka-client-user-service</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>
</project>

3.2 启动文件配置

//@EnableDiscoveryClient注解表示当前服务为Eureka的客户端
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientUserServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientUserServiceApplication.class, args);
    }
}

3.3 编写application.properties

spring.application.name=eureka-client-user-service
server.port=8081
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#采用IP注册
eureka.instance.prefer-ip-address=true
#定义实例id格式
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

3.4 提供接口

@RestController
public class UserController {
    @RequestMapping("/hello")
    public String hello () {
        return "hello";
    }
}

4、编写服务消费者(通过Eureka来消费接口)

4.1 pom配置(与服务提供者一样)

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.zhaojiang</groupId>
    <artifactId>spring-cloud-2020</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>eureka-client-article-service</artifactId>

  <name>eureka-client-article-service</name>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--eureka-->
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
  </dependencies>
</project>

4.2 编写启动类(与服务提供者一样)

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

4.3编写application.properties(与服务提供者只是服务名和端口号不一样)

spring.application.name=eureka-client-article-service
server.port=8082
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
#采用IP注册
eureka.instance.prefer-ip-address=true
#定义实例id格式
eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}

4.4 配置RestTemplate,用于访问Rest服务的客户端

//@LoadBalanced注解会自动构造LoadBalancerClient接口的实现类并注册到Spring容器中
@Configuration
public class BeanConfiguration {
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

4.5 编写接口访问服务提供者接口

@RestController
public class ArticleController {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/article/callHello")
    public String callHello () {
        return restTemplate.getForObject("http://eureka-client-user-service/hello", String.class);
    }
}


5、开启Eureka认证

5.1背景介绍

Eureka自带一个web管理页面,方便我们查询到上面的实例信息,但是有一个问题,如何实际使用中,注册中心地址有公网ip的话,必然能直接访问到,这样是不安全的,因此,我们对Eureka进行改造,加上权限认证来保证安全性
5.2 eureka-server的pom中添加依赖

<!--eureka权限认证-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

5.3 application.properties添加

#eureka 权限认证
spring.security.user.name=leizhaojiang
spring.security.user.password=123456

5.4 添加配置

注意不要把该类建在和启动类同级了,已经犯过此类错误

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //关闭csrf,防止 CSRF 攻击、 XSS 攻击。
        http.csrf().disable();
        //所有的请求访问都需要被授权。
        http.authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                //支持httpBasic,启用 HTTP Basic 认证
                .httpBasic();
    }
}

5.5 eureka client中添加账户密码

eureka.client.serviceUrl.defaultZone=http://leizhaojiang:123456@localhost:8761/eureka/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值